相关记录表admin_id关联当前管理员id

This commit is contained in:
2026-03-10 12:30:56 +08:00
parent b1efeb8b31
commit fdd8f6dffa
18 changed files with 290 additions and 26 deletions

View File

@@ -137,9 +137,16 @@ class GameController extends BaseController
]);
$timeoutRecord = null;
$timeout_message = '';
$adminId = null;
try {
$timeoutPlayer = DicePlayer::find($userId);
$adminId = ($timeoutPlayer && ($timeoutPlayer->admin_id ?? null)) ? (int) $timeoutPlayer->admin_id : null;
} catch (\Throwable $_) {
}
try {
$timeoutRecord = DicePlayRecord::create([
'player_id' => $userId,
'admin_id' => $adminId,
'lottery_config_id' => 0,
'lottery_type' => 0,
'is_win' => 0,

View File

@@ -6,6 +6,7 @@ namespace app\api\controller\v1;
use app\api\logic\UserLogic;
use app\api\util\ReturnCode;
use app\dice\model\player\DicePlayer;
use plugin\saiadmin\app\model\system\SystemUser;
use app\dice\model\play_record\DicePlayRecord;
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
use app\dice\model\player_ticket_record\DicePlayerTicketRecord;
@@ -40,9 +41,18 @@ class GameController extends BaseController
$time = (string) time();
}
$adminId = null;
$agentId = trim((string) ($request->agent_id ?? ''));
if ($agentId !== '') {
$systemUser = SystemUser::where('agent_id', $agentId)->find();
if ($systemUser) {
$adminId = (int) $systemUser->id;
}
}
try {
$logic = new UserLogic();
$result = $logic->loginByUsername($username, $password, 'chs', 0.0, $time);
$result = $logic->loginByUsername($username, $password, 'chs', 0.0, $time, $adminId);
} catch (\plugin\saiadmin\exception\ApiException $e) {
return $this->fail($e->getMessage(), ReturnCode::PARAMS_ERROR);
}
@@ -264,8 +274,10 @@ class GameController extends BaseController
$player->coin = $walletAfter;
$player->save();
$adminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
$record = DicePlayerWalletRecord::create([
'player_id' => (int) $player->id,
'admin_id' => $adminId,
'coin' => $coinVal,
'type' => $type,
'wallet_before' => $walletBefore,

View File

@@ -69,10 +69,12 @@ class GameLogic
UserCache::setUser($playerId, $updatedUserArr);
$adminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
try {
Db::transaction(function () use (
$player,
$playerId,
$adminId,
$cost,
$coinBefore,
$coinAfter,
@@ -91,6 +93,7 @@ class GameLogic
DicePlayerWalletRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'coin' => -$cost,
'type' => self::WALLET_TYPE_BUY_DRAW,
'wallet_before' => $coinBefore,
@@ -103,6 +106,7 @@ class GameLogic
DicePlayerTicketRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'use_coins' => $cost,
'total_ticket_count' => $addTotal,
'paid_ticket_count' => $addPaid,

View File

@@ -160,9 +160,11 @@ class PlayStartLogic
$rewardId = $chosenId;
$configName = (string) ($config->name ?? '');
$isTierT5 = (string) ($chosen['tier'] ?? '') === 'T5';
$adminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
try {
Db::transaction(function () use (
$playerId,
$adminId,
$configId,
$rewardId,
$configName,
@@ -181,6 +183,7 @@ class PlayStartLogic
) {
$record = DicePlayRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'lottery_config_id' => $configId,
'lottery_type' => $ticketType,
'is_win' => $isWin,
@@ -218,9 +221,10 @@ class PlayStartLogic
$p->total_ticket_count = (int) $p->total_ticket_count + 1;
DicePlayerTicketRecord::create([
'player_id' => $playerId,
'player_id' => $playerId,
'admin_id' => $adminId,
'free_ticket_count' => 1,
'remark' => '中奖结果为T5',
'remark' => '中奖结果为T5',
]);
}
@@ -236,6 +240,7 @@ class PlayStartLogic
DicePlayerWalletRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId,
'coin' => $winCoin,
'type' => self::WALLET_TYPE_DRAW,
'wallet_before' => $coinBefore,
@@ -248,6 +253,7 @@ class PlayStartLogic
try {
$record = DicePlayRecord::create([
'player_id' => $playerId,
'admin_id' => $adminId ?? null,
'lottery_config_id' => $configId ?? 0,
'lottery_type' => $ticketType,
'is_win' => 0,

View File

@@ -45,8 +45,10 @@ class UserLogic
* 登录JSONusername, password, lang, coin, time
* 存在则校验密码并更新 coin累加不存在则创建用户并写入 coin。
* 将会话写入 Redis返回 token 与前端连接地址。
*
* @param int|null $adminId 创建新用户时关联的后台管理员IDsa_system_user.id可选
*/
public function loginByUsername(string $username, string $password, string $lang, float $coin, string $time): array
public function loginByUsername(string $username, string $password, string $lang, float $coin, string $time, ?int $adminId = null): array
{
$username = trim($username);
if ($username === '') {
@@ -72,6 +74,9 @@ class UserLogic
$player->password = $this->hashPassword($password);
$player->status = self::STATUS_NORMAL;
$player->coin = $coin;
if ($adminId !== null && $adminId > 0) {
$player->admin_id = $adminId;
}
$player->save();
}

View File

@@ -6,6 +6,7 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\play_record;
use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\play_record\DicePlayRecordLogic;
use app\dice\validate\play_record\DicePlayRecordValidate;
@@ -53,6 +54,7 @@ class DicePlayRecordController extends BaseController
['direction', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with([
'dicePlayer',
'diceRewardConfig',
@@ -68,7 +70,9 @@ class DicePlayRecordController extends BaseController
#[Permission('玩家抽奖记录列表', 'dice:play_record:index:index')]
public function getPlayerOptions(Request $request): Response
{
$list = DicePlayer::field('id,username')->select();
$query = DicePlayer::field('id,username');
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$list = $query->select();
$data = $list->map(function ($item) {
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
})->toArray();
@@ -115,12 +119,15 @@ class DicePlayRecordController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该记录');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**

View File

@@ -6,7 +6,9 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\player;
use app\dice\helper\AdminScopeHelper;
use app\dice\model\lottery_config\DiceLotteryConfig;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player\DicePlayerLogic;
use app\dice\validate\player\DicePlayerValidate;
@@ -44,6 +46,35 @@ class DicePlayerController extends BaseController
return $this->success($data);
}
/**
* 获取后台管理员选项SystemUser.id、username、realname供 admin_id 下拉使用
* 根据当前登录用户权限过滤(超级管理员可见全部,普通管理员按部门)
* @param Request $request
* @return Response 返回 [ ['id' => int, 'username' => string, 'realname' => string], ... ]
*/
#[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);
}
}
$list = $query->select();
$data = $list->map(function ($item) {
$label = trim((string) ($item['realname'] ?? '')) ?: (string) ($item['username'] ?? '');
return [
'id' => (int) $item['id'],
'username' => (string) ($item['username'] ?? ''),
'realname' => (string) ($item['realname'] ?? ''),
'label' => $label ?: (string) $item['id'],
];
})->toArray();
return $this->success($data);
}
/**
* 数据列表
* @param Request $request
@@ -61,6 +92,7 @@ class DicePlayerController extends BaseController
['lottery_config_id', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with(['diceLotteryConfig']);
$data = $this->logic->getList($query);
return $this->success($data);
@@ -76,12 +108,15 @@ class DicePlayerController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该玩家');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**
@@ -94,6 +129,10 @@ class DicePlayerController extends BaseController
{
$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) {
return $this->success('添加成功');
@@ -112,6 +151,13 @@ class DicePlayerController extends BaseController
{
$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)) {
return $this->fail('无权限修改该玩家');
}
}
$result = $this->logic->edit($data['id'], $data);
if ($result) {
return $this->success('修改成功');
@@ -136,6 +182,13 @@ class DicePlayerController extends BaseController
if ($status === null || $status === '') {
return $this->fail('缺少 status');
}
$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)) {
return $this->fail('无权限修改该玩家');
}
}
$this->logic->edit($id, ['status' => (int) $status]);
return $this->success('修改成功');
}
@@ -152,6 +205,22 @@ class DicePlayerController extends BaseController
if (empty($ids)) {
return $this->fail('请选择要删除的数据');
}
$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');
$validIds = [];
foreach ($ids as $id) {
$adminId = (int) ($models[$id] ?? 0);
if (in_array($adminId, $allowedIds, true)) {
$validIds[] = $id;
}
}
$ids = $validIds;
if (empty($ids)) {
return $this->fail('无权限删除所选玩家');
}
}
$result = $this->logic->destroy($ids);
if ($result) {
return $this->success('删除成功');

View File

@@ -6,6 +6,7 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\player_ticket_record;
use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player_ticket_record\DicePlayerTicketRecordLogic;
use app\dice\validate\player_ticket_record\DicePlayerTicketRecordValidate;
@@ -51,6 +52,7 @@ class DicePlayerTicketRecordController extends BaseController
['create_time_max', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with([
'dicePlayer',
]);
@@ -66,7 +68,9 @@ class DicePlayerTicketRecordController extends BaseController
#[Permission('抽奖券获取记录列表', 'dice:player_ticket_record:index:index')]
public function getPlayerOptions(Request $request): Response
{
$list = DicePlayer::field('id,username')->select();
$query = DicePlayer::field('id,username');
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$list = $query->select();
$data = $list->map(function ($item) {
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
})->toArray();
@@ -83,12 +87,15 @@ class DicePlayerTicketRecordController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该记录');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**

View File

@@ -6,6 +6,7 @@
// +----------------------------------------------------------------------
namespace app\dice\controller\player_wallet_record;
use app\dice\helper\AdminScopeHelper;
use plugin\saiadmin\basic\BaseController;
use app\dice\logic\player_wallet_record\DicePlayerWalletRecordLogic;
use app\dice\validate\player_wallet_record\DicePlayerWalletRecordValidate;
@@ -47,6 +48,7 @@ class DicePlayerWalletRecordController extends BaseController
['create_time_max', ''],
]);
$query = $this->logic->search($where);
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$query->with([
'dicePlayer',
'operator',
@@ -63,7 +65,9 @@ class DicePlayerWalletRecordController extends BaseController
#[Permission('玩家钱包流水列表', 'dice:player_wallet_record:index:index')]
public function getPlayerOptions(Request $request): Response
{
$list = DicePlayer::field('id,username')->select();
$query = DicePlayer::field('id,username');
AdminScopeHelper::applyAdminScope($query, $this->adminInfo ?? null);
$list = $query->select();
$data = $list->map(function ($item) {
return ['id' => $item['id'], 'username' => $item['username'] ?? ''];
})->toArray();
@@ -83,10 +87,14 @@ class DicePlayerWalletRecordController extends BaseController
if ($playerId === null || $playerId === '') {
return $this->fail('缺少 player_id');
}
$player = DicePlayer::field('coin')->where('id', $playerId)->find();
$player = DicePlayer::field('coin,admin_id')->where('id', $playerId)->find();
if (!$player) {
return $this->fail('玩家不存在');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限操作该玩家');
}
return $this->success(['wallet_before' => (float) $player['coin']]);
}
@@ -100,12 +108,15 @@ class DicePlayerWalletRecordController extends BaseController
{
$id = $request->input('id', '');
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
} else {
if (!$model) {
return $this->fail('未查找到信息');
}
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($model->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限查看该记录');
}
$data = is_array($model) ? $model : $model->toArray();
return $this->success($data);
}
/**
@@ -155,6 +166,14 @@ class DicePlayerWalletRecordController extends BaseController
return $this->fail('请先登录');
}
$player = DicePlayer::field('admin_id')->where('id', $playerId)->find();
if ($player) {
$allowedIds = AdminScopeHelper::getAllowedAdminIds($this->adminInfo ?? null);
if ($allowedIds !== null && !in_array((int) ($player->admin_id ?? 0), $allowedIds, true)) {
return $this->fail('无权限操作该玩家');
}
}
try {
$this->logic->adminOperate($data, $adminId);
return $this->success('操作成功');

View File

@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace app\dice\helper;
use plugin\saiadmin\app\model\system\SystemUser;
/**
* 管理员数据范围辅助类
* 用于获取当前管理员及其部门下属管理员可访问的数据范围
*/
class AdminScopeHelper
{
/**
* 获取当前管理员可访问的 admin_id 列表
* 超级管理员(id=1) 返回 null 表示不限制
* 普通管理员返回其本人及部门下属管理员的 id 列表
*
* @param array|null $adminInfo 当前登录管理员信息(含 id、deptList
* @return int[]|null null=不限制(超级管理员),否则为可访问的 admin_id 数组
*/
public static function getAllowedAdminIds(?array $adminInfo): ?array
{
if (empty($adminInfo) || !isset($adminInfo['id'])) {
return [];
}
$adminId = (int) $adminInfo['id'];
if ($adminId <= 1) {
return null;
}
$deptList = $adminInfo['deptList'] ?? [];
if (empty($deptList) || !isset($deptList['id'])) {
return [$adminId];
}
$query = SystemUser::field('id');
$query->auth($deptList);
$ids = $query->column('id');
return array_map('intval', $ids ?: []);
}
/**
* 对查询应用 admin_id 范围过滤
*
* @param object $query ThinkORM 查询对象
* @param array|null $adminInfo 当前登录管理员信息
* @return void
*/
public static function applyAdminScope($query, ?array $adminInfo): void
{
$allowedIds = self::getAllowedAdminIds($adminInfo);
if ($allowedIds === null) {
return;
}
if (empty($allowedIds)) {
$query->whereRaw('1=0');
return;
}
$query->whereIn('admin_id', $allowedIds);
}
}

View File

@@ -73,8 +73,10 @@ class DicePlayerWalletRecordLogic extends BaseLogic
DicePlayer::where('id', $playerId)->update(['coin' => $walletAfter]);
$playerAdminId = ($player->admin_id ?? null) ? (int) $player->admin_id : null;
$record = [
'player_id' => $playerId,
'admin_id' => $playerAdminId,
'coin' => $type === 3 ? $coin : -$coin,
'type' => $type,
'wallet_before' => $walletBefore,

View File

@@ -19,6 +19,7 @@ use think\model\relation\BelongsTo;
*
* @property $id ID
* @property $player_id 玩家id
* @property $admin_id 关联玩家所属管理员IDDicePlayer.admin_id
* @property $lottery_config_id 彩金池配置
* @property $lottery_type 抽奖类型
* @property $is_win 是否中大奖:豹子号[1,1,1,1,1]~[6,6,6,6,6]为1否则0

View File

@@ -22,6 +22,7 @@ use app\dice\model\lottery_config\DiceLotteryConfig;
* @property $password 密码
* @property $status 状态
* @property $coin 平台币
* @property $admin_id 创建该玩家的后台管理员ID关联 sa_system_user.id
* @property $lottery_config_id 彩金池配置ID0或null时使用自定义权重*_weight
* @property $t1_weight T1池权重
* @property $t2_weight T2池权重

View File

@@ -17,6 +17,7 @@ use think\model\relation\BelongsTo;
*
* @property $id ID
* @property $player_id 玩家id
* @property $admin_id 关联玩家所属管理员IDDicePlayer.admin_id
* @property $use_coins 消耗硬币
* @property $total_ticket_count 总抽奖次数
* @property $paid_ticket_count 购买抽奖次数

View File

@@ -18,6 +18,7 @@ use think\model\relation\BelongsTo;
*
* @property $id ID
* @property $player_id 用户id
* @property $admin_id 关联玩家所属管理员IDDicePlayer.admin_id
* @property $coin 平台币变化
* @property $type 类型:0=充值 1=提现 2=购买抽奖次数
* @property $wallet_before 钱包操作前

View File

@@ -87,6 +87,7 @@ Route::group('/core', function () {
fastRoute('dice/player/DicePlayer', \app\dice\controller\player\DicePlayerController::class);
Route::put('/dice/player/DicePlayer/updateStatus', [\app\dice\controller\player\DicePlayerController::class, 'updateStatus']);
Route::get('/dice/player/DicePlayer/getLotteryConfigOptions', [\app\dice\controller\player\DicePlayerController::class, 'getLotteryConfigOptions']);
Route::get('/dice/player/DicePlayer/getSystemUserOptions', [\app\dice\controller\player\DicePlayerController::class, 'getSystemUserOptions']);
fastRoute('dice/play_record/DicePlayRecord', \app\dice\controller\play_record\DicePlayRecordController::class);
Route::get('/dice/play_record/DicePlayRecord/getPlayerOptions', [\app\dice\controller\play_record\DicePlayRecordController::class, 'getPlayerOptions']);
Route::get('/dice/play_record/DicePlayRecord/getLotteryConfigOptions', [\app\dice\controller\play_record\DicePlayRecordController::class, 'getLotteryConfigOptions']);