110 lines
3.5 KiB
PHP
110 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service\qywx;
|
|
|
|
use app\common\service\DataScope\DataScopeService;
|
|
use think\facade\Db;
|
|
|
|
/** 分流方案共享操作人产生的页面入口与专用数据范围。 */
|
|
final class QywxPromotionOperatorAccess
|
|
{
|
|
public const PAGE_PERMISSION = 'firstvisit.wecomPromotion/overview';
|
|
|
|
public static function hasBasePagePermission(int $adminId, array $adminInfo = []): bool
|
|
{
|
|
if ((int) ($adminInfo['root'] ?? 0) === 1) {
|
|
return true;
|
|
}
|
|
if ($adminId <= 0) {
|
|
return false;
|
|
}
|
|
|
|
return Db::name('admin_role')->alias('ar')
|
|
->join('system_role_menu rm', 'rm.role_id = ar.role_id')
|
|
->join('system_menu m', 'm.id = rm.menu_id')
|
|
->where('ar.admin_id', $adminId)
|
|
->where('m.perms', self::PAGE_PERMISSION)
|
|
->where('m.is_disable', 0)
|
|
->count() > 0;
|
|
}
|
|
|
|
public static function hasSharedPagePermission(int $adminId): bool
|
|
{
|
|
if ($adminId <= 0 || !self::pageMenuEnabled()) {
|
|
return false;
|
|
}
|
|
try {
|
|
return Db::name('qywx_promotion_pool_operator')->alias('po')
|
|
->join('qywx_promotion_pool p', 'p.id = po.pool_id')
|
|
->where('po.admin_id', $adminId)
|
|
->whereNull('po.delete_time')
|
|
->whereNull('p.delete_time')
|
|
->count() > 0;
|
|
} catch (\Throwable $error) {
|
|
if (self::isMissingTable($error)) {
|
|
return false;
|
|
}
|
|
throw $error;
|
|
}
|
|
}
|
|
|
|
public static function hasPagePermission(int $adminId, array $adminInfo = []): bool
|
|
{
|
|
return self::hasBasePagePermission($adminId, $adminInfo)
|
|
|| self::hasSharedPagePermission($adminId);
|
|
}
|
|
|
|
/** 基础页面权限沿用角色数据范围;纯共享账号只能通过 operator pool 范围访问。 */
|
|
public static function visibleAdminIds(int $adminId, array $adminInfo): ?array
|
|
{
|
|
return self::hasBasePagePermission($adminId, $adminInfo)
|
|
? DataScopeService::getVisibleAdminIds($adminId, $adminInfo)
|
|
: [];
|
|
}
|
|
|
|
/** @return list<int> */
|
|
public static function activePoolIds(int $adminId): array
|
|
{
|
|
if ($adminId <= 0) {
|
|
return [];
|
|
}
|
|
try {
|
|
$ids = Db::name('qywx_promotion_pool_operator')->alias('po')
|
|
->join('qywx_promotion_pool p', 'p.id = po.pool_id')
|
|
->where('po.admin_id', $adminId)
|
|
->whereNull('po.delete_time')
|
|
->whereNull('p.delete_time')
|
|
->column('po.pool_id');
|
|
} catch (\Throwable $error) {
|
|
if (self::isMissingTable($error)) {
|
|
return [];
|
|
}
|
|
throw $error;
|
|
}
|
|
|
|
return array_values(array_unique(array_filter(array_map(
|
|
static fn ($value): int => (int) $value,
|
|
$ids
|
|
), static fn (int $value): bool => $value > 0)));
|
|
}
|
|
|
|
private static function pageMenuEnabled(): bool
|
|
{
|
|
return Db::name('system_menu')
|
|
->where('perms', self::PAGE_PERMISSION)
|
|
->where('is_disable', 0)
|
|
->count() > 0;
|
|
}
|
|
|
|
private static function isMissingTable(\Throwable $error): bool
|
|
{
|
|
$message = strtolower($error->getMessage());
|
|
|
|
return str_contains($message, '42s02')
|
|
|| str_contains($message, '1146')
|
|
|| str_contains($message, 'no such table');
|
|
}
|
|
}
|