[渠道管理]

This commit is contained in:
2026-04-15 10:19:41 +08:00
parent 7b002c9410
commit 8cc28096c4
11 changed files with 595 additions and 343 deletions

View File

@@ -1,6 +1,6 @@
<?php
namespace app\admin\controller\game;
namespace app\admin\controller;
use Throwable;
use app\common\controller\Backend;
@@ -14,9 +14,9 @@ use Webman\Http\Request as WebmanRequest;
class Channel extends Backend
{
/**
* GameChannel模型对象
* Channel模型对象
* @var object|null
* @phpstan-var \app\common\model\GameChannel|null
* @phpstan-var \app\common\model\Channel|null
*/
protected ?object $model = null;
@@ -26,9 +26,12 @@ class Channel extends Backend
protected string|array $quickSearchField = ['id', 'code', 'name'];
private array $currentChannelIds = [];
protected function initController(WebmanRequest $request): ?Response
{
$this->model = new \app\common\model\GameChannel();
$this->model = new \app\common\model\Channel();
$this->currentChannelIds = $this->getCurrentChannelIds();
return null;
}
@@ -40,11 +43,13 @@ class Channel extends Backend
$response = $this->initializeBackend($request);
if ($response !== null) return $response;
$channels = Db::name('game_channel')
$query = Db::name('channel')
->field(['id', 'name', 'admin_group_id'])
->order('id', 'asc')
->select()
->toArray();
->order('id', 'asc');
if (!$this->auth->isSuperAdmin()) {
$query = $query->where('id', 'in', $this->currentChannelIds ?: [0]);
}
$channels = $query->select()->toArray();
$groupChildrenCache = [];
$getGroupChildren = function ($groupId) use (&$getGroupChildren, &$groupChildrenCache) {
@@ -131,13 +136,14 @@ class Channel extends Backend
$data = $this->applyInputFilter($data);
$data = $this->excludeFields($data);
$data = $this->normalizeAgentModeFields($data);
unset($data['invite_code']);
$adminId = $data['admin_id'] ?? null;
if ($adminId === null || $adminId === '') {
return $this->error(__('Parameter %s can not be empty', ['admin_id']));
}
// 不允许前端填写,统一后端根据管理员所属“顶级角色组(pid=0)”自动回填
if (array_key_exists('admin_group_id', $data)) {
unset($data['admin_group_id']);
}
@@ -153,6 +159,10 @@ class Channel extends Backend
return $this->error(__('Record not found'));
}
$data['admin_group_id'] = $topGroupId;
if (!$this->auth->isSuperAdmin()) {
$data['top_admin_id'] = $this->auth->id;
$data['admin_id'] = $this->auth->id;
}
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$data[$this->dataLimitField] = $this->auth->id;
@@ -198,6 +208,9 @@ class Channel extends Backend
if (!$row) {
return $this->error(__('Record not found'));
}
if (!$this->auth->isSuperAdmin() && !in_array($row['id'], $this->currentChannelIds, true)) {
return $this->error(__('You have no permission'));
}
$dataLimitAdminIds = $this->getDataLimitAdminIds();
if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
@@ -212,8 +225,9 @@ class Channel extends Backend
$data = $this->applyInputFilter($data);
$data = $this->excludeFields($data);
$data = $this->normalizeAgentModeFields($data);
unset($data['invite_code']);
// 不允许前端填写,统一后端根据管理员所属“顶级角色组(pid=0)”自动回填
if (array_key_exists('admin_group_id', $data)) {
unset($data['admin_group_id']);
}
@@ -232,6 +246,10 @@ class Channel extends Backend
}
$data['admin_group_id'] = $topGroupId;
}
if (!$this->auth->isSuperAdmin()) {
$data['top_admin_id'] = $this->auth->id;
$data['admin_id'] = $this->auth->id;
}
$result = false;
$this->model->startTrans();
@@ -270,17 +288,14 @@ class Channel extends Backend
*/
protected function _index(): Response
{
// 如果是 select 则转发到 select 方法,若未重写该方法,其实还是继续执行 index
if ($this->request && $this->request->get('select')) {
return $this->select($this->request);
}
/**
* 1. withJoin 不可使用 alias 方法设置表别名,别名将自动使用关联模型名称(小写下划线命名规则)
* 2. 以下的别名设置了主表别名,同时便于拼接查询参数等
* 3. paginate 数据集可使用链式操作 each(function($item, $key) {}) 遍历处理
*/
list($where, $alias, $limit, $order) = $this->queryBuilder();
if (!$this->auth->isSuperAdmin()) {
$where[] = [$alias['channel'] . '.id', 'in', $this->currentChannelIds ?: [0]];
}
$res = $this->model
->withJoin($this->withJoinTable, $this->withJoinType)
->with($this->withJoinTable)
@@ -297,7 +312,36 @@ class Channel extends Backend
]);
}
/**
* 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
*/
}
private function getCurrentChannelIds(): array
{
if ($this->auth->isSuperAdmin()) {
return Db::name('channel')->column('id');
}
$admin = Db::name('admin')
->field(['id', 'channel_id'])
->where('id', $this->auth->id)
->find();
$ids = [];
if ($admin && !empty($admin['channel_id'])) {
$ids[] = $admin['channel_id'];
}
$owned = Db::name('channel')->where('top_admin_id', $this->auth->id)->column('id');
$created = Db::name('channel')->where('admin_id', $this->auth->id)->column('id');
return array_values(array_unique(array_merge($ids, $owned, $created)));
}
private function normalizeAgentModeFields(array $data): array
{
$mode = $data['agent_mode'] ?? null;
if ($mode === 'turnover') {
$data['affiliate_share_rate'] = null;
$data['affiliate_fee_rate'] = null;
$data['carryover_balance'] = 0;
return $data;
}
if ($mode === 'affiliate') {
$data['turnover_share_rate'] = null;
}
return $data;
}
}