1.将部门修改为渠道,并且所有dice_表关联渠道表
2.将所有配置表,记录表设置关联渠道 3.优化后台页面设置
This commit is contained in:
@@ -3,21 +3,75 @@ declare(strict_types=1);
|
||||
|
||||
namespace app\dice\helper;
|
||||
|
||||
use app\dice\model\player\DicePlayer;
|
||||
use plugin\saiadmin\app\model\system\SystemUser;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* 管理员数据范围辅助类
|
||||
* 用于获取当前管理员及其部门下属管理员可访问的数据范围
|
||||
* 大富翁数据范围:按渠道 dept_id 隔离(关联 sa_system_dept)
|
||||
*/
|
||||
class AdminScopeHelper
|
||||
{
|
||||
/** 超管查看默认配置模板 */
|
||||
public const DEFAULT_TEMPLATE_DEPT = 0;
|
||||
|
||||
/**
|
||||
* 获取当前管理员可访问的 admin_id 列表
|
||||
* 超级管理员(id=1) 返回 null 表示不限制
|
||||
* 普通管理员返回其本人及部门下属管理员的 id 列表
|
||||
* 当前管理员所属渠道 ID;超级管理员(id=1) 返回 null 表示不限制
|
||||
*/
|
||||
public static function getDeptId(?array $adminInfo): ?int
|
||||
{
|
||||
if (empty($adminInfo) || !isset($adminInfo['id'])) {
|
||||
return null;
|
||||
}
|
||||
$adminId = (int) $adminInfo['id'];
|
||||
if ($adminId <= 1) {
|
||||
return null;
|
||||
}
|
||||
$deptList = $adminInfo['deptList'] ?? [];
|
||||
if (!empty($deptList['id'])) {
|
||||
return (int) $deptList['id'];
|
||||
}
|
||||
if (!empty($adminInfo['dept_id'])) {
|
||||
return (int) $adminInfo['dept_id'];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function isSuperAdmin(?array $adminInfo): bool
|
||||
{
|
||||
return !empty($adminInfo['id']) && (int) $adminInfo['id'] <= 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析配置类接口的渠道 ID(请求参数 dept_id)
|
||||
* 超管:0 或空=默认模板(null);>0=指定渠道
|
||||
* 普通管理员:固定本渠道
|
||||
*/
|
||||
public static function resolveConfigDeptId(?array $adminInfo, $requestDeptId): int
|
||||
{
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId !== null) {
|
||||
return $scopeDeptId > 0 ? $scopeDeptId : self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
if ($requestDeptId === null || $requestDeptId === '') {
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
$id = (int) $requestDeptId;
|
||||
if ($id === self::DEFAULT_TEMPLATE_DEPT) {
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
return $id > 0 ? $id : self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
|
||||
public static function isTemplateDeptId($deptId): bool
|
||||
{
|
||||
return $deptId === null || $deptId === '' || (int) $deptId === self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同渠道下可访问的管理员 ID
|
||||
*
|
||||
* @param array|null $adminInfo 当前登录管理员信息(含 id、deptList)
|
||||
* @return int[]|null null=不限制(超级管理员),否则为可访问的 admin_id 数组
|
||||
* @return int[]|null null=不限制
|
||||
*/
|
||||
public static function getAllowedAdminIds(?array $adminInfo): ?array
|
||||
{
|
||||
@@ -28,33 +82,285 @@ class AdminScopeHelper
|
||||
if ($adminId <= 1) {
|
||||
return null;
|
||||
}
|
||||
$deptList = $adminInfo['deptList'] ?? [];
|
||||
if (empty($deptList) || !isset($deptList['id'])) {
|
||||
$deptId = self::getDeptId($adminInfo);
|
||||
if ($deptId === null) {
|
||||
return null;
|
||||
}
|
||||
if ($deptId <= 0) {
|
||||
return [$adminId];
|
||||
}
|
||||
$query = SystemUser::field('id');
|
||||
$query->auth($deptList);
|
||||
$ids = $query->column('id');
|
||||
return array_map('intval', $ids ?: []);
|
||||
$ids = SystemUser::where('dept_id', $deptId)->column('id');
|
||||
return array_map('intval', $ids ?: [$adminId]);
|
||||
}
|
||||
|
||||
public static function fillDeptId(array &$data, ?array $adminInfo, $requestDeptId = null): void
|
||||
{
|
||||
if (isset($data['dept_id']) && $data['dept_id'] !== '' && $data['dept_id'] !== null) {
|
||||
return;
|
||||
}
|
||||
$deptId = self::resolveConfigDeptId($adminInfo, $requestDeptId ?? ($data['filter_dept_id'] ?? null));
|
||||
if ($deptId > 0) {
|
||||
$data['dept_id'] = $deptId;
|
||||
} elseif (!isset($data['dept_id']) || self::isTemplateDeptId($data['dept_id'])) {
|
||||
$data['dept_id'] = self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对查询应用 admin_id 范围过滤
|
||||
*
|
||||
* @param object $query ThinkORM 查询对象
|
||||
* @param array|null $adminInfo 当前登录管理员信息
|
||||
* @return void
|
||||
* 业务数据新增:按渠道写入 dept_id(玩家、记录等)
|
||||
* 优先级:已有 dept_id → 所属管理员 admin_id → 请求渠道 → 当前登录人渠道
|
||||
*/
|
||||
public static function applyAdminScope($query, ?array $adminInfo): void
|
||||
public static function fillBusinessDeptId(array &$data, ?array $adminInfo, $requestDeptId = null): void
|
||||
{
|
||||
$allowedIds = self::getAllowedAdminIds($adminInfo);
|
||||
if ($allowedIds === null) {
|
||||
if (isset($data['dept_id']) && $data['dept_id'] !== '' && $data['dept_id'] !== null) {
|
||||
$data['dept_id'] = (int) $data['dept_id'];
|
||||
if ($data['dept_id'] > 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data['player_id'])) {
|
||||
$playerDeptId = self::resolveDeptIdByPlayerId($data['player_id']);
|
||||
if ($playerDeptId !== null && $playerDeptId > 0) {
|
||||
$data['dept_id'] = $playerDeptId;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($data['admin_id'])) {
|
||||
$ownerDeptId = self::resolveDeptIdByAdminId($data['admin_id']);
|
||||
if ($ownerDeptId !== null && $ownerDeptId > 0) {
|
||||
$data['dept_id'] = $ownerDeptId;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$deptId = self::resolveBusinessDeptId($adminInfo, $requestDeptId);
|
||||
if ($deptId !== null && $deptId > 0) {
|
||||
$data['dept_id'] = $deptId;
|
||||
return;
|
||||
}
|
||||
if (empty($allowedIds)) {
|
||||
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId !== null && $scopeDeptId > 0) {
|
||||
$data['dept_id'] = $scopeDeptId;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务新增:解析请求渠道并填充 dept_id,缺失时抛错
|
||||
*/
|
||||
public static function prepareBusinessSaveData(
|
||||
array &$data,
|
||||
?array $adminInfo,
|
||||
$inputDeptId = null,
|
||||
array $body = []
|
||||
): void {
|
||||
$requestDeptId = self::pickRequestDeptId($inputDeptId, $body);
|
||||
self::fillBusinessDeptId($data, $adminInfo, $requestDeptId);
|
||||
self::assertBusinessDeptId($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置新增:解析请求渠道并填充 dept_id
|
||||
*/
|
||||
public static function prepareConfigSaveData(
|
||||
array &$data,
|
||||
?array $adminInfo,
|
||||
$inputDeptId = null,
|
||||
array $body = []
|
||||
): void {
|
||||
$requestDeptId = self::pickRequestDeptId($inputDeptId, $body);
|
||||
self::fillDeptId($data, $adminInfo, $requestDeptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务表 dept_id 必须 > 0(非默认模板)
|
||||
*/
|
||||
public static function assertBusinessDeptId(array $data): void
|
||||
{
|
||||
if (!isset($data['dept_id']) || $data['dept_id'] === '' || $data['dept_id'] === null) {
|
||||
throw new ApiException('CHANNEL_DEPT_ID_REQUIRED');
|
||||
}
|
||||
if ((int) $data['dept_id'] <= 0) {
|
||||
throw new ApiException('INVALID_CHANNEL_DEPT_ID');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据玩家 ID 解析所属渠道
|
||||
*/
|
||||
public static function resolveDeptIdByPlayerId($playerId): ?int
|
||||
{
|
||||
if ($playerId === null || $playerId === '') {
|
||||
return null;
|
||||
}
|
||||
$player = DicePlayer::field('dept_id,admin_id')->find($playerId);
|
||||
if (!$player || $player->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
$deptId = $player->dept_id ?? null;
|
||||
if ($deptId !== null && $deptId !== '' && (int) $deptId > 0) {
|
||||
return (int) $deptId;
|
||||
}
|
||||
if (!empty($player->admin_id)) {
|
||||
return self::resolveDeptIdByAdminId($player->admin_id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据后台管理员 ID 解析所属渠道
|
||||
*/
|
||||
public static function resolveDeptIdByAdminId($adminId): ?int
|
||||
{
|
||||
if ($adminId === null || $adminId === '') {
|
||||
return null;
|
||||
}
|
||||
$admin = SystemUser::find($adminId);
|
||||
if (!$admin || $admin->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
$deptId = $admin->dept_id ?? null;
|
||||
if ($deptId !== null && $deptId !== '' && (int) $deptId > 0) {
|
||||
return (int) $deptId;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 规范化记录上的 dept_id(null 视为默认模板 0)
|
||||
*/
|
||||
public static function normalizeRecordDeptId($recordDeptId): int
|
||||
{
|
||||
if ($recordDeptId === null || $recordDeptId === '') {
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
return (int) $recordDeptId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 玩家端读取游戏配置时使用的渠道 ID(玩家 dept_id 优先,否则按所属管理员)
|
||||
*/
|
||||
public static function resolvePlayerConfigDeptId($player): int
|
||||
{
|
||||
$deptId = null;
|
||||
$adminId = null;
|
||||
if (is_array($player)) {
|
||||
$deptId = $player['dept_id'] ?? null;
|
||||
$adminId = $player['admin_id'] ?? null;
|
||||
} elseif (is_object($player)) {
|
||||
$deptId = $player->dept_id ?? null;
|
||||
$adminId = $player->admin_id ?? null;
|
||||
}
|
||||
if ($deptId !== null && $deptId !== '' && (int) $deptId > 0) {
|
||||
return (int) $deptId;
|
||||
}
|
||||
if ($adminId !== null && $adminId !== '') {
|
||||
$fromAdmin = self::resolveDeptIdByAdminId($adminId);
|
||||
if ($fromAdmin !== null && $fromAdmin > 0) {
|
||||
return $fromAdmin;
|
||||
}
|
||||
}
|
||||
return self::DEFAULT_TEMPLATE_DEPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从请求参数或请求体中解析 dept_id(兼容 PUT JSON 仅出现在 body 的情况)
|
||||
*/
|
||||
public static function pickRequestDeptId($inputDeptId, array $body = [])
|
||||
{
|
||||
if ($inputDeptId !== null && $inputDeptId !== '') {
|
||||
return $inputDeptId;
|
||||
}
|
||||
if (isset($body['dept_id']) && $body['dept_id'] !== '' && $body['dept_id'] !== null) {
|
||||
return $body['dept_id'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function canAccessDept(?array $adminInfo, $recordDeptId, $requestDeptId = null): bool
|
||||
{
|
||||
$recordDeptId = self::normalizeRecordDeptId($recordDeptId);
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId === null) {
|
||||
if ($requestDeptId === null || $requestDeptId === '') {
|
||||
return true;
|
||||
}
|
||||
$target = self::resolveConfigDeptId($adminInfo, $requestDeptId);
|
||||
if (self::isTemplateDeptId($target)) {
|
||||
return self::isTemplateDeptId($recordDeptId);
|
||||
}
|
||||
return $recordDeptId === $target;
|
||||
}
|
||||
if ($recordDeptId === self::DEFAULT_TEMPLATE_DEPT && $scopeDeptId > 0) {
|
||||
return false;
|
||||
}
|
||||
return $recordDeptId === $scopeDeptId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务页渠道 ID:超管通过请求 dept_id 筛选;未传或 <=0 时不限制
|
||||
*/
|
||||
public static function resolveBusinessDeptId(?array $adminInfo, $requestDeptId): ?int
|
||||
{
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
if ($scopeDeptId !== null) {
|
||||
return $scopeDeptId > 0 ? $scopeDeptId : null;
|
||||
}
|
||||
if ($requestDeptId === null || $requestDeptId === '') {
|
||||
return null;
|
||||
}
|
||||
$id = (int) $requestDeptId;
|
||||
return $id > 0 ? $id : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 业务数据列表(玩家、记录、工作台等)
|
||||
*/
|
||||
public static function applyAdminScope($query, ?array $adminInfo, $requestDeptId = null): void
|
||||
{
|
||||
if (self::getDeptId($adminInfo) === null) {
|
||||
$target = self::resolveBusinessDeptId($adminInfo, $requestDeptId);
|
||||
if ($target !== null && $target > 0) {
|
||||
$query->where('dept_id', $target);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$deptId = self::getDeptId($adminInfo);
|
||||
if ($deptId <= 0) {
|
||||
$query->whereRaw('1=0');
|
||||
return;
|
||||
}
|
||||
$query->whereIn('admin_id', $allowedIds);
|
||||
$query->where('dept_id', $deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置类列表:超管按所选渠道/默认模板筛选
|
||||
*/
|
||||
public static function applyConfigScope($query, ?array $adminInfo, $requestDeptId = null, string $deptColumn = 'dept_id'): void
|
||||
{
|
||||
$targetDeptId = self::resolveConfigDeptId($adminInfo, $requestDeptId);
|
||||
$scopeDeptId = self::getDeptId($adminInfo);
|
||||
|
||||
if ($scopeDeptId !== null && !self::isSuperAdmin($adminInfo)) {
|
||||
if ($scopeDeptId <= 0) {
|
||||
$query->whereRaw('1=0');
|
||||
return;
|
||||
}
|
||||
$query->where($deptColumn, $scopeDeptId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self::isTemplateDeptId($targetDeptId)) {
|
||||
$templateId = self::DEFAULT_TEMPLATE_DEPT;
|
||||
$query->where(function ($q) use ($templateId, $deptColumn) {
|
||||
$q->where($deptColumn, $templateId)->whereOr($deptColumn, null);
|
||||
});
|
||||
return;
|
||||
}
|
||||
$query->where($deptColumn, $targetDeptId);
|
||||
}
|
||||
}
|
||||
|
||||
139
server/app/dice/helper/ConfigScopeEditHelper.php
Normal file
139
server/app/dice/helper/ConfigScopeEditHelper.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\dice\helper;
|
||||
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
|
||||
/**
|
||||
* 配置类数据按渠道隔离的更新(防止 find(id) 误更新其他渠道同业务主键行)
|
||||
*/
|
||||
class ConfigScopeEditHelper
|
||||
{
|
||||
/**
|
||||
* 在查询上附加渠道条件
|
||||
*/
|
||||
public static function applyDeptIdWhere($query, int $deptId, string $column = 'dept_id'): void
|
||||
{
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$query->where(function ($q) use ($deptId, $column) {
|
||||
$q->where($column, $deptId);
|
||||
if (method_exists($q, 'orWhereNull')) {
|
||||
$q->orWhereNull($column);
|
||||
} else {
|
||||
$q->whereOr($column, null);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
$query->where($column, $deptId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按主键 + 渠道更新配置行
|
||||
*
|
||||
* @param Model $model 模型实例(用于取表名、主键)
|
||||
* @param mixed $primaryKeyValue 列表/表单中的主键值
|
||||
* @param int $deptId 渠道 ID(0=默认模板)
|
||||
* @param array $data 更新字段
|
||||
* @param array $forbidden 禁止写入的字段名
|
||||
*/
|
||||
/**
|
||||
* 按主键更新(主键全局唯一表如 dice_lottery_pool_config)
|
||||
* 以库中记录的 dept_id 为准,避免请求未带 dept_id 时误按默认模板 0 查找失败
|
||||
*/
|
||||
public static function updateByPkAndDept(
|
||||
object $model,
|
||||
$primaryKeyValue,
|
||||
int $requestDeptId,
|
||||
array $data,
|
||||
array $forbidden = ['id', 'dept_id', 'create_time', 'update_time', 'delete_time', 'row_id'],
|
||||
?array $adminInfo = null,
|
||||
$rawRequestDeptId = null
|
||||
): bool {
|
||||
foreach ($forbidden as $field) {
|
||||
unset($data[$field]);
|
||||
}
|
||||
if ($data === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$pk = self::resolvePk($model);
|
||||
$record = $model->where($pk, $primaryKeyValue)->find();
|
||||
if ($record === null) {
|
||||
throw new ApiException('data not found');
|
||||
}
|
||||
|
||||
$recordDeptId = AdminScopeHelper::normalizeRecordDeptId(
|
||||
is_array($record) ? ($record['dept_id'] ?? null) : ($record->dept_id ?? null)
|
||||
);
|
||||
|
||||
if ($adminInfo !== null && ! AdminScopeHelper::canAccessDept($adminInfo, $recordDeptId, $rawRequestDeptId)) {
|
||||
throw new ApiException('no permission to update this record');
|
||||
}
|
||||
|
||||
if ($rawRequestDeptId !== null && $rawRequestDeptId !== '') {
|
||||
$targetDeptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $rawRequestDeptId);
|
||||
if ($targetDeptId !== $recordDeptId) {
|
||||
throw new ApiException('record does not belong to selected channel');
|
||||
}
|
||||
}
|
||||
|
||||
$query = $model->where($pk, $primaryKeyValue);
|
||||
self::applyDeptIdWhere($query, $recordDeptId);
|
||||
|
||||
$affected = $query->update($data);
|
||||
return $affected !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* dice_reward_config / dice_config:业务 id(0~25 等)+ 渠道更新
|
||||
*/
|
||||
public static function updateByBusinessIdAndDept(
|
||||
object $model,
|
||||
int $businessId,
|
||||
int $deptId,
|
||||
array $data,
|
||||
array $forbidden = ['id', 'dept_id', 'create_time', 'update_time', 'delete_time', 'row_id']
|
||||
): bool {
|
||||
foreach ($forbidden as $field) {
|
||||
unset($data[$field]);
|
||||
}
|
||||
if ($data === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$query = $model->where('id', $businessId);
|
||||
self::applyDeptIdWhere($query, $deptId);
|
||||
|
||||
$record = (clone $query)->find();
|
||||
if ($record === null) {
|
||||
throw new ApiException('config id=' . $businessId . ' not found for current channel');
|
||||
}
|
||||
|
||||
$affected = $query->update($data);
|
||||
return $affected !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表/读取:按主键 + 渠道取单条(避免 find(pk) 命中其他渠道)
|
||||
*/
|
||||
private static function resolvePk(object $model): string
|
||||
{
|
||||
if (method_exists($model, 'getPk')) {
|
||||
return (string) $model->getPk();
|
||||
}
|
||||
if (method_exists($model, 'getKeyName')) {
|
||||
return (string) $model->getKeyName();
|
||||
}
|
||||
return 'id';
|
||||
}
|
||||
|
||||
public static function findByPkAndDept(object $model, $primaryKeyValue, int $deptId)
|
||||
{
|
||||
$pk = self::resolvePk($model);
|
||||
$query = $model->where($pk, $primaryKeyValue);
|
||||
self::applyDeptIdWhere($query, $deptId);
|
||||
return $query->find();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user