const express = require('express'); const { body } = require('express-validator'); const { authMiddleware } = require('../middleware/auth'); const { getEmployees, getEmployeeById, createEmployee, updateEmployee, resetPassword, deleteEmployee, batchUpdateStatus, getEmployeeStats } = require('../controllers/employeeController'); const router = express.Router(); // 验证规则 const createEmployeeValidation = [ body('employeeNumber') .notEmpty() .withMessage('员工编号不能为空') .isLength({ min: 3, max: 20 }) .withMessage('员工编号长度在3-20个字符'), body('name') .notEmpty() .withMessage('员工姓名不能为空') .isLength({ min: 2, max: 50 }) .withMessage('员工姓名长度在2-50个字符'), body('phone') .notEmpty() .withMessage('联系电话不能为空') .matches(/^1[3-9]\d{9}$/) .withMessage('请输入正确的手机号码'), body('email') .optional() .isEmail() .withMessage('请输入正确的邮箱地址'), body('password') .optional() .isLength({ min: 6, max: 20 }) .withMessage('密码长度在6-20个字符'), body('isLoanSpecialist') .optional() .isBoolean() .withMessage('贷款专员标识必须是布尔值'), body('department') .optional() .isLength({ max: 50 }) .withMessage('部门名称不能超过50个字符'), body('position') .optional() .isLength({ max: 50 }) .withMessage('职位名称不能超过50个字符') ]; const updateEmployeeValidation = [ body('name') .optional() .isLength({ min: 2, max: 50 }) .withMessage('员工姓名长度在2-50个字符'), body('phone') .optional() .matches(/^1[3-9]\d{9}$/) .withMessage('请输入正确的手机号码'), body('email') .optional() .isEmail() .withMessage('请输入正确的邮箱地址'), body('isLoanSpecialist') .optional() .isBoolean() .withMessage('贷款专员标识必须是布尔值'), body('department') .optional() .isLength({ max: 50 }) .withMessage('部门名称不能超过50个字符'), body('position') .optional() .isLength({ max: 50 }) .withMessage('职位名称不能超过50个字符'), body('status') .optional() .isIn(['active', 'inactive', 'locked']) .withMessage('状态值无效') ]; const resetPasswordValidation = [ body('newPassword') .optional() .isLength({ min: 6, max: 20 }) .withMessage('密码长度在6-20个字符') ]; const batchUpdateStatusValidation = [ body('ids') .isArray({ min: 1 }) .withMessage('请选择要更新的员工'), body('status') .isIn(['active', 'inactive', 'locked']) .withMessage('状态值无效') ]; // 应用认证中间件 router.use(authMiddleware); // 路由定义 router.get('/', getEmployees); // 获取员工列表 router.get('/stats', getEmployeeStats); // 获取员工统计 router.get('/:id', getEmployeeById); // 获取员工详情 router.post('/', createEmployeeValidation, createEmployee); // 创建员工 router.put('/:id', updateEmployeeValidation, updateEmployee); // 更新员工信息 router.put('/:id/reset-password', resetPasswordValidation, resetPassword); // 重设密码 router.delete('/:id', deleteEmployee); // 删除员工 router.put('/batch/status', batchUpdateStatusValidation, batchUpdateStatus); // 批量更新状态 module.exports = router;