151 lines
6.5 KiB
TypeScript
151 lines
6.5 KiB
TypeScript
const familyNames = ['赵', '冯', '钱', '刘', '孙', '李', '周', '吴', '郑', '王', '陈', '杨', '黄', '张', '林', '何', '高', '马', '罗', '梁'];
|
|
const givenNames = ['玉岚', '艺莲', '若蓉', '思钰', '婷雅', '恩铭', '浩然', '子涵', '欣怡', '梓萱', '宇轩', '雨桐', '佳琪', '思远', '天乐', '晓峰', '文博', '嘉欣', '雅静', '明辉'];
|
|
|
|
const colleges = ['设计学院', '管理学院', '艺术学院'];
|
|
const deptMap: Record<string, string[]> = {
|
|
'设计学院': ['视觉传达设计系', '环境设计系'],
|
|
'管理学院': ['工商管理系', '会计系', '人力资源系'],
|
|
'艺术学院': ['音乐系', '美术系'],
|
|
};
|
|
|
|
const classMap: Record<string, string[]> = {
|
|
'工商管理系': ['工商管理1班', '工商管理2班'],
|
|
'会计系': ['财务管理1班', '财务管理2班'],
|
|
'人力资源系': ['人力资源1班'],
|
|
'视觉传达设计系': ['平面设计1班', '平面设计2班'],
|
|
'环境设计系': ['室内设计1班', '室内设计2班', '室内设计3班'],
|
|
'音乐系': ['声乐表演1班'],
|
|
'美术系': ['国画1班', '油画1班'],
|
|
};
|
|
|
|
const statuses: Array<'normal' | 'disabled'> = ['normal', 'normal', 'normal', 'normal', 'disabled'];
|
|
|
|
function randomPick<T>(arr: T[]): T {
|
|
return arr[Math.floor(Math.random() * arr.length)];
|
|
}
|
|
|
|
function maskPhone(phone: string) {
|
|
return phone.substring(0, 3) + '****' + phone.substring(7);
|
|
}
|
|
|
|
// 生成100条教职工数据
|
|
const staffList: any[] = [];
|
|
for (let i = 0; i < 100; i++) {
|
|
const college = randomPick(colleges);
|
|
const depts = deptMap[college] || [];
|
|
const dept = randomPick(depts);
|
|
const classes = classMap[dept] || [];
|
|
const cls = classes.length > 0 ? randomPick(classes) : '';
|
|
const status = randomPick(statuses);
|
|
const phone = `1${randomPick(['39', '56', '78', '38', '59'])}${String(Math.floor(Math.random() * 100000000)).padStart(8, '0')}`;
|
|
|
|
staffList.push({
|
|
id: String(i + 1),
|
|
name: randomPick(familyNames) + randomPick(givenNames),
|
|
phone,
|
|
maskedPhone: maskPhone(phone),
|
|
status,
|
|
college,
|
|
dept,
|
|
className: cls,
|
|
});
|
|
}
|
|
|
|
// 学院树结构(用于左侧导航)
|
|
const collegeTree = colleges.map((c) => ({
|
|
id: c,
|
|
name: c,
|
|
children: (deptMap[c] || []).map((d) => ({
|
|
id: d,
|
|
name: d,
|
|
children: (classMap[d] || []).map((cls) => ({
|
|
id: cls,
|
|
name: cls,
|
|
})),
|
|
})),
|
|
}));
|
|
|
|
export default {
|
|
'GET /api/admin/staff': (req: any, res: any) => {
|
|
const { page = '1', pageSize = '10', name, phone, college, dept, className, status } = req.query;
|
|
let filtered = [...staffList];
|
|
if (name) filtered = filtered.filter((s) => s.name.includes(name));
|
|
if (phone) filtered = filtered.filter((s) => s.phone.includes(phone));
|
|
if (college) filtered = filtered.filter((s) => s.college === college);
|
|
if (dept) filtered = filtered.filter((s) => s.dept === dept);
|
|
if (className) filtered = filtered.filter((s) => s.className === className);
|
|
if (status) filtered = filtered.filter((s) => s.status === status);
|
|
|
|
const p = Number(page);
|
|
const ps = Number(pageSize);
|
|
const start = (p - 1) * ps;
|
|
res.json({ code: 0, data: { list: filtered.slice(start, start + ps), total: filtered.length, page: p, pageSize: ps }, message: 'ok' });
|
|
},
|
|
|
|
'GET /api/admin/staff/college-tree': (_req: any, res: any) => {
|
|
res.json({ code: 0, data: collegeTree, message: 'ok' });
|
|
},
|
|
|
|
'POST /api/admin/staff/add': (req: any, res: any) => {
|
|
const { name, phone, college, dept, className } = req.body;
|
|
if (!name || !phone) { res.json({ code: 40000, message: '请填写完整信息' }); return; }
|
|
const newStaff = {
|
|
id: String(staffList.length + 1000),
|
|
name, phone, maskedPhone: maskPhone(phone), status: 'normal' as const,
|
|
college: college || '', dept: dept || '', className: className || '',
|
|
};
|
|
staffList.unshift(newStaff);
|
|
res.json({ code: 0, data: newStaff, message: '新增成功' });
|
|
},
|
|
|
|
'POST /api/admin/staff/edit': (req: any, res: any) => {
|
|
const { id, name, phone, college, dept, className } = req.body;
|
|
const staff = staffList.find((s) => s.id === id);
|
|
if (!staff) { res.json({ code: 40000, message: '教职工不存在' }); return; }
|
|
Object.assign(staff, { name, phone, maskedPhone: maskPhone(phone), college, dept, className });
|
|
res.json({ code: 0, data: staff, message: '编辑成功' });
|
|
},
|
|
|
|
'POST /api/admin/staff/toggle-status': (req: any, res: any) => {
|
|
const { id } = req.body;
|
|
const staff = staffList.find((s) => s.id === id);
|
|
if (staff) { staff.status = staff.status === 'normal' ? 'disabled' : 'normal'; res.json({ code: 0, message: '操作成功' }); }
|
|
else { res.json({ code: 40000, message: '教职工不存在' }); }
|
|
},
|
|
|
|
'POST /api/admin/staff/delete': (req: any, res: any) => {
|
|
const { id } = req.body;
|
|
const idx = staffList.findIndex((s) => s.id === id);
|
|
if (idx > -1) { staffList.splice(idx, 1); res.json({ code: 0, message: '删除成功' }); }
|
|
else { res.json({ code: 40000, message: '教职工不存在' }); }
|
|
},
|
|
|
|
'POST /api/admin/staff/batch-delete': (req: any, res: any) => {
|
|
const { ids } = req.body;
|
|
if (!ids || !Array.isArray(ids) || ids.length === 0) { res.json({ code: 40000, message: '请选择要删除的教职工' }); return; }
|
|
let count = 0;
|
|
for (let i = staffList.length - 1; i >= 0; i--) { if (ids.includes(staffList[i].id)) { staffList.splice(i, 1); count++; } }
|
|
res.json({ code: 0, data: { count }, message: `成功删除${count}名教职工` });
|
|
},
|
|
|
|
'POST /api/admin/staff/batch-import': (_req: any, res: any) => {
|
|
const imported: any[] = [];
|
|
for (let i = 0; i < 5; i++) {
|
|
const college = randomPick(colleges);
|
|
const dept = randomPick(deptMap[college] || []);
|
|
const phone = `1${randomPick(['39', '56', '78', '38', '59'])}${String(Math.floor(Math.random() * 100000000)).padStart(8, '0')}`;
|
|
const s = { id: String(Date.now() + i), name: randomPick(familyNames) + randomPick(givenNames), phone, maskedPhone: maskPhone(phone), status: 'normal' as const, college, dept, className: '' };
|
|
staffList.unshift(s);
|
|
imported.push(s);
|
|
}
|
|
res.json({ code: 0, data: { count: imported.length }, message: `成功导入${imported.length}名教职工` });
|
|
},
|
|
|
|
'POST /api/admin/staff/reset-password': (req: any, res: any) => {
|
|
const { id } = req.body;
|
|
const staff = staffList.find((s) => s.id === id);
|
|
if (staff) { res.json({ code: 0, message: '密码已重置为 123456' }); }
|
|
else { res.json({ code: 40000, message: '教职工不存在' }); }
|
|
},
|
|
};
|