feat: 添加用户管理模块 + username/email 唯一约束

- 新增 UserController,实现完整的用户 CRUD 接口
- 扩展 SysUserService 接口和实现类
- 为 sys_user 表添加 username 和 email 唯一约束
- 添加 phone、role_type、status 索引优化查询性能
- 修复 Result 类方法名(failed -> error)

相关接口:
- GET /api/users - 获取用户列表(分页)
- GET /api/users/:id - 获取用户详情
- POST /api/users - 创建用户
- PUT /api/users/:id - 更新用户
- DELETE /api/users/:id - 删除用户
- GET /api/users/teachers - 获取教师列表
- POST /api/users/change-password - 修改密码
- PUT /api/users/profile - 更新个人资料
This commit is contained in:
likingcode
2026-03-13 10:31:24 +08:00
parent c6a845f405
commit a67496694c
56 changed files with 501 additions and 21 deletions

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

View File

Binary file not shown.

Binary file not shown.

View File

@@ -1,38 +1,38 @@
-- 预埋测试数据
-- 1. 管理员账号 (密码: admin123)
INSERT INTO sys_user (username, password, real_name, gender, role_type, status) VALUES
INSERT IGNORE INTO sys_user (username, password, real_name, gender, role_type, status) VALUES
('admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt9hQIu', '系统管理员', 1, 3, 1);
-- 2. 教师账号 (密码: teacher123)
INSERT INTO sys_user (username, password, real_name, gender, role_type, status) VALUES
INSERT IGNORE INTO sys_user (username, password, real_name, gender, role_type, status) VALUES
('teacher001', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt9hQIu', '张教授', 1, 2, 1),
('teacher002', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt9hQIu', '李老师', 2, 2, 1);
-- 3. 学生账号 (密码: student123)
INSERT INTO sys_user (username, password, real_name, gender, role_type, status) VALUES
INSERT IGNORE INTO sys_user (username, password, real_name, gender, role_type, status) VALUES
('student001', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt9hQIu', '王小明', 1, 1, 1),
('student002', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt9hQIu', '李小红', 2, 1, 1),
('student003', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt9hQIu', '张小华', 1, 1, 1);
-- 4. 教师信息
INSERT INTO teacher_info (user_id, teacher_no, college, title, research_field) VALUES
INSERT IGNORE INTO teacher_info (user_id, teacher_no, college, title, research_field) VALUES
(2, 'T2024001', '计算机学院', '教授', '人工智能'),
(3, 'T2024002', '计算机学院', '副教授', '大数据');
-- 5. 学生信息
INSERT INTO stu_info (user_id, student_no, college, major, grade, class_name, advisor_id) VALUES
INSERT IGNORE INTO stu_info (user_id, student_no, college, major, grade, class_name, advisor_id) VALUES
(4, 'S2021001', '计算机学院', '计算机科学与技术', '2021', '计科2101', 2),
(5, 'S2021002', '计算机学院', '软件工程', '2021', '软工2101', 2),
(6, 'S2022001', '计算机学院', '计算机科学与技术', '2022', '计科2201', 3);
-- 6. 测试项目
INSERT INTO project (project_no, project_name, project_type, project_level, leader_id, advisor_id, description, status) VALUES
INSERT IGNORE INTO project (project_no, project_name, project_type, project_level, leader_id, advisor_id, description, status) VALUES
('PRJ2024001', '基于AI的智能问答系统', 1, 2, 4, 2, '本项目旨在开发一个基于大语言模型的智能问答系统', 3),
('PRJ2024002', '校园二手交易平台', 2, 1, 5, 3, '搭建一个面向高校学生的二手物品交易平台', 1);
-- 7. 项目成员
INSERT INTO project_member (project_id, user_id, member_order, role) VALUES
INSERT IGNORE INTO project_member (project_id, user_id, member_order, role) VALUES
(1, 4, 1, 2),
(1, 5, 2, 1),
(2, 5, 1, 2),

View File

@@ -0,0 +1,40 @@
-- V2: 为 sys_user 表添加唯一约束
-- 执行时间2026-03-13
-- 1. 先清理可能存在的重复数据(保留 ID 最小的记录)
-- 删除重复的用户名(保留 id 最小的)
DELETE FROM sys_user
WHERE id NOT IN (
SELECT id FROM (
SELECT MIN(id) as id
FROM sys_user
WHERE username IS NOT NULL
GROUP BY username
) as tmp
);
-- 删除重复的邮箱(保留 id 最小的)
DELETE FROM sys_user
WHERE id NOT IN (
SELECT id FROM (
SELECT MIN(id) as id
FROM sys_user
WHERE email IS NOT NULL
GROUP BY email
) as tmp
);
-- 2. 为 username 添加唯一约束
ALTER TABLE sys_user
ADD CONSTRAINT uk_username UNIQUE (username);
-- 3. 为 email 添加唯一约束
ALTER TABLE sys_user
ADD CONSTRAINT uk_email UNIQUE (email);
-- 4. 为 phone 添加索引(不唯一,因为可能有没有手机号的情况)
CREATE INDEX idx_phone ON sys_user(phone);
-- 5. 为 role_type 和 status 添加索引,优化查询性能
CREATE INDEX idx_role_type ON sys_user(role_type);
CREATE INDEX idx_status ON sys_user(status);

View File

@@ -16,7 +16,11 @@ CREATE TABLE IF NOT EXISTS sys_user (
update_by BIGINT DEFAULT NULL,
deleted TINYINT DEFAULT 0,
PRIMARY KEY (id),
UNIQUE KEY uk_username (username)
UNIQUE KEY uk_username (username),
UNIQUE KEY uk_email (email),
KEY idx_phone (phone),
KEY idx_role_type (role_type),
KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 创建学生信息表