diff --git a/src/api/system/role.ts b/src/api/system/role.ts new file mode 100644 index 0000000..e4d8d2e --- /dev/null +++ b/src/api/system/role.ts @@ -0,0 +1,48 @@ +import { get, post } from '@/utils/request'; + +/** 角色记录数据结构(不导出,仅供本文件内使用) */ +interface RoleRecord { + /** 字符串 ID */ + id: string; + /** 角色名称 */ + roleName: string; + /** 角色标识 */ + roleKey: string; + /** 状态:启用 | 禁用 */ + status: string; + /** 备注 */ + remark?: string; +} + +/** 新增/编辑角色的请求参数(不导出) */ +interface RoleParams { + roleName: string; + roleKey: string; + status: string; + remark?: string; +} + +// ───────────── 角色接口 ───────────── + +/** 获取角色列表 */ +export const listRole = () => get('/api/system/role/list'); + +/** + * 新增角色 + * @param params 角色参数 + */ +export const addRole = (params: RoleParams) => post('/api/system/role/add', params); + +/** + * 编辑角色 + * @param id 角色 ID + * @param params 角色参数 + */ +export const editRole = (id: string, params: RoleParams) => + post('/api/system/role/edit', { id, ...params }); + +/** + * 删除角色 + * @param id 角色 ID + */ +export const delRole = (id: string) => post('/api/system/role/del', { id }); diff --git a/src/mock/system.ts b/src/mock/system.ts index ee59a8a..ff075c7 100644 --- a/src/mock/system.ts +++ b/src/mock/system.ts @@ -19,6 +19,15 @@ interface MockUserRecord { deptId?: string; } +/** 角色记录(mock 本地定义) */ +interface MockRoleRecord { + id: string; + roleName: string; + roleKey: string; + status: string; + remark?: string; +} + // ── mock 数据 ──────────────────────────────── /** 部门树 mock 数据 */ @@ -56,6 +65,14 @@ const userMap: Record = { '1-3': [], }; +/** 角色列表 mock 数据(运行时可变) */ +const roleList: MockRoleRecord[] = [ + { id: '1', roleName: '超级管理员', roleKey: 'admin', status: '启用', remark: '拥有所有权限' }, + { id: '2', roleName: '编辑员', roleKey: 'editor', status: '启用', remark: '内容编辑权限' }, + { id: '3', roleName: '审核员', roleKey: 'reviewer', status: '启用', remark: '内容审核权限' }, + { id: '4', roleName: '访客', roleKey: 'guest', status: '禁用', remark: '只读权限' }, +]; + /** 包装为统一响应格式 */ const ok = (data: unknown) => ({ code: '0', msg: 'ok', data, time: Date.now(), ok: true }); @@ -154,4 +171,37 @@ export const systemHandlers = [ } return HttpResponse.json(ok({ id: body.id })); }), + + // ───── 角色接口 ───── + + http.get('/api/system/role/list', () => { + return HttpResponse.json(ok(roleList)); + }), + + http.post('/api/system/role/add', async ({ request }) => { + const body = await request.json() as Record; + const newRole: MockRoleRecord = { + id: `role_${Date.now()}`, + roleName: body.roleName as string, + roleKey: body.roleKey as string, + status: body.status as string, + remark: body.remark as string | undefined, + }; + roleList.push(newRole); + return HttpResponse.json(ok(newRole)); + }), + + http.post('/api/system/role/edit', async ({ request }) => { + const body = await request.json() as MockRoleRecord; + const idx = roleList.findIndex((r) => r.id === body.id); + if (idx !== -1) roleList[idx] = body; + return HttpResponse.json(ok(body)); + }), + + http.post('/api/system/role/del', async ({ request }) => { + const body = await request.json() as { id: string }; + const idx = roleList.findIndex((r) => r.id === body.id); + if (idx !== -1) roleList.splice(idx, 1); + return HttpResponse.json(ok({ id: body.id })); + }), ]; diff --git a/src/pages/system/role/RoleModal.tsx b/src/pages/system/role/RoleModal.tsx new file mode 100644 index 0000000..018be8e --- /dev/null +++ b/src/pages/system/role/RoleModal.tsx @@ -0,0 +1,77 @@ +import { Form, Input, Modal, Select } from 'antd'; +import { useEffect } from 'react'; + +/** 角色表单字段(页面本地定义) */ +export interface RoleFormValues { + roleName: string; + roleKey: string; + status: string; + remark?: string; +} + +interface RoleModalProps { + /** 弹窗是否可见 */ + open: boolean; + /** 弹窗标题 */ + title: string; + /** 编辑时的初始值 */ + initialValues?: Partial; + /** 确认回调 */ + onOk: (values: RoleFormValues) => void; + /** 取消回调 */ + onCancel: () => void; +} + +const RoleModal = ({ open, title, initialValues, onOk, onCancel }: RoleModalProps) => { + const [form] = Form.useForm(); + + /** 每次打开时重置并填充表单 */ + useEffect(() => { + if (open) { + form.resetFields(); + if (initialValues) form.setFieldsValue(initialValues); + } + }, [open, initialValues, form]); + + const handleOk = async () => { + const values = await form.validateFields(); + onOk(values); + }; + + const isEdit = !!initialValues; + + return ( + +
+ + + + + {/* 编辑时角色标识不可修改 */} + + + +