logic = new ChannelManageLogic(); $this->validate = new ChannelManageValidate; parent::__construct(); } /** * 数据列表 * @param Request $request * @return Response */ #[Permission('渠道列表', 'channel:manage:index:index')] public function index(Request $request): Response { $where = $request->more([ ['name', ''], ['title', ''], ['status', ''], ['department_id', ''], ['total_recharge', ''], ['total_withdrawal', ''], ['total_profit', ''], ]); $query = $this->logic->search($where); // 仅渠道管理员和超级管理员可查看:超管看全部,渠道管理员只看自己管理的渠道 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); } /** * 读取数据 * @param Request $request * @return Response */ #[Permission('渠道读取', 'channel:manage:index:read')] public function read(Request $request): Response { $id = $request->input('id', ''); $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('未查找到信息'); } } /** * 保存数据 * @param Request $request * @return Response */ #[Permission('渠道添加', 'channel:manage:index:save')] public function save(Request $request): Response { $data = $request->post(); $data['agent'] = md5((string)($data['name'] ?? '') . (string)($data['admin_id'] ?? '')); $this->validate('save', $data); $result = $this->logic->add($data); if ($result) { return $this->success('添加成功'); } else { return $this->fail('添加失败'); } } /** * 更新数据 * @param Request $request * @return Response */ #[Permission('渠道修改', 'channel:manage:index:update')] 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); if ($result) { return $this->success('修改成功'); } else { return $this->fail('修改失败'); } } /** * 仅更新状态(列表内开关用) * @param Request $request * @return Response */ #[Permission('渠道管理-更新状态', 'channel:manage:index:update')] public function updateStatus(Request $request): Response { $id = $request->input('id'); $status = $request->input('status'); if ($id === null || $id === '') { return $this->fail('缺少 id'); } 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('修改成功'); } /** * 删除数据 * @param Request $request * @return Response */ #[Permission('渠道删除', 'channel:manage:index:destroy')] public function destroy(Request $request): Response { $ids = $request->post('ids', ''); 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('删除成功'); } else { return $this->fail('删除失败'); } } /** * 获取可操作部门列表(树形,用于渠道所属部门选择) * @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); } /** * 获取管理员下拉列表(远程关联 SystemUser:value=id, label=name) * @param Request $request * @return Response */ #[Permission('渠道管理员列表', 'channel:manage:index:index')] public function getAdminList(Request $request): Response { $list = SystemUser::where('status', 1) ->field('id, username, realname') ->select(); $rows = $list->toArray(); $data = array_map(function ($row) { $name = !empty($row['realname']) && trim($row['realname']) !== '' ? trim($row['realname']) : ($row['username'] ?? '#' . $row['id']); return [ 'id' => $row['id'], 'name' => $name, ]; }, $rows); return $this->success($data); } /** * 为列表行附加管理员信息(避免 Eloquent with(Think 模型) 触发 getConnectionName 报错) * @param array $rows 列表行,可为 Eloquent 模型或数组 * @return array 已附加 admin 的列表 */ protected function attachAdminToRows(array $rows): array { if (empty($rows)) { return $rows; } $adminIds = []; foreach ($rows as $row) { $id = is_array($row) ? ($row['admin_id'] ?? null) : $row->admin_id; if ($id !== null && $id !== '') { $adminIds[(string) $id] = true; } } $adminIds = array_keys($adminIds); if (empty($adminIds)) { return $rows; } $admins = SystemUser::whereIn('id', $adminIds) ->field('id, username, realname') ->select() ->toArray(); $map = []; foreach ($admins as $a) { $map[(string) ($a['id'] ?? '')] = $a; } foreach ($rows as &$row) { $aid = is_array($row) ? ($row['admin_id'] ?? null) : $row->admin_id; $admin = $map[(string) ($aid ?? '')] ?? null; if (is_array($row)) { $row['admin'] = $admin; } else { $row->setAttribute('admin', $admin); } } unset($row); 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; } }