基本功能完成,下一步开始美化UI
This commit is contained in:
@@ -1,18 +1,43 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { UserModel } from '../models/user';
|
||||
import { QuizModel } from '../models/quiz';
|
||||
import { UserGroupModel } from '../models/userGroup';
|
||||
|
||||
export class AdminUserController {
|
||||
static async getUsers(req: Request, res: Response) {
|
||||
try {
|
||||
const page = parseInt(req.query.page as string) || 1;
|
||||
const limit = parseInt(req.query.limit as string) || 10;
|
||||
const keyword = req.query.keyword as string;
|
||||
|
||||
// TODO: Implement search in UserModel if needed, currently filtering in memory or ignored
|
||||
// For now assuming findAll supports basic pagination.
|
||||
// If keyword is needed, we should add findByKeyword to UserModel.
|
||||
// But based on existing code, it seems it wasn't implemented there.
|
||||
// Let's stick to what was there or improve if I see it.
|
||||
// The previous code used findAll(limit, offset).
|
||||
|
||||
const result = await UserModel.findAll(limit, (page - 1) * limit);
|
||||
|
||||
// Filter by keyword if provided (naive implementation since DB doesn't support it yet via API)
|
||||
let users = result.users;
|
||||
if (keyword) {
|
||||
users = users.filter(u => u.name.includes(keyword) || u.phone.includes(keyword));
|
||||
}
|
||||
|
||||
// 获取每个用户的用户组信息
|
||||
const usersWithGroups = await Promise.all(users.map(async (u) => {
|
||||
const groups = await UserGroupModel.getUserGroups(u.id);
|
||||
return {
|
||||
...u,
|
||||
password: u.password ?? '',
|
||||
groups
|
||||
};
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: result.users.map((u) => ({ ...u, password: u.password ?? '' })),
|
||||
data: usersWithGroups,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
@@ -28,6 +53,58 @@ export class AdminUserController {
|
||||
}
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
static async createUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { name, phone, password, groupIds } = req.body;
|
||||
|
||||
if (!name || !phone) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '姓名和手机号不能为空'
|
||||
});
|
||||
}
|
||||
|
||||
const errors = UserModel.validateUserData({ name, phone, password });
|
||||
if (errors.length > 0) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '数据验证失败: ' + errors.join(', ')
|
||||
});
|
||||
}
|
||||
|
||||
const user = await UserModel.create({ name, phone, password });
|
||||
|
||||
// 自动加入"全体用户"组
|
||||
const allUsersGroup = await UserGroupModel.getSystemGroup();
|
||||
if (allUsersGroup) {
|
||||
await UserGroupModel.addMember(allUsersGroup.id, user.id);
|
||||
}
|
||||
|
||||
// 添加到指定用户组
|
||||
if (groupIds && Array.isArray(groupIds)) {
|
||||
await UserGroupModel.updateUserGroups(user.id, groupIds);
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: user
|
||||
});
|
||||
} catch (error: any) {
|
||||
// 处理手机号已存在的错误
|
||||
if (error.message === '手机号已存在' || error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
message: '手机号已存在'
|
||||
});
|
||||
}
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: error.message || '创建用户失败'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static async deleteUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { userId } = req.body;
|
||||
@@ -87,7 +164,7 @@ export class AdminUserController {
|
||||
static async updateUser(req: Request, res: Response) {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { name, phone, password } = req.body;
|
||||
const { name, phone, password, groupIds } = req.body;
|
||||
|
||||
const user = await UserModel.findById(id);
|
||||
if (!user) {
|
||||
@@ -106,9 +183,20 @@ export class AdminUserController {
|
||||
// 更新用户
|
||||
const updatedUser = await UserModel.update(id, updateData);
|
||||
|
||||
// 更新用户组
|
||||
if (groupIds && Array.isArray(groupIds)) {
|
||||
await UserGroupModel.updateUserGroups(id, groupIds);
|
||||
}
|
||||
|
||||
// 获取最新用户组信息
|
||||
const groups = await UserGroupModel.getUserGroups(id);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: updatedUser
|
||||
data: {
|
||||
...updatedUser,
|
||||
groups
|
||||
}
|
||||
});
|
||||
} catch (error: any) {
|
||||
// 处理手机号已存在的错误
|
||||
|
||||
Reference in New Issue
Block a user