修改渠道ChannelManage关联部门SystemDepart

This commit is contained in:
2026-03-10 10:09:35 +08:00
parent e94ebd3fe6
commit a6d87d5c0d
8 changed files with 242 additions and 15 deletions

View File

@@ -10,6 +10,8 @@ use plugin\saiadmin\basic\BaseController;
use app\channel\logic\manage\ChannelManageLogic;
use app\channel\validate\manage\ChannelManageValidate;
use plugin\saiadmin\app\model\system\SystemUser;
use plugin\saiadmin\app\model\system\SystemDept;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\service\Permission;
use support\Request;
use support\Response;
@@ -41,14 +43,20 @@ class ChannelManageController extends BaseController
['name', ''],
['title', ''],
['status', ''],
['department_id', ''],
['total_recharge', ''],
['total_withdrawal', ''],
['total_profit', ''],
]);
$query = $this->logic->search($where);
// 不使用 with('admin')SystemUser 为 Think 模型,与 Eloquent 混用会触发 getConnectionName 报错,改为手动填充
// 仅渠道管理员和超级管理员可查看:超管看全部,渠道管理员只看自己管理的渠道
if (isset($this->adminInfo['id']) && (int) $this->adminInfo['id'] > 1) {
$query->where('admin_id', $this->adminInfo['id']);
}
$data = $this->logic->getList($query);
$data['data'] = $this->attachAdminToRows($data['data'] ?? []);
$data['data'] = $this->attachDepartmentToRows($data['data'] ?? []);
$data['data'] = $this->attachCanOperate($data['data'] ?? []);
return $this->success($data);
}
@@ -64,6 +72,9 @@ class ChannelManageController extends BaseController
$model = $this->logic->read($id);
if ($model) {
$data = is_array($model) ? $model : $model->toArray();
if (!$this->logic->canOperateChannel($data)) {
throw new ApiException('没有权限查看该渠道');
}
return $this->success($data);
} else {
return $this->fail('未查找到信息');
@@ -98,6 +109,10 @@ class ChannelManageController extends BaseController
public function update(Request $request): Response
{
$data = $request->post();
$channel = $this->logic->read($data['id'] ?? 0);
if (!$channel || !$this->logic->canOperateChannel($channel)) {
throw new ApiException('没有权限修改该渠道');
}
$data['agent'] = md5((string)($data['name'] ?? '') . (string)($data['admin_id'] ?? ''));
$this->validate('update', $data);
$result = $this->logic->edit($data['id'], $data);
@@ -124,6 +139,10 @@ class ChannelManageController extends BaseController
if ($status === null || $status === '') {
return $this->fail('缺少 status');
}
$channel = $this->logic->read($id);
if (!$channel || !$this->logic->canOperateChannel($channel)) {
throw new ApiException('没有权限修改该渠道');
}
$this->logic->edit($id, ['status' => (int) $status]);
return $this->success('修改成功');
}
@@ -140,6 +159,13 @@ class ChannelManageController extends BaseController
if (empty($ids)) {
return $this->fail('请选择要删除的数据');
}
$idList = is_array($ids) ? $ids : (array) $ids;
foreach ($idList as $id) {
$channel = $this->logic->read($id);
if (!$channel || !$this->logic->canOperateChannel($channel)) {
throw new ApiException('没有权限删除该渠道');
}
}
$result = $this->logic->destroy($ids);
if ($result) {
return $this->success('删除成功');
@@ -148,6 +174,21 @@ class ChannelManageController extends BaseController
}
}
/**
* 获取可操作部门列表(树形,用于渠道所属部门选择)
* @param Request $request
* @return Response
*/
#[Permission('渠道部门列表', 'channel:manage:index:index')]
public function getDeptList(Request $request): Response
{
$this->ensureAdminInfo();
$logic = new \plugin\saiadmin\app\logic\system\SystemDeptLogic();
$logic->init($this->adminInfo ?? []);
$data = $logic->accessDept(['status' => 1]);
return $this->success($data);
}
/**
* 获取管理员下拉列表(远程关联 SystemUservalue=id, label=name
* @param Request $request
@@ -214,4 +255,73 @@ class ChannelManageController extends BaseController
return $rows;
}
/**
* 为列表行附加部门信息department_id 关联 SystemDept
* @param array $rows 列表行,可为 Eloquent 模型或数组
* @return array 已附加 department 的列表
*/
protected function attachDepartmentToRows(array $rows): array
{
if (empty($rows)) {
return $rows;
}
$deptIds = [];
foreach ($rows as $row) {
$id = is_array($row) ? ($row['department_id'] ?? null) : ($row->department_id ?? null);
if ($id !== null && $id !== '') {
$deptIds[(string) $id] = true;
}
}
$deptIds = array_keys($deptIds);
if (empty($deptIds)) {
foreach ($rows as &$row) {
if (is_array($row)) {
$row['department'] = null;
} else {
$row->setAttribute('department', null);
}
}
unset($row);
return $rows;
}
$depts = SystemDept::whereIn('id', $deptIds)
->field('id, name')
->select()
->toArray();
$map = [];
foreach ($depts as $d) {
$map[(string) ($d['id'] ?? '')] = $d;
}
foreach ($rows as &$row) {
$did = is_array($row) ? ($row['department_id'] ?? null) : ($row->department_id ?? null);
$dept = $map[(string) ($did ?? '')] ?? null;
if (is_array($row)) {
$row['department'] = $dept;
} else {
$row->setAttribute('department', $dept);
}
}
unset($row);
return $rows;
}
/**
* 为列表行附加是否可操作标记(仅渠道管理员和超级管理员可操作)
* @param array $rows 列表行
* @return array
*/
protected function attachCanOperate(array $rows): array
{
foreach ($rows as &$row) {
$canOperate = $this->logic->canOperateChannel($row);
if (is_array($row)) {
$row['canOperate'] = $canOperate;
} else {
$row->setAttribute('canOperate', $canOperate);
}
}
unset($row);
return $rows;
}
}

