1.优化开奖逻辑

2.优化后台开奖派彩
3.优化接口规范
This commit is contained in:
2026-04-17 13:56:13 +08:00
parent 3cf386756b
commit bf3d50a309
50 changed files with 1036 additions and 770 deletions

View File

@@ -25,9 +25,9 @@ class Channel extends Backend
*/
protected ?object $model = null;
protected array|string $preExcludeFields = ['id', 'user_count', 'profit_amount', 'create_time', 'update_time'];
protected array|string $preExcludeFields = ['id', 'user_count', 'profit_amount', 'create_time', 'update_time', 'admin_id'];
protected array $withJoinTable = ['adminGroup', 'admin'];
protected array $withJoinTable = [];
protected string|array $quickSearchField = ['id', 'code', 'name'];
@@ -51,7 +51,7 @@ class Channel extends Backend
if ($response !== null) return $response;
$query = Db::name('channel')
->field(['id', 'name', 'admin_group_id'])
->field(['id', 'name'])
->order('id', 'asc');
if (!$this->auth->isSuperAdmin()) {
$query = $query->where('id', 'in', $this->currentChannelIds ?: [0]);
@@ -79,11 +79,16 @@ class Channel extends Backend
$tree = [];
foreach ($channels as $ch) {
$groupId = $ch['admin_group_id'] ?? null;
$channelId = (int) ($ch['id'] ?? 0);
$rootGroupIds = Db::name('admin_group')
->where('channel_id', $channelId)
->where('pid', 0)
->where('status', 1)
->column('id');
$groupIds = [];
if ($groupId !== null && $groupId !== '') {
$groupIds[] = $groupId;
foreach ($getGroupChildren($groupId) as $gid) {
foreach ($rootGroupIds as $rootId) {
$groupIds[] = $rootId;
foreach ($getGroupChildren($rootId) as $gid) {
$groupIds[] = $gid;
}
}
@@ -130,7 +135,7 @@ class Channel extends Backend
}
/**
* 添加(重写:管理员只选顶级组;admin_group_id 后端自动写入
* 添加(重写:渠道与角色组在「角色组」侧绑定 channel_id此处不再写入 admin_group_id
* @throws Throwable
*/
protected function _add(): Response
@@ -150,30 +155,10 @@ class Channel extends Backend
}
unset($data['invite_code']);
$adminId = $data['admin_id'] ?? null;
if ($adminId === null || $adminId === '') {
return $this->error(__('Parameter %s can not be empty', ['admin_id']));
}
if (array_key_exists('admin_group_id', $data)) {
unset($data['admin_group_id']);
}
$topGroupId = Db::name('admin_group_access')
->alias('aga')
->join('admin_group ag', 'aga.group_id = ag.id')
->where('aga.uid', $adminId)
->where('ag.pid', 0)
->value('ag.id');
if ($topGroupId === null || $topGroupId === '') {
return $this->error(__('Record not found'));
}
$data['admin_group_id'] = $topGroupId;
if (!$this->auth->isSuperAdmin()) {
$data['admin_id'] = $this->auth->id;
}
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$data[$this->dataLimitField] = $this->auth->id;
}
@@ -207,7 +192,7 @@ class Channel extends Backend
}
/**
* 编辑(重写:管理员只选顶级组;admin_group_id 后端自动写入
* 编辑(重写:不再维护 channel.admin_group_id
* @throws Throwable
*/
protected function _edit(): Response
@@ -246,24 +231,6 @@ class Channel extends Backend
unset($data['admin_group_id']);
}
$nextAdminId = array_key_exists('admin_id', $data) ? $data['admin_id'] : ($row['admin_id'] ?? null);
if ($nextAdminId !== null && $nextAdminId !== '') {
$topGroupId = Db::name('admin_group_access')
->alias('aga')
->join('admin_group ag', 'aga.group_id = ag.id')
->where('aga.uid', $nextAdminId)
->where('ag.pid', 0)
->value('ag.id');
if ($topGroupId === null || $topGroupId === '') {
return $this->error(__('Record not found'));
}
$data['admin_group_id'] = $topGroupId;
}
if (!$this->auth->isSuperAdmin()) {
$data['admin_id'] = $this->auth->id;
}
$result = false;
$this->model->startTrans();
try {
@@ -310,9 +277,6 @@ class Channel extends Backend
$where[] = [$alias['channel'] . '.id', 'in', $this->currentChannelIds ?: [0]];
}
$res = $this->model
->withJoin($this->withJoinTable, $this->withJoinType)
->with($this->withJoinTable)
->visible(['adminGroup' => ['name'], 'admin' => ['username']])
->alias($alias)
->where($where)
->order($order)
@@ -392,9 +356,9 @@ class Channel extends Backend
return $this->error('结算单号已存在,请稍后重试');
}
$adminId = $row['admin_id'] ?? null;
if ($adminId === null || $adminId === '' || (int) $adminId <= 0) {
return $this->error('渠道未绑定代理管理员,无法生成佣金记录');
$adminId = $this->resolveCommissionAdminIdForChannel((int) $row['id']);
if ($adminId === null || $adminId <= 0) {
return $this->error('渠道下无归属管理员账号(请为管理员设置所属渠道),无法生成佣金记录');
}
$now = time();
@@ -685,8 +649,25 @@ class Channel extends Backend
if ($admin && !empty($admin['channel_id'])) {
$ids[] = $admin['channel_id'];
}
$byAdmin = Db::name('channel')->where('admin_id', $this->auth->id)->column('id');
return array_values(array_unique(array_merge($ids, $byAdmin)));
return array_values(array_unique($ids));
}
/**
* 佣金归属管理员:取该渠道下 admin.channel_id 匹配的首个管理员(按 id 升序)。
*/
private function resolveCommissionAdminIdForChannel(int $channelId): ?int
{
if ($channelId <= 0) {
return null;
}
$aid = Db::name('admin')
->where('channel_id', $channelId)
->order('id', 'asc')
->value('id');
if ($aid === null || $aid === '') {
return null;
}
return (int) $aid;
}
private function normalizeAgentModeFields(array $data): array

