添加银行政府后端接口

This commit is contained in:
2025-09-25 15:53:44 +08:00
parent b17bdcc24c
commit 5b6b7e0a96
60 changed files with 5345 additions and 1920 deletions

View File

@@ -1,316 +1,116 @@
/**
* 员工路由
* @file employees.js
* @description 员工相关的路由定义
*/
const express = require('express');
const { body } = require('express-validator');
const { authMiddleware, roleMiddleware, adminMiddleware, managerMiddleware } = require('../middleware/auth');
const employeeController = require('../controllers/employeeController');
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);
/**
* @swagger
* tags:
* name: Employees
* description: 员工管理
*/
// 路由定义
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); // 批量更新状态
/**
* @swagger
* /api/employees:
* get:
* summary: 获取员工列表
* tags: [Employees]
* security:
* - bearerAuth: []
* parameters:
* - in: query
* name: page
* schema:
* type: integer
* description: 页码
* - in: query
* name: limit
* schema:
* type: integer
* description: 每页数量
* - in: query
* name: search
* schema:
* type: string
* description: 搜索关键词
* - in: query
* name: department
* schema:
* type: string
* description: 部门筛选
* - in: query
* name: position
* schema:
* type: string
* description: 职位筛选
* - in: query
* name: status
* schema:
* type: string
* enum: [active, inactive, resigned]
* description: 状态筛选
* responses:
* 200:
* description: 获取成功
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* message:
* type: string
* data:
* type: object
* properties:
* employees:
* type: array
* items:
* $ref: '#/components/schemas/Employee'
* pagination:
* $ref: '#/components/schemas/Pagination'
* 401:
* description: 未授权
* 500:
* description: 服务器内部错误
*/
router.get('/', roleMiddleware(['admin', 'manager', 'teller']), employeeController.getEmployees);
/**
* @swagger
* /api/employees:
* post:
* summary: 创建员工
* tags: [Employees]
* security:
* - bearerAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - name
* - employee_id
* - department_id
* - position_id
* - hire_date
* - salary
* properties:
* name:
* type: string
* description: 员工姓名
* employee_id:
* type: string
* description: 员工编号
* department_id:
* type: integer
* description: 部门ID
* position_id:
* type: integer
* description: 职位ID
* phone:
* type: string
* description: 联系电话
* email:
* type: string
* description: 邮箱地址
* hire_date:
* type: string
* format: date
* description: 入职日期
* salary:
* type: number
* description: 薪资
* status:
* type: string
* enum: [active, inactive, resigned]
* description: 员工状态
* responses:
* 201:
* description: 创建成功
* 400:
* description: 请求参数错误
* 401:
* description: 未授权
* 403:
* description: 权限不足
* 500:
* description: 服务器内部错误
*/
router.post('/',
adminMiddleware,
[
body('name').notEmpty().withMessage('员工姓名不能为空'),
body('employee_id').notEmpty().withMessage('员工编号不能为空'),
body('department_id').isInt().withMessage('部门ID必须是整数'),
body('position_id').isInt().withMessage('职位ID必须是整数'),
body('phone').optional().isMobilePhone('zh-CN').withMessage('手机号格式不正确'),
body('email').optional().isEmail().withMessage('邮箱格式不正确'),
body('hire_date').isISO8601().withMessage('入职日期格式不正确'),
body('salary').isNumeric().withMessage('薪资必须是数字'),
body('status').optional().isIn(['active', 'inactive', 'resigned']).withMessage('状态值无效')
],
employeeController.createEmployee
);
/**
* @swagger
* /api/employees/{id}:
* get:
* summary: 获取员工详情
* tags: [Employees]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: 员工ID
* responses:
* 200:
* description: 获取成功
* 404:
* description: 员工不存在
* 401:
* description: 未授权
* 500:
* description: 服务器内部错误
*/
router.get('/:id', roleMiddleware(['admin', 'manager', 'teller']), employeeController.getEmployeeById);
/**
* @swagger
* /api/employees/{id}:
* put:
* summary: 更新员工
* tags: [Employees]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: 员工ID
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* employee_id:
* type: string
* department_id:
* type: integer
* position_id:
* type: integer
* phone:
* type: string
* email:
* type: string
* hire_date:
* type: string
* format: date
* salary:
* type: number
* status:
* type: string
* enum: [active, inactive, resigned]
* responses:
* 200:
* description: 更新成功
* 400:
* description: 请求参数错误
* 404:
* description: 员工不存在
* 401:
* description: 未授权
* 403:
* description: 权限不足
* 500:
* description: 服务器内部错误
*/
router.put('/:id',
adminMiddleware,
[
body('name').optional().notEmpty().withMessage('员工姓名不能为空'),
body('employee_id').optional().notEmpty().withMessage('员工编号不能为空'),
body('department_id').optional().isInt().withMessage('部门ID必须是整数'),
body('position_id').optional().isInt().withMessage('职位ID必须是整数'),
body('phone').optional().isMobilePhone('zh-CN').withMessage('手机号格式不正确'),
body('email').optional().isEmail().withMessage('邮箱格式不正确'),
body('hire_date').optional().isISO8601().withMessage('入职日期格式不正确'),
body('salary').optional().isNumeric().withMessage('薪资必须是数字'),
body('status').optional().isIn(['active', 'inactive', 'resigned']).withMessage('状态值无效')
],
employeeController.updateEmployee
);
/**
* @swagger
* /api/employees/{id}:
* delete:
* summary: 删除员工
* tags: [Employees]
* security:
* - bearerAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: 员工ID
* responses:
* 200:
* description: 删除成功
* 404:
* description: 员工不存在
* 401:
* description: 未授权
* 403:
* description: 权限不足
* 500:
* description: 服务器内部错误
*/
router.delete('/:id', adminMiddleware, employeeController.deleteEmployee);
/**
* @swagger
* /api/employees/stats/overview:
* get:
* summary: 获取员工统计
* tags: [Employees]
* security:
* - bearerAuth: []
* responses:
* 200:
* description: 获取成功
* 401:
* description: 未授权
* 500:
* description: 服务器内部错误
*/
router.get('/stats/overview', roleMiddleware(['admin', 'manager', 'teller']), employeeController.getEmployeeStats);
module.exports = router;
module.exports = router;