View File

@@ -8,7 +8,6 @@ namespace app\channel\logic\manage;
use plugin\saiadmin\basic\eloquent\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\channel\model\manage\ChannelManage;
/**
@@ -24,4 +23,22 @@ class ChannelManageLogic extends BaseLogic
$this->model = new ChannelManage();
}
/**
* 判断当前用户是否可操作该渠道(仅渠道管理员或超级管理员可操作)
* @param object|array $channel 渠道数据,需包含 admin_id
* @return bool
*/
public function canOperateChannel($channel): bool
{
if (empty($this->adminInfo) || !isset($this->adminInfo['id'])) {
return false;
}
$adminId = (int) ($this->adminInfo['id']);
if ($adminId === 1) {
return true;
}
$channelAdminId = is_array($channel) ? ($channel['admin_id'] ?? null) : ($channel->admin_id ?? null);
return $channelAdminId !== null && (int) $channelAdminId === $adminId;
}
}

View File

@@ -18,6 +18,7 @@ use plugin\saiadmin\basic\eloquent\BaseModel;
* @property $title 标题
* @property $status 状态
* @property $admin_id 管理员
* @property $department_id 所属部门ID关联sa_system_dept
* @property $game_url 游戏地址
* @property $image 图标
* @property $agent 代理agent
@@ -67,6 +68,16 @@ class ChannelManage extends BaseModel
$query->where('title', 'like', '%'.$value.'%');
}
/**
* 部门 搜索
*/
public function searchDepartmentIdAttr($query, $value)
{
if ($value !== '' && $value !== null) {
$query->where('department_id', $value);
}
}
/**
* 总充值 搜索
*/

View File

@@ -82,6 +82,12 @@ Route::group('/core', function () {
Route::get("/server/cache", [\plugin\saiadmin\app\controller\system\SystemServerController::class, 'cache']);
Route::post("/server/clear", [\plugin\saiadmin\app\controller\system\SystemServerController::class, 'clear']);
// 渠道管理
fastRoute('channel/manage/ChannelManage', \app\channel\controller\manage\ChannelManageController::class);
Route::get("/channel/manage/ChannelManage/getDeptList", [\app\channel\controller\manage\ChannelManageController::class, 'getDeptList']);
Route::get("/channel/manage/ChannelManage/getAdminList", [\app\channel\controller\manage\ChannelManageController::class, 'getAdminList']);
Route::put("/channel/manage/ChannelManage/updateStatus", [\app\channel\controller\manage\ChannelManageController::class, 'updateStatus']);
// 数据表维护
Route::get("/database/index", [\plugin\saiadmin\app\controller\system\DataBaseController::class, 'index']);
Route::get("/database/recycle", [\plugin\saiadmin\app\controller\system\DataBaseController::class, 'recycle']);