1.将部门修改为渠道,并且所有dice_表关联渠道表

2.将所有配置表,记录表设置关联渠道
3.优化后台页面设置
This commit is contained in:
2026-05-19 09:49:02 +08:00
parent 085454fb78
commit dd264b1e97
143 changed files with 4741 additions and 1254 deletions

View File

@@ -8,6 +8,7 @@ namespace app\dice\controller\player;
use app\dice\helper\AdminScopeHelper;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use plugin\saiadmin\app\model\system\SystemDept;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player\DicePlayerLogic;
@@ -42,7 +43,13 @@ class DicePlayerController extends BaseController
#[Permission('玩家列表', 'dice:player:index:index')]
public function getLotteryConfigOptions(Request $request): Response
{
$list = DiceLotteryPoolConfig::field('id,name')->order('id', 'asc')->select();
$query = DiceLotteryPoolConfig::field('id,name')->order('id', 'asc');
$requestDeptId = AdminScopeHelper::pickRequestDeptId(
$request->input('dept_id'),
$request->all()
);
AdminScopeHelper::applyConfigScope($query, $this->adminInfo ?? null, $requestDeptId);
$list = $query->select();
$data = $list->map(function ($item) {
return ['id' => (int) $item['id'], 'name' => (string) ($item['name'] ?? '')];
})->toArray();
@@ -57,12 +64,17 @@ class DicePlayerController extends BaseController
#[Permission('玩家列表', 'dice:player:index:index')]
public function getSystemUserOptions(Request $request): Response
{
$query = SystemUser::field('id,username,realname')->where('status', 1)->order('id', 'asc');
if (isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 1) {
$deptList = $this->adminInfo['deptList'] ?? [];
if (!empty($deptList)) {
$query->auth($deptList);
$query = SystemUser::field('id,username,realname,dept_id')->where('status', 1)->order('id', 'asc');
$requestDeptId = AdminScopeHelper::pickRequestDeptId(
$request->input('dept_id'),
$request->all()
);
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null) {
if ($allowedIds === []) {
return $this->success([]);
}
$query->whereIn('id', $allowedIds);
}
$list = $query->select();
$data = $list->map(function ($item) {
@@ -71,12 +83,89 @@ class DicePlayerController extends BaseController
'id' => (int) $item['id'],
'username' => (string) ($item['username'] ?? ''),
'realname' => (string) ($item['realname'] ?? ''),
'dept_id' => isset($item['dept_id']) ? (int) $item['dept_id'] : null,
'label' => $label ?: (string) $item['id'],
];
})->toArray();
return $this->success($data);
}
/**
* 超管:按渠道树状展示全部管理员;非超管:同 getSystemUserOptions 扁平列表
*/
#[Permission('玩家列表', 'dice:player:index:index')]
public function getSystemUserTreeOptions(Request $request): Response
{
if (!AdminScopeHelper::isSuperAdmin($this->adminInfo ?? null)) {
return $this->getSystemUserOptions($request);
}
$users = SystemUser::field('id,username,realname,dept_id')
->where('status', 1)
->order('id', 'asc')
->select()
->toArray();
$depts = SystemDept::field('id,name')
->where('status', 1)
->order('id', 'asc')
->select()
->toArray();
$deptNameMap = [];
foreach ($depts as $dept) {
$deptNameMap[(int) $dept['id']] = (string) ($dept['name'] ?? $dept['id']);
}
$grouped = [];
$unassigned = [];
foreach ($users as $user) {
$item = [
'id' => (int) $user['id'],
'username' => (string) ($user['username'] ?? ''),
'realname' => (string) ($user['realname'] ?? ''),
'dept_id' => isset($user['dept_id']) ? (int) $user['dept_id'] : null,
];
$label = trim($item['realname']) ?: $item['username'];
$item['label'] = $label ?: (string) $item['id'];
$deptId = $item['dept_id'] ?? 0;
if ($deptId > 0 && isset($deptNameMap[$deptId])) {
if (!isset($grouped[$deptId])) {
$grouped[$deptId] = [];
}
$grouped[$deptId][] = $item;
} else {
$unassigned[] = $item;
}
}
$tree = [];
foreach ($depts as $dept) {
$deptId = (int) $dept['id'];
$children = $grouped[$deptId] ?? [];
if ($children === []) {
continue;
}
$tree[] = [
'id' => 'dept_' . $deptId,
'label' => (string) ($dept['name'] ?? $deptId),
'disabled' => true,
'children' => $children,
];
}
if ($unassigned !== []) {
$tree[] = [
'id' => 'dept_unassigned',
'label' => '__unassigned__',
'disabled' => true,
'children' => $unassigned,
];
}
return $this->success($tree);
}
/**
* 数据列表
* @param Request $request
@@ -94,7 +183,7 @@ class DicePlayerController extends BaseController
['lottery_config_id', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null, $request->input('dept_id'));
$query->with(['diceLotteryPoolConfig']);
$data = $this->logic->getList($query);
return $this->success($data);
@@ -113,8 +202,7 @@ class DicePlayerController extends BaseController
if (!$model) {
return $this->fail('not found');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null)) {
return $this->fail('no permission to view this record');
}
$data = is_array($model) ? $model : $model->toArray();
@@ -130,20 +218,32 @@ class DicePlayerController extends BaseController
public function save(Request $request): Response
{
$data = $request->post();
$this->validate('save', $data);
// 类型转化
if (empty($data['admin_id']) && isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 0) {
$data['admin_id'] = (int) $this->adminInfo['id'];
}
$result = $this->logic->add($data);
if ($result && isset($result['id'])) {
// 出于安全:删除该玩家缓存,后续 API 按需重建
UserCache::deleteUser($result['id']);
$player = DicePlayer::find($result['id']);
AdminScopeHelper::prepareBusinessSaveData(
$data,
$this->adminInfo ?? null,
$request->input('dept_id'),
$data
);
$this->validate('save', $data);
try {
$result = $this->logic->add($data);
} catch (\Throwable $e) {
if (self::isDeptUsernameDuplicateException($e)) {
return $this->fail('PLAYER_USERNAME_DEPT_UNIQUE');
}
throw $e;
}
$playerId = is_array($result) ? ($result['id'] ?? null) : $result;
if ($playerId) {
UserCache::deleteUser($playerId);
$player = DicePlayer::find($playerId);
if ($player && $player->username !== '') {
UserCache::deletePlayerByUsername($player->username);
}
return $this->success('add success');
return $this->success('ADD_SUCCESS');
}
return $this->fail('add failed');
}
@@ -157,14 +257,16 @@ class DicePlayerController extends BaseController
public function update(Request $request): Response
{
$data = $request->post();
$this->validate('update', $data);
$model = $this->logic->read($data['id'] ?? 0);
if ($model) {
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
return $this->fail('no permission to update this record');
}
if (!isset($data['dept_id']) || $data['dept_id'] === '' || $data['dept_id'] === null) {
$data['dept_id'] = $model->dept_id ?? null;
}
}
$this->validate('update', $data);
$result = $this->logic->edit($data['id'], $data);
if ($result) {
// 出于安全:删除该玩家缓存,后续 API 按需重建
@@ -173,7 +275,7 @@ class DicePlayerController extends BaseController
if ($player && $player->username !== '') {
UserCache::deletePlayerByUsername($player->username);
}
return $this->success('update success');
return $this->success('UPDATE_SUCCESS');
}
return $this->fail('update failed');
}
@@ -196,8 +298,7 @@ class DicePlayerController extends BaseController
}
$model = $this->logic->read($id);
if ($model) {
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $model->dept_id ?? null, $request->input('dept_id'))) {
return $this->fail('no permission to update this record');
}
}
@@ -227,8 +328,7 @@ class DicePlayerController extends BaseController
if (!$player) {
return $this->fail('not found');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
if (!AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $player->dept_id ?? null)) {
return $this->fail('no permission to view this record');
}
if ((int) ($player->status ?? 1) === 0) {
@@ -281,13 +381,12 @@ class DicePlayerController extends BaseController
return $this->fail('please select data to delete');
}
$ids = is_array($ids) ? $ids : explode(',', (string) $ids);
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null) {
$models = $this->logic->model->whereIn('id', $ids)->column('admin_id', 'id');
$deptId = AdminScopeHelper::getDeptId($this->adminInfo ?? null);
if ($deptId !== null) {
$models = $this->logic->model->whereIn('id', $ids)->column('dept_id', 'id');
$validIds = [];
foreach ($ids as $id) {
$adminId = (int) ($models[$id] ?? 0);
if (in_array($adminId, $allowedIds, true)) {
if (AdminScopeHelper::canAccessDept($this->adminInfo ?? null, $models[$id] ?? null)) {
$validIds[] = $id;
}
}
@@ -311,4 +410,17 @@ class DicePlayerController extends BaseController
return $this->fail('delete failed');
}
/**
* 判断是否违反 dice_player (dept_id, username) 唯一索引
*/
private static function isDeptUsernameDuplicateException(\Throwable $e): bool
{
$message = $e->getMessage();
if ($message === '') {
return false;
}
return str_contains($message, 'uk_dice_player_dept_username')
|| (str_contains($message, 'Duplicate entry') && str_contains($message, 'username'));
}
}