diff --git a/app/Controller/Admin/AuthController.php b/app/Controller/Admin/AuthController.php new file mode 100644 index 0000000..61cfdb6 --- /dev/null +++ b/app/Controller/Admin/AuthController.php @@ -0,0 +1,71 @@ +add(); + } + + public function menu_edit() + { + return (new RoleMenuService)->edit(); + } + + public function menu_del() + { + return (new RoleMenuService)->del(); + } + + public function menu_list() + { + return (new RoleMenuService)->handle(); + } + + public function mean() + { + return (new RoleMenuService)->details(); + } + + public function role_add(AuthRequest $request) + { + return (new RoleService)->add(); + } + + public function role_edit() + { + return (new RoleService)->edit(); + } + + public function role_status() + { + return (new RoleService)->changeStatus(); + } + + public function role_list() + { + return (new RoleService)->handle(); + } + + #[RequestMapping(path: "role", methods: "GET")] + #[Scene(scene: "role_info")] + public function role(AuthRequest $request) + { + return (new RoleService)->details(); + } +} diff --git a/app/Model/AdminMenu.php b/app/Model/AdminMenu.php new file mode 100644 index 0000000..a2a26a5 --- /dev/null +++ b/app/Model/AdminMenu.php @@ -0,0 +1,45 @@ + 'integer', 'parent_id' => 'integer', 'type' => 'integer', 'status' => 'integer', 'sort' => 'integer']; + + const CREATED_AT = 'create_time'; + + const UPDATED_AT = 'update_time'; + +} diff --git a/app/Model/AdminRole.php b/app/Model/AdminRole.php new file mode 100644 index 0000000..fe73e10 --- /dev/null +++ b/app/Model/AdminRole.php @@ -0,0 +1,58 @@ + 'integer', 'status' => 'integer']; + + const CREATED_AT = 'create_time'; + + const UPDATED_AT = 'update_time'; + + + /** + * @param int $id + * @return Builder|\Hyperf\Database\Model\Model|object|null + */ + public function getInfoById(int $id) + { + return $this->where('id', $id)->first(); + } + + /** + * @param string $name + * @return Builder|\Hyperf\Database\Model\Model|object|null + */ + public function getInfoByName(string $name) + { + return $this->where('name', $name)->first(); + } +} diff --git a/app/Request/Admin/AuthRequest.php b/app/Request/Admin/AuthRequest.php new file mode 100644 index 0000000..94598ce --- /dev/null +++ b/app/Request/Admin/AuthRequest.php @@ -0,0 +1,39 @@ + 'required|integer', + ]; + } + + public function messages(): array + { + return [ + '' + ]; + } + + protected array $scenes = [ + 'role_info' => 'role_id', + ]; +} diff --git a/app/Service/Admin/User/RoleMenuService.php b/app/Service/Admin/User/RoleMenuService.php new file mode 100644 index 0000000..0ee694c --- /dev/null +++ b/app/Service/Admin/User/RoleMenuService.php @@ -0,0 +1,41 @@ +return->success(); + } + + public function add() + { + return $this->return->success(); + } + + public function edit() + { + return $this->return->success(); + } + + public function del() + { + return $this->return->success(); + } + + public function details() + { + return $this->return->success(); + } +} \ No newline at end of file diff --git a/app/Service/Admin/User/RoleService.php b/app/Service/Admin/User/RoleService.php new file mode 100644 index 0000000..e00e95c --- /dev/null +++ b/app/Service/Admin/User/RoleService.php @@ -0,0 +1,135 @@ +request->input('limit', 10); + + $list = $this->adminRoleModel->paginate($limit,$this->field)->toArray(); + + return $this->return->success('success',['list' => $list]); + } + + + public function add() + { + $name = $this->request->input('name'); + + if ($this->adminRoleModel->getInfoByName($name)) throw new AdminException('角色已存在'); + + Db::beginTransaction(); + try { + $model = new AdminRole(); + + $model->name = $name; + $model->status = $this->request->input('status', 1); + $model->remark = $this->request->input('remark', ''); + + if (!$model->save()) throw new Exception('添加失败'); + + //todo 添加角色权限 + $menuIdArr = explode(',', $this->request->input('menu_ids')); + + + + Db::commit(); + } catch (Exception $e) { + Db::rollBack(); + throw new AdminException($e->getMessage()); + } + + return $this->return->success(); + } + + /** + * 修改 + * @return array + */ + public function edit(): array + { + $id = $this->request->input('id'); + + if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); + + Db::beginTransaction(); + try { + $info->name = $this->request->input('name'); + $info->status = $this->request->input('status', 1); + $info->remark = $this->request->input('remark', ''); + + if (!$info->save()) throw new Exception('修改失败'); + + //todo 删除权限 添加角色权限 + + Db::commit(); + } catch (Exception $e) { + Db::rollBack(); + throw new AdminException($e->getMessage()); + } + + return $this->return->success(); + } + + /** + * 修改状态 + * @return array + * @throws Exception + */ + public function changeStatus(): array + { + $id = $this->request->input('id'); + + if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); + + $info->status = $this->request->input('status', 0); + + if (!$info->save()) throw new Exception('修改失败'); + + return $this->return->success(); + } + + /** + * 详情 + * @return array + */ + public function details(): array + { + $roleId = $this->request->input('role_id'); + $res = $this->adminRoleModel->where('id',$roleId)->first($this->field); + if (!$res) throw new AdminException('角色不存在'); + + return $this->return->success('success',[ + 'info' => $res->toArray() + ]); + } +} \ No newline at end of file diff --git a/sync/database/admin.sql b/sync/database/admin.sql index af284cc..e195eaa 100644 --- a/sync/database/admin.sql +++ b/sync/database/admin.sql @@ -22,4 +22,32 @@ CREATE TABLE `app_admin_user` ( `role_id` tinyint(1) NOT NULL DEFAULT '0' COMMENT '角色', `create_time` datetime NOT NULL COMMENT '创建时间', PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='后台用户表'; \ No newline at end of file +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='后台用户表'; + +-- 后台用户角色表 +DROP TABLE IF EXISTS `app_admin_role`; +CREATE TABLE `app_admin_role` ( + `id` int NOT NULL AUTO_INCREMENT, + `name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '角色名', + `remark` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '备注', + `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=正常 2=禁用', + `create_time` datetime DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 后台用户权限表 +DROP TABLE IF EXISTS `app_admin_menu`; +CREATE TABLE `app_admin_menu` ( + `id` int NOT NULL AUTO_INCREMENT, + `parent_id` int NOT NULL DEFAULT '0' COMMENT '父级权限ID', + `title` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '权限名', + `icon` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '图标', + `type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '权限类型 1=菜单 2=按钮', + `url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '权限URL', + `value` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '权限方法', + `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '权限状态 1=正常 2=禁用', + `sort` int NOT NULL DEFAULT '0' COMMENT '排序', + `create_time` datetime DEFAULT NULL, + `update_time` datetime DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/sync/http/admin/auth.http b/sync/http/admin/auth.http new file mode 100644 index 0000000..c065e3a --- /dev/null +++ b/sync/http/admin/auth.http @@ -0,0 +1,5 @@ +### 角色详情 +GET {{host}}/admin/auth/role?role_id=1 +Content-Type: application/x-www-form-urlencoded + +### \ No newline at end of file diff --git a/sync/http/admin/http-client.env.json b/sync/http/admin/http-client.env.json new file mode 100644 index 0000000..1fc4c7f --- /dev/null +++ b/sync/http/admin/http-client.env.json @@ -0,0 +1,5 @@ +{ + "dev": { + "host": "http://api.hhl.top" + } +} \ No newline at end of file