feat: 添加部门管理功能、主题切换和多语言支持

refactor(dashboard): 重构用户管理页面和路由结构

feat(server): 实现部门管理API和RBAC增强功能

docs: 更新用户手册和管理员指南文档

style: 统一图标使用和组件命名规范

test: 添加部门服务和数据隔离测试用例

chore: 更新依赖和配置文件
This commit is contained in:
2026-03-28 22:52:12 +08:00
parent 22308fe042
commit d327706087
87 changed files with 21372 additions and 4806 deletions

View File

@@ -0,0 +1,169 @@
import { Request, Response } from 'express';
import { HierarchyService } from '../../services/tenant/HierarchyService';
import { logger } from '../../utils/logger';
export class DepartmentController {
static async createDepartment(req: Request, res: Response) {
try {
const { tenantId } = req.user;
const { name, parentId, managerId } = req.body;
if (!name) {
return res.status(400).json({ success: false, error: '部门名称不能为空' });
}
const department = await HierarchyService.createDepartment(
tenantId,
name,
parentId || null,
managerId
);
res.json({ success: true, data: department });
} catch (error: unknown) {
logger.error('[DepartmentController] Create department failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '创建部门失败'
});
}
}
static async updateDepartment(req: Request, res: Response) {
try {
const { tenantId } = req.user;
const { id } = req.params;
const updates = req.body;
const department = await HierarchyService.updateDepartment(
id,
tenantId,
updates
);
res.json({ success: true, data: department });
} catch (error: unknown) {
logger.error('[DepartmentController] Update department failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '更新部门失败'
});
}
}
static async deleteDepartment(req: Request, res: Response) {
try {
const { tenantId } = req.user;
const { id } = req.params;
await HierarchyService.deleteDepartment(id, tenantId);
res.json({ success: true, message: '部门删除成功' });
} catch (error: unknown) {
logger.error('[DepartmentController] Delete department failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '删除部门失败'
});
}
}
static async getDepartmentTree(req: Request, res: Response) {
try {
const { tenantId } = req.user;
const tree = await HierarchyService.getDepartmentTree(tenantId);
res.json({ success: true, data: tree });
} catch (error: unknown) {
logger.error('[DepartmentController] Get department tree failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '获取部门树失败'
});
}
}
static async assignManager(req: Request, res: Response) {
try {
const { tenantId, id: assignedBy } = req.user;
const { departmentId } = req.params;
const { managerId } = req.body;
if (!managerId) {
return res.status(400).json({ success: false, error: '负责人ID不能为空' });
}
const department = await HierarchyService.assignDepartmentManager(
departmentId,
tenantId,
managerId,
assignedBy
);
res.json({ success: true, data: department });
} catch (error: unknown) {
logger.error('[DepartmentController] Assign manager failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '设置部门负责人失败'
});
}
}
static async getManager(req: Request, res: Response) {
try {
const { tenantId } = req.user;
const { departmentId } = req.params;
const manager = await HierarchyService.getDepartmentManager(
departmentId,
tenantId
);
res.json({ success: true, data: manager });
} catch (error: unknown) {
logger.error('[DepartmentController] Get manager failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '获取部门负责人失败'
});
}
}
static async getDepartmentStats(req: Request, res: Response) {
try {
const { tenantId } = req.user;
const { departmentId } = req.params;
const stats = await HierarchyService.getDepartmentStats(
departmentId,
tenantId
);
res.json({ success: true, data: stats });
} catch (error: unknown) {
logger.error('[DepartmentController] Get department stats failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '获取部门统计失败'
});
}
}
static async getHierarchyStats(req: Request, res: Response) {
try {
const { tenantId } = req.user;
const stats = await HierarchyService.getHierarchyStats(tenantId);
res.json({ success: true, data: stats });
} catch (error: unknown) {
logger.error('[DepartmentController] Get hierarchy stats failed', error);
res.status(500).json({
success: false,
error: error instanceof Error ? error.message : '获取层级统计失败'
});
}
}
}