View File

@@ -187,12 +187,21 @@ class Admin extends Backend
$passwd = $data['password'] ?? '';
$data = $this->excludeFields($data);
$creatorChannelId = $this->getCreatorChannelId();
$groupChannelId = $this->resolveChannelIdFromPrimaryGroup($data['group_arr'] ?? []);
if (!$this->auth->isSuperAdmin()) {
if ($creatorChannelId === null || $creatorChannelId === '') {
return $this->error(__('You have no permission'));
}
if ($groupChannelId === null || $groupChannelId === '') {
return $this->error('所选角色组未绑定渠道');
}
if ((string) $groupChannelId !== (string) $creatorChannelId) {
return $this->error('所选角色组渠道与当前账号不一致');
}
$data['channel_id'] = $creatorChannelId;
$data['parent_admin_id'] = $this->auth->id;
} else {
$data['channel_id'] = ($groupChannelId === null || $groupChannelId === '') ? null : $groupChannelId;
}
$data['invite_code'] = $this->generateUniqueInviteCode();
$requireCommissionRate = $this->requireCommissionRate($data['group_arr'] ?? []);
@@ -343,8 +352,20 @@ class Admin extends Backend
$data = $this->excludeFields($data);
unset($data['invite_code']);
$creatorChannelId = $this->getCreatorChannelId();
if (!$this->auth->isSuperAdmin() && $creatorChannelId !== null && $creatorChannelId !== '') {
$groupChannelId = $this->resolveChannelIdFromPrimaryGroup($data['group_arr'] ?? []);
if (!$this->auth->isSuperAdmin()) {
if ($creatorChannelId === null || $creatorChannelId === '') {
return $this->error(__('You have no permission'));
}
if ($groupChannelId === null || $groupChannelId === '') {
return $this->error('所选角色组未绑定渠道');
}
if ((string) $groupChannelId !== (string) $creatorChannelId) {
return $this->error('所选角色组渠道与当前账号不一致');
}
$data['channel_id'] = $creatorChannelId;
} else {
$data['channel_id'] = ($groupChannelId === null || $groupChannelId === '') ? null : $groupChannelId;
}
$requireCommissionRate = $this->requireCommissionRate($data['group_arr'] ?? []);
if ($requireCommissionRate) {
@@ -463,10 +484,24 @@ class Admin extends Backend
if ($currentAdmin && !empty($currentAdmin['channel_id'])) {
return $currentAdmin['channel_id'];
}
$channelId = Db::name('channel')
->where('admin_id', $this->auth->id)
->value('id');
return $channelId ?: null;
return null;
}
/**
* @param array<int|string> $groupIds
*/
private function resolveChannelIdFromPrimaryGroup(array $groupIds): mixed
{
if ($groupIds === []) {
return null;
}
$gid = $groupIds[0];
if ($gid === null || $gid === '') {
return null;
}
return Db::name('admin_group')->where('id', $gid)->value('channel_id');
}
private function generateUniqueInviteCode(): string

View File

@@ -86,6 +86,10 @@ class Group extends Backend
if (!$this->auth->isSuperAdmin() && $pidInt !== 0 && !in_array($pidInt, $this->manageableGroupIds, true)) {
return $this->error(__('You have no permission'));
}
$inheritRes = $this->applyChannelInheritance($data, $pidInt);
if ($inheritRes !== null) {
return $inheritRes;
}
$shouldHandleCommissionRate = true;
if ($shouldHandleCommissionRate) {
if (!$this->isValidCommissionRate($data['commission_rate'] ?? null)) {
@@ -165,6 +169,10 @@ class Group extends Backend
if (!$this->auth->isSuperAdmin() && $pidInt !== 0 && !in_array($pidInt, $this->manageableGroupIds, true)) {
return $this->error(__('You have no permission'));
}
$inheritRes = $this->applyChannelInheritance($data, $pidInt);
if ($inheritRes !== null) {
return $inheritRes;
}
$shouldHandleCommissionRate = true;
if ($shouldHandleCommissionRate) {
if (!$this->isValidCommissionRate($data['commission_rate'] ?? null)) {
@@ -204,6 +212,7 @@ class Group extends Backend
return $this->error($e->getMessage());
}
if ($result !== false) {
$this->syncDescendantChannelIds(intval((string)$row['id']));
return $this->success(__('Update successful'));
}
return $this->error(__('No rows updated'));
@@ -223,11 +232,39 @@ class Group extends Backend
}
$rowData = $row->toArray();
$rowData['rules'] = array_values($rules);
$rowData = $this->enrichChannelDisplay($rowData);
return $this->success('', [
'row' => $rowData
]);
}
/**
* 表单只读展示:根据 channel_id 解析渠道名称与渠道负责人admin.channel_id → admin.username取首个
*/
public function channelBindPreview(Request $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
$cid = $request->get('channel_id') ?? $request->post('channel_id');
if ($cid === null || $cid === '') {
return $this->success('', [
'channel_name' => '',
'channel_admin_username' => '',
]);
}
if (!Db::name('channel')->where('id', $cid)->value('id')) {
return $this->error(__('Record not found'));
}
$row = $this->enrichChannelDisplay(['channel_id' => $cid]);
return $this->success('', [
'channel_name' => $row['channel_name'] ?? '',
'channel_admin_username' => $row['channel_admin_username'] ?? '',
]);
}
public function del(Request $request): Response
{
$response = $this->initializeBackend($request);
@@ -353,7 +390,21 @@ class Group extends Backend
}
$data = $this->model->where($where)->select()->toArray();
$channelIds = [];
foreach ($data as $datum) {
$c = $datum['channel_id'] ?? null;
if ($c !== null && $c !== '') {
$channelIds[] = $c;
}
}
$channelNames = [];
if ($channelIds !== []) {
$channelNames = Db::name('channel')->where('id', 'in', array_unique($channelIds))->column('name', 'id');
}
foreach ($data as &$datum) {
$c = $datum['channel_id'] ?? null;
$datum['channel_name'] = ($c !== null && $c !== '') ? ($channelNames[$c] ?? '') : '';
if ($datum['rules']) {
if ($datum['rules'] == '*') {
$datum['rules'] = __('Super administrator');
@@ -368,6 +419,7 @@ class Group extends Backend
$datum['rules'] = __('No permission');
}
}
unset($datum);
return $this->assembleTree ? $this->tree->assembleChild($data) : $data;
}
@@ -418,4 +470,85 @@ class Group extends Backend
return null;
}
/**
* 顶级角色组可选渠道;子级继承父级 channel_id不信任客户端提交的子级 channel_id
*
* @param array<string, mixed> $data
*/
private function applyChannelInheritance(array &$data, int $pidInt): ?Response
{
if ($pidInt === 0) {
if (!$this->auth->isSuperAdmin()) {
unset($data['channel_id']);
$cc = $this->getCreatorChannelId();
if ($cc !== null && $cc !== '') {
$data['channel_id'] = $cc;
}
}
$cid = $data['channel_id'] ?? null;
if ($cid !== null && $cid !== '') {
$exists = Db::name('channel')->where('id', $cid)->value('id');
if (!$exists) {
return $this->error(__('Record not found'));
}
}
return null;
}
unset($data['channel_id']);
$parent = Db::name('admin_group')->where('id', $pidInt)->find();
if (!$parent) {
return $this->error(__('Record not found'));
}
$data['channel_id'] = $parent['channel_id'];
return null;
}
/**
* @param array<string, mixed> $row
* @return array<string, mixed>
*/
private function enrichChannelDisplay(array $row): array
{
$row['channel_name'] = '';
$row['channel_admin_username'] = '';
$cid = $row['channel_id'] ?? null;
if ($cid === null || $cid === '') {
return $row;
}
$ch = Db::name('channel')->where('id', $cid)->field(['id', 'name'])->find();
if (!$ch) {
return $row;
}
$row['channel_name'] = $ch['name'] ?? '';
$row['channel_admin_username'] = (string) (Db::name('admin')->where('channel_id', $cid)->order('id', 'asc')->value('username') ?? '');
return $row;
}
private function syncDescendantChannelIds(int $groupId): void
{
$channelId = Db::name('admin_group')->where('id', $groupId)->value('channel_id');
$children = Db::name('admin_group')->where('pid', $groupId)->column('id');
foreach ($children as $childId) {
Db::name('admin_group')->where('id', $childId)->update(['channel_id' => $channelId]);
$this->syncDescendantChannelIds($childId);
}
}
private function getCreatorChannelId(): mixed
{
$currentAdmin = Db::name('admin')
->field(['id', 'channel_id'])
->where('id', $this->auth->id)
->find();
if ($currentAdmin && !empty($currentAdmin['channel_id'])) {
return $currentAdmin['channel_id'];
}
return null;
}
}

