增加注册功能

This commit is contained in:
2026-04-12 15:10:22 +08:00
parent 3b1bd94dce
commit 05ee0929e2
21 changed files with 409 additions and 4 deletions

View File

@@ -1,7 +1,7 @@
// 注意此版本使用明文密码验证所有用户密码均为123456仅用于测试环境
// 生产环境必须使用加密密码存储和验证
import jwt from 'jsonwebtoken';
import { findByUsername } from '../dao/UserDAO';
import { findByUsername, createUser, CreateUserInput } from '../dao/UserDAO';
import { JWT_SECRET, JWT_EXPIRES_IN } from '../config/jwt';
import { LoginResult, UserInfo, UserRole } from '../types';
@@ -38,3 +38,53 @@ export async function login(
return { token, userInfo };
}
export interface RegisterInput {
username: string;
password: string;
name: string;
department: string;
position: string;
role?: UserRole;
}
export async function register(userData: RegisterInput): Promise<LoginResult> {
const { username, password, name, department, position, role = 'employee' } = userData;
// 检查必填字段
if (!username || !password || !name || !department || !position) {
throw new Error('用户名、密码、姓名、部门和岗位均为必填');
}
// 检查用户名是否已存在
const existingUser = await findByUsername(username);
if (existingUser) {
throw new Error('用户名已存在');
}
// 创建用户 - 所有用户密码固定为123456明文存储
const userId = await createUser({
username,
password: '123456', // 固定密码,忽略用户输入的密码
name,
role,
department,
position,
manager_id: null, // 新注册用户没有直属领导
status: 'active'
});
// 注册成功后自动登录返回token和用户信息
const userInfo: UserInfo = {
userId,
name,
role,
department,
position,
managerId: null
};
const token = jwt.sign(userInfo, JWT_SECRET, { expiresIn: JWT_EXPIRES_IN });
return { token, userInfo };
}