/** * Mock数据生成器 * 基于API文档定义生成符合规范的模拟数据 */ import type { Result, User, Role, Menu, OperationLog } from '@/types'; // 生成模拟用户数据 export const generateMockUser = (id?: number): User => { const userId = id || Math.floor(Math.random() * 10000) + 1; return { id: userId, username: `user${userId}`, email: `user${userId}@example.com`, phone: `138${Math.random().toString().substring(2, 11).padEnd(11, '0').substring(0, 9)}`, nickname: `昵称${userId}`, status: Math.random() > 0.5 ? 'ENABLED' : 'DISABLED', remark: `用户${userId}的备注`, createBy: 'admin', updateBy: 'admin', createdAt: new Date(Date.now() - Math.random() * 10000000000).toISOString(), updatedAt: new Date().toISOString(), permissions: ['user:view', 'user:edit'], roles: [] }; }; // 生成模拟角色数据 export const generateMockRole = (id?: number): Role => { const roleId = id || Math.floor(Math.random() * 1000) + 1; return { id: roleId, name: `角色${roleId}`, roleKey: `role${roleId}`, description: `角色${roleId}的描述`, status: Math.random() > 0.5 ? 'ENABLED' : 'DISABLED', sortOrder: Math.floor(Math.random() * 100), remark: `角色${roleId}的备注`, menuIds: [], createdAt: new Date(Date.now() - Math.random() * 10000000000).toISOString(), updatedAt: new Date().toISOString() }; }; // 生成模拟菜单数据 export const generateMockMenu = (id?: number, parentId?: number): Menu => { const menuId = id || Math.floor(Math.random() * 1000) + 1; return { id: menuId, name: `菜单${menuId}`, code: `menu${menuId}`, path: `/menu${menuId}`, icon: 'MenuOutlined', parentId: parentId || 0, sortOrder: Math.floor(Math.random() * 100), status: Math.random() > 0.5 ? 'ENABLED' : 'DISABLED', component: `views/Menu${menuId}.vue`, redirect: '', description: `菜单${menuId}的描述`, children: [], createdAt: new Date(Date.now() - Math.random() * 10000000000).toISOString(), updatedAt: new Date().toISOString() }; }; // 生成模拟操作日志数据 export const generateMockOperationLog = (id?: number): OperationLog => { const logId = id || Math.floor(Math.random() * 10000) + 1; const modules = ['用户管理', '角色管理', '菜单管理', '权限管理', '操作日志', '系统设置']; const operations = ['查询', '新增', '修改', '删除', '导出', '导入', '登录', '登出']; const methods = ['GET', 'POST', 'PUT', 'DELETE']; const paths = ['/sys/user/query', '/sys/user/create', '/sys/user/update', '/sys/user/delete', '/sys/role/query', '/sys/role/create', '/sys/menu/query', '/sys/operationLog/query']; const statuses = ['success', 'error']; const status = statuses[Math.floor(Math.random() * statuses.length)]; const isError = status === 'error'; return { id: logId, userId: Math.floor(Math.random() * 10) + 1, username: `user${Math.floor(Math.random() * 10) + 1}`, module: modules[Math.floor(Math.random() * modules.length)], operation: operations[Math.floor(Math.random() * operations.length)], method: methods[Math.floor(Math.random() * methods.length)], path: paths[Math.floor(Math.random() * paths.length)], params: JSON.stringify({ page: Math.floor(Math.random() * 10) + 1, pageSize: 10, username: `user${Math.floor(Math.random() * 10) + 1}` }), ip: `192.168.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`, status, errorMsg: isError ? '操作失败:权限不足或参数错误' : undefined, duration: Math.floor(Math.random() * 1000) + 10, createdAt: new Date(Date.now() - Math.random() * 10000000000).toISOString() }; }; // 生成分页结果 export const generatePageResult = (data: T[], current: number = 1, pageSize: number = 10) => { const start = (current - 1) * pageSize; const end = start + pageSize; const records = data.slice(start, end); return { records, total: data.length, current, size: pageSize }; }; // 模拟成功响应 export const successResponse = (data: T, message: string = 'Success'): Result => { return { code: '200', message, data }; }; // 模拟错误响应 export const errorResponse = (code: string = '500', message: string = 'Error'): Result => { return { code, message, data: null as unknown as T }; }; // 生成随机延迟 export const randomDelay = (min: number = 200, max: number = 800) => { const delay = Math.random() * (max - min) + min; return new Promise(resolve => setTimeout(resolve, delay)); };