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', ''], ['total_recharge', ''], ['total_withdrawal', ''], ['total_profit', ''], ]); $query = $this->logic->search($where); // 不使用 with('admin'):SystemUser 为 Think 模型,与 Eloquent 混用会触发 getConnectionName 报错,改为手动填充 $data = $this->logic->getList($query); $data['data'] = $this->attachAdminToRows($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(); 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(); $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'); } $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('请选择要删除的数据'); } $result = $this->logic->destroy($ids); if ($result) { return $this->success('删除成功'); } else { return $this->fail('删除失败'); } } /** * 获取管理员下拉列表(远程关联 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; } }