View File

@@ -13,7 +13,7 @@ class Record extends Backend
{
protected ?object $model = null;
protected string|array $preExcludeFields = ['id', 'create_time', 'update_time'];
protected string|array $preExcludeFields = ['id', 'create_time', 'update_time', 'platform_profit_amount', 'winner_user_count'];
protected string|array $quickSearchField = ['id', 'period_no'];

View File

@@ -122,7 +122,6 @@ class BetOrder extends Backend
if ($admin && !empty($admin['channel_id'])) {
$ids[] = $admin['channel_id'];
}
$byAdmin = Db::name('channel')->where('admin_id', $this->auth->id)->column('id');
return array_values(array_unique(array_merge($ids, $byAdmin)));
return array_values(array_unique($ids));
}
}

View File

@@ -81,7 +81,6 @@ class DepositOrder extends Backend
if ($admin && !empty($admin['channel_id'])) {
$ids[] = $admin['channel_id'];
}
$byAdmin = Db::name('channel')->where('admin_id', $this->auth->id)->column('id');
return array_values(array_unique(array_merge($ids, $byAdmin)));
return array_values(array_unique($ids));
}
}

View File

@@ -82,7 +82,6 @@ class WithdrawOrder extends Backend
if ($admin && !empty($admin['channel_id'])) {
$ids[] = $admin['channel_id'];
}
$byAdmin = Db::name('channel')->where('admin_id', $this->auth->id)->column('id');
return array_values(array_unique(array_merge($ids, $byAdmin)));
return array_values(array_unique($ids));
}
}

View File

@@ -20,7 +20,7 @@ class User extends Backend
*/
protected ?object $model = null;
protected array|string $preExcludeFields = ['id', 'uuid', 'create_time', 'update_time'];
protected array|string $preExcludeFields = ['id', 'uuid', 'create_time', 'update_time', 'invite_code'];
protected array $withJoinTable = ['channel', 'admin'];
@@ -35,7 +35,7 @@ class User extends Backend
}
/**
* 添加重写password 使用 Admin 同款加密uuid 由 username+channel_id 生成
* 添加重写password 使用 Admin 同款加密uuid 为 10 位唯一对外标识
* @throws Throwable
*/
protected function _add(): Response
@@ -47,6 +47,10 @@ class User extends Backend
}
$data = $this->applyInputFilter($data);
$inviteResolved = $this->applyInviteCodeToUserChannel($data);
if ($inviteResolved !== null) {
return $inviteResolved;
}
$data = $this->excludeFields($data);
$password = $data['password'] ?? null;
@@ -60,7 +64,7 @@ class User extends Backend
if (!is_string($username) || trim($username) === '' || $channelId === null || $channelId === '') {
return $this->error(__('Parameter %s can not be empty', ['username/channel_id']));
}
$data['uuid'] = md5(trim($username) . '|' . $channelId);
$data['uuid'] = \app\common\model\User::generateUniquePublicCode10();
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$data[$this->dataLimitField] = $this->auth->id;
@@ -95,7 +99,7 @@ class User extends Backend
}
/**
* 编辑重写password 使用 Admin 同款加密uuid 由 username+channel_id 生成
* 编辑重写password 使用 Admin 同款加密uuid 创建后不因改名改渠道自动变更
* @throws Throwable
*/
protected function _edit(): Response
@@ -130,18 +134,6 @@ class User extends Backend
}
}
$nextUsername = array_key_exists('username', $data) ? $data['username'] : $row['username'];
$nextChannelId = null;
if (array_key_exists('channel_id', $data)) {
$nextChannelId = $data['channel_id'];
} else {
$nextChannelId = $row['channel_id'] ?? null;
}
if (is_string($nextUsername) && trim($nextUsername) !== '' && $nextChannelId !== null && $nextChannelId !== '') {
$data['uuid'] = md5(trim($nextUsername) . '|' . $nextChannelId);
}
$result = false;
$this->model->startTrans();
try {
@@ -337,6 +329,63 @@ class User extends Backend
return array_values(array_unique(array_merge($own, $children)));
}
/**
* 请求中的 `invite_code`(子代理 admin.invite_code解析为 `channel_id`,并写入 `register_invite_code`、`admin_id`。
* 若同时提交 `channel_id` / `admin_id`,须与邀请码对应记录一致。
*/
private function applyInviteCodeToUserChannel(array &$data): ?Response
{
if (!array_key_exists('invite_code', $data)) {
return null;
}
$raw = $data['invite_code'];
unset($data['invite_code']);
$inviteCode = is_string($raw) ? trim($raw) : '';
if ($inviteCode === '') {
return null;
}
$row = Db::name('admin')->field(['id', 'channel_id'])->where('invite_code', $inviteCode)->find();
if (!$row) {
return $this->error(__('Invite code does not exist'));
}
$cid = $row['channel_id'] ?? null;
if ($cid === null || $cid === '') {
return $this->error(__('Invite code not bound to channel'));
}
$cidInt = intval(trim((string) $cid));
if ($cidInt <= 0) {
return $this->error(__('Invite code not bound to channel'));
}
if (isset($data['channel_id']) && $data['channel_id'] !== null && $data['channel_id'] !== '') {
$existing = intval(trim((string) $data['channel_id']));
if ($existing > 0 && $existing !== $cidInt) {
return $this->error(__('Parameter error'));
}
}
$data['channel_id'] = $cidInt;
$data['register_invite_code'] = $inviteCode;
$aidRaw = $row['id'] ?? null;
if ($aidRaw === null || $aidRaw === '') {
return $this->error(__('Parameter error'));
}
$aidInt = intval(trim((string) $aidRaw));
if ($aidInt <= 0) {
return $this->error(__('Parameter error'));
}
if (isset($data['admin_id']) && $data['admin_id'] !== null && $data['admin_id'] !== '') {
$reqAid = intval(trim((string) $data['admin_id']));
if ($reqAid > 0 && $reqAid !== $aidInt) {
return $this->error(__('Parameter error'));
}
}
$data['admin_id'] = $aidInt;
return null;
}
/**
* 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
*/

View File

@@ -1,6 +1,6 @@
<?php
namespace app\admin\controller\record;
namespace app\admin\controller\user;
use app\common\controller\Backend;
use support\think\Db;
@@ -124,7 +124,6 @@ class UserWalletRecord extends Backend
if ($admin && !empty($admin['channel_id'])) {
$ids[] = $admin['channel_id'];
}
$byAdmin = Db::name('channel')->where('admin_id', $this->auth->id)->column('id');
return array_values(array_unique(array_merge($ids, $byAdmin)));
return array_values(array_unique($ids));
}
}