diff --git a/app/Cache/Redis/Admin/AdminRedisKey.php b/app/Cache/Redis/Admin/AdminRedisKey.php index d6b9027..0a6b3ff 100644 --- a/app/Cache/Redis/Admin/AdminRedisKey.php +++ b/app/Cache/Redis/Admin/AdminRedisKey.php @@ -42,4 +42,13 @@ class AdminRedisKey { return '__system:admin:menu:list:role_id:'.$roleId; } + + /** + * 部门集合 + * @return string + */ + public static function adminSectionList() + { + return '__system:admin:section:list'; + } } \ No newline at end of file diff --git a/app/Cache/Redis/Admin/SectionCache.php b/app/Cache/Redis/Admin/SectionCache.php new file mode 100644 index 0000000..fa7634f --- /dev/null +++ b/app/Cache/Redis/Admin/SectionCache.php @@ -0,0 +1,75 @@ +key = AdminRedisKey::adminSectionList(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + public function getList(): array + { + if ($this->redis->exists($this->key,'system')) { + return json_decode($this->redis->get($this->key,'system'),true); + } + + $allList = $this->adminSectionModel->getList(); + + $data = $this->getDbList($allList); + + $this->redis->set($this->key,json_encode($data),'system'); + + return $data; + } + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + public function delList() + { + $this->redis->delete($this->key,'system'); + } +} \ No newline at end of file diff --git a/app/Constants/Admin/AuthCode.php b/app/Constants/Admin/AuthCode.php index efad9d4..3bdc868 100644 --- a/app/Constants/Admin/AuthCode.php +++ b/app/Constants/Admin/AuthCode.php @@ -28,4 +28,14 @@ class AuthCode * 超级管理员 */ const SUPERADMIN = 1; + + /** + * 部门启用 + */ + const SECTION_STATUS_ENABLE = 1; + + /** + * 部门禁用 + */ + const SECTION_STATUS_DISABLE = 2; } \ No newline at end of file diff --git a/app/Controller/Admin/SectionController.php b/app/Controller/Admin/SectionController.php new file mode 100644 index 0000000..6ccf816 --- /dev/null +++ b/app/Controller/Admin/SectionController.php @@ -0,0 +1,98 @@ +add(); + } + + /** + * @param SectionRequest $request + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + #[RequestMapping(path: "edit", methods: "POST")] + #[Scene(scene: "edit")] + public function edit(SectionRequest $request) + { + return (new SectionService)->edit(); + } + + /** + * @param SectionRequest $request + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + #[RequestMapping(path: "del", methods: "GET")] + #[Scene(scene: "del")] + public function del(SectionRequest $request) + { + return (new SectionService)->del(); + } + + /** + * @param SectionRequest $request + * @return array + */ + #[RequestMapping(path: "info", methods: "GET")] + #[Scene(scene: "info")] + public function info(SectionRequest $request) + { + return (new SectionService)->info(); + } + + /** + * @param SectionRequest $request + * @return array + */ + #[RequestMapping(path: "list", methods: "GET")] + #[Scene(scene: "list")] + public function list(SectionRequest $request) + { + return (new SectionService)->handle(); + } + + /** + * @param SectionRequest $request + * @return array + */ + #[RequestMapping(path: "all_list", methods: "GET")] + #[Scene(scene: "all_list")] + public function allList(SectionRequest $request) + { + return (new SectionService)->handle(); + } +} diff --git a/app/Model/AdminSection.php b/app/Model/AdminSection.php new file mode 100644 index 0000000..749f303 --- /dev/null +++ b/app/Model/AdminSection.php @@ -0,0 +1,74 @@ + 'integer', 'city_id' => 'integer', 'status' => 'integer']; + + const CREATED_AT = 'create_time'; + + const UPDATED_AT = 'update_time'; + + /** + * @param int $id + * @return \Hyperf\Database\Model\Model|Builder|null + */ + public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null + { + return $this->where('id',$id)->first(['id','pid','name','status','remark']); + } + + /** + * @param int $pid + * @return Builder[]|Collection + */ + public function getInfoByPid(int $pid): Collection|array + { + return $this->where('pid',$pid)->get(); + } + + /** + * 获取所有 + * @return array + */ + public function getList(): array + { + return $this + ->where('status', AuthCode::SECTION_STATUS_ENABLE) + ->get([['id','pid','name','status','remark']]) + ->toArray(); + } +} diff --git a/app/Model/AdminUser.php b/app/Model/AdminUser.php index 905218c..a671831 100644 --- a/app/Model/AdminUser.php +++ b/app/Model/AdminUser.php @@ -16,11 +16,12 @@ use Hyperf\DbConnection\Model\Model; * @property int $avatar * @property string $chinese_name * @property string $mobile - * @property int $status + * @property int $status * @property string $last_login_ip * @property string $last_login_time * @property int $is_del - * @property int $role_id + * @property int $role_id + * @property int $section_id * @property string $create_time * @property string $update_time */ diff --git a/app/Request/Admin/LoginRequest.php b/app/Request/Admin/LoginRequest.php index 02a530d..37c1e58 100644 --- a/app/Request/Admin/LoginRequest.php +++ b/app/Request/Admin/LoginRequest.php @@ -22,7 +22,7 @@ class LoginRequest extends FormRequest public function rules(): array { return [ - 'account' => 'required|digits:11', + 'account' => 'required|string|digits:11', 'password' => 'required|string|min:6', ]; } diff --git a/app/Request/Admin/SectionRequest.php b/app/Request/Admin/SectionRequest.php new file mode 100644 index 0000000..6dd7d55 --- /dev/null +++ b/app/Request/Admin/SectionRequest.php @@ -0,0 +1,43 @@ + 'required|integer', + 'name' => 'required|string', + 'city_id' => 'exists:system_city,id', + 'pid' => 'exists:admin_section,id', + 'status' => 'in:1,2', + ]; + } + + public function messages(): array + { + return [ + ]; + } + + protected array $scenes = [ + 'add' => ['name', 'city_id', 'status','pid'], + 'edit' => ['name', 'city_id', 'status','pid','id'], + ]; +} diff --git a/app/Service/Admin/User/SectionService.php b/app/Service/Admin/User/SectionService.php new file mode 100644 index 0000000..e336e9f --- /dev/null +++ b/app/Service/Admin/User/SectionService.php @@ -0,0 +1,156 @@ +adminSectionModel->get(['id','pid','name','status','remark']); + if (empty($all)) return $this->return->success('success',['list' => []]); + + $data = $this->getDbList($all->toArray()); + + return $this->return->success('success',['list' => $data]); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + public function add(): array + { + $model = new AdminSection(); + $pid = (int)$this->request->input('pid',0); + if ($pid > 0) $pidInfo = $this->adminSectionModel->getInfoById($pid); + + $model->name = $this->request->input('name'); + $model->pid = $pid; + $model->city_id = !empty($pidInfo) ? $pidInfo->city_id : $this->request->input('city_id',0); + $model->remark = $this->request->input('remark'); + $model->status = $this->request->input('status',1); + + if (!$model->save()) throw new AdminException('添加失败'); + + $this->sectionCache->delList(); + + return $this->return->success(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + public function edit(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->adminSectionModel->getInfoById($id); + if (empty($info)) throw new AdminException('数据不存在'); + + $info->name = $this->request->input('name'); + $info->pid = $this->request->input('pid',0); + $info->city_id = $this->request->input('city_id',0); + $info->remark = $this->request->input('remark'); + $info->status = $this->request->input('status',1); + + if (!$info->save()) throw new AdminException('修改失败'); + $this->sectionCache->delList(); + return $this->return->success(); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + public function del(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->adminSectionModel->getInfoById($id); + if (empty($info)) throw new AdminException('数据不存在'); + + $children = $this->adminSectionModel->getInfoByPid($info->id); + if (empty($children)) throw new AdminException('请先删除下级数据'); + + $this->adminUserModel->where('section_id',$id)->update(['section_id'=>0]); + + $info->delete(); + $this->sectionCache->delList(); + return $this->return->success(); + } + + /** + * @return array + */ + public function info(): array + { + $id = (int)$this->request->input('id'); + + $info = $this->adminSectionModel->getInfoById($id); + if (empty($info)) throw new AdminException('数据不存在'); + + return $this->return->success('success',$info->toArray()); + } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws RedisException + */ + public function allList(): array + { + return $this->return->success('success',['list' => $this->sectionCache->getList()]); + } +} \ No newline at end of file diff --git a/app/Service/ServiceTrait/Admin/SectionTrait.php b/app/Service/ServiceTrait/Admin/SectionTrait.php new file mode 100644 index 0000000..34f8a56 --- /dev/null +++ b/app/Service/ServiceTrait/Admin/SectionTrait.php @@ -0,0 +1,27 @@ +getDbList($allMenuList, (int)$menu['id']); + if (!empty($children)) { + $menu['children'] = $children; + } + $menuList[] = $menu; + } + } + return $menuList; + } +} \ No newline at end of file diff --git a/sync/http/admin/auth.http b/sync/http/admin/auth.http index 7416483..fef489b 100644 --- a/sync/http/admin/auth.http +++ b/sync/http/admin/auth.http @@ -114,4 +114,39 @@ Authorization: Bearer {{admin_token}} ### ali sts 临时授权 GET {{host}}/admin/third/sts/accredit Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + + +### 部门列表 +GET {{host}}/admin/section/list +Content-Type: application/json +Authorization: Bearer {{admin_token}} + +### 部门添加 +POST {{host}}/admin/section/add +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +name=总公司c&pid=1 + +### 部门修改 +POST {{host}}/admin/section/edit +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{admin_token}} + +id=1&name=总公司啊 + +### 部门删除 +GET {{host}}/admin/section/del?id=1 +Content-Type: application/json +Authorization: Bearer {{admin_token}} + +### 部门详情 +GET {{host}}/admin/section/info?id=1 +Content-Type: application/json +Authorization: Bearer {{admin_token}} + +### 部门启用列表 +GET {{host}}/admin/section/all_list +Content-Type: application/json Authorization: Bearer {{admin_token}} \ No newline at end of file