import Mock from 'mockjs' import { SUCCESS_CODE } from '@/constants' const timeout = 1000 // 学生模拟数据 const studentList: any[] = [] // 生成100条模拟数据 for (let i = 1; i <= 100; i++) { studentList.push({ id: `student_${i}`, userId: `user_${i}`, classId: `class_${Mock.Random.integer(1, 10)}`, className: `计算机${Mock.Random.integer(1, 10)}班`, studentNo: `S${String(i).padStart(8, '0')}`, realName: Mock.Random.cname(), gender: Mock.Random.pick([1, 2]), phone: Mock.Random.pick([ '13800138000', '13900139000', '15000150000', '15100151000', '18600186000' ]), status: Mock.Random.pick([0, 1, 2]), createdTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'), updatedTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss') }) } export default [ // 查询学生列表 { url: '/api/students', method: 'get', timeout, response: ({ query }: any) => { const { pageNo = 1, pageSize = 10, studentNo, realName, classId, status } = query let filteredList = studentList // 学号模糊查询 if (studentNo) { filteredList = filteredList.filter((item) => item.studentNo.toLowerCase().includes(studentNo.toLowerCase()) ) } // 真实姓名模糊查询 if (realName) { filteredList = filteredList.filter((item) => item.realName.toLowerCase().includes(realName.toLowerCase()) ) } // 班级ID精确查询 if (classId) { filteredList = filteredList.filter((item) => item.classId === classId) } // 状态精确查询 if (status !== undefined && status !== null && status !== '') { filteredList = filteredList.filter((item) => item.status === Number(status)) } const totals = filteredList.length const totalPages = Math.ceil(totals / pageSize) const startIndex = (pageNo - 1) * pageSize const endIndex = startIndex + Number(pageSize) const list = filteredList.slice(startIndex, endIndex) return { code: SUCCESS_CODE, data: { totals, totalPages, list } } } }, // 新增学生 { url: '/api/students', method: 'post', timeout, response: ({ body }: any) => { const newStudent = { id: `student_${studentList.length + 1}`, userId: `user_${studentList.length + 1}`, ...body, className: `计算机${Mock.Random.integer(1, 10)}班`, status: 1, createdTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'), updatedTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss') } studentList.unshift(newStudent) return { code: SUCCESS_CODE, data: newStudent.id, msg: '学生添加成功' } } }, // 获取学生详情 { url: '/api/students/:studentId', method: 'get', timeout, response: ({ query }: any) => { const { studentId } = query const student = studentList.find((item) => item.id === studentId) if (student) { return { code: SUCCESS_CODE, data: { id: student.id, userId: student.userId, classId: student.classId, classCode: `CS${Mock.Random.integer(100, 999)}`, majorId: `major_${Mock.Random.integer(1, 5)}`, studentNo: student.studentNo, realName: student.realName, gender: student.gender, phone: student.phone } } } else { return { code: 404, msg: '学生不存在' } } } }, // 编辑学生信息 { url: '/api/students/:studentId', method: 'put', timeout, response: ({ query, body }: any) => { const { studentId } = query const index = studentList.findIndex((item) => item.id === studentId) if (index !== -1) { studentList[index] = { ...studentList[index], ...body, updatedTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss') } return { code: SUCCESS_CODE, data: null, msg: '学生信息修改成功' } } else { return { code: 404, msg: '学生不存在' } } } }, // 删除学生 { url: '/api/students/:studentId', method: 'delete', timeout, response: ({ query }: any) => { const { studentId } = query const index = studentList.findIndex((item) => item.id === studentId) if (index !== -1) { studentList.splice(index, 1) return { code: SUCCESS_CODE, data: null, msg: '学生删除成功' } } else { return { code: 404, msg: '学生不存在' } } } }, // 启用学生 { url: '/api/students/:studentId/enable', method: 'put', timeout, response: ({ query }: any) => { const { studentId } = query const student = studentList.find((item) => item.id === studentId) if (student) { student.status = 1 student.updatedTime = Mock.Random.datetime('yyyy-MM-dd HH:mm:ss') return { code: SUCCESS_CODE, data: null, msg: '学生启用成功' } } else { return { code: 404, msg: '学生不存在' } } } }, // 禁用学生 { url: '/api/students/:studentId/disable', method: 'put', timeout, response: ({ query }: any) => { const { studentId } = query const student = studentList.find((item) => item.id === studentId) if (student) { student.status = 0 student.updatedTime = Mock.Random.datetime('yyyy-MM-dd HH:mm:ss') return { code: SUCCESS_CODE, data: null, msg: '学生禁用成功' } } else { return { code: 404, msg: '学生不存在' } } } }, // Excel批量导入学生 { url: '/api/students/import', method: 'post', timeout: 2000, response: () => { // 模拟导入结果 const successCount = Mock.Random.integer(10, 20) const failureCount = Mock.Random.integer(0, 5) const errors: any[] = [] // 生成错误明细 for (let i = 0; i < failureCount; i++) { errors.push({ rowNo: Mock.Random.integer(2, 30), errorMsg: Mock.Random.pick([ '学号不能为空', '真实姓名不能为空', '班级代码不存在', '学号已存在', '手机号格式不正确', '性别格式不正确(应为1或2)' ]), errorData: `S${Mock.Random.integer(10000000, 99999999)},${Mock.Random.cname()},CS${Mock.Random.integer(100, 999)},${Mock.Random.pick([1, 2])},1380013800${Mock.Random.integer(0, 9)}` }) } return { code: SUCCESS_CODE, data: { success: failureCount === 0, totalCount: successCount + failureCount, successCount, failureCount, errors, message: failureCount === 0 ? `成功导入 ${successCount} 条学生数据` : `导入完成,成功 ${successCount} 条,失败 ${failureCount} 条` } } } } ]