Files
certd/packages/ui/certd-server/src/controller/user/login/register-controller.ts
T

97 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-10-03 22:03:49 +08:00
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
2024-11-28 17:36:45 +08:00
import { BaseController, Constants, SysSettingsService } from '@certd/lib-server';
2025-01-15 01:05:34 +08:00
import { RegisterType, UserService } from '../../../modules/sys/authority/service/user-service.js';
import { CodeService } from '../../../modules/basic/service/code-service.js';
2024-11-30 01:57:09 +08:00
import { checkComm, checkPlus } from '@certd/plus-core';
2024-11-28 17:36:45 +08:00
export type RegisterReq = {
type: RegisterType;
username: string;
password: string;
mobile: string;
email: string;
phoneCode?: string;
validateCode: string;
imgCode: string;
2024-11-28 17:36:45 +08:00
randomStr: string;
};
2023-06-27 09:29:43 +08:00
/**
*/
@Provide()
@Controller('/api/')
export class RegisterController extends BaseController {
@Inject()
userService: UserService;
2024-11-28 17:36:45 +08:00
@Inject()
codeService: CodeService;
@Inject()
sysSettingsService: SysSettingsService;
2023-06-27 09:29:43 +08:00
@Post('/register', { summary: Constants.per.guest })
public async register(
@Body(ALL)
2024-11-28 17:36:45 +08:00
body: RegisterReq
2023-06-27 09:29:43 +08:00
) {
const sysPublicSettings = await this.sysSettingsService.getPublicSettings();
if (sysPublicSettings.registerEnabled === false) {
throw new Error('当前站点已禁止自助注册功能');
}
2024-11-28 17:36:45 +08:00
if (body.type === 'username') {
if (sysPublicSettings.usernameRegisterEnabled === false) {
2024-11-28 17:36:45 +08:00
throw new Error('当前站点已禁止用户名注册功能');
}
2025-08-07 10:28:21 +08:00
if (!body.username) {
throw new Error('用户名不能为空');
}
if (body.username in ["admin","certd"]) {
throw new Error('用户名不能为保留字');
}
await this.codeService.checkCaptcha(body.randomStr, body.imgCode);
2024-11-28 17:36:45 +08:00
const newUser = await this.userService.register(body.type, {
username: body.username,
password: body.password,
} as any);
return this.ok(newUser);
} else if (body.type === 'mobile') {
if (sysPublicSettings.mobileRegisterEnabled === false) {
2024-11-28 17:36:45 +08:00
throw new Error('当前站点已禁止手机号注册功能');
}
2024-11-30 01:57:09 +08:00
checkComm();
2024-11-28 17:36:45 +08:00
//验证短信验证码
await this.codeService.checkSmsCode({
mobile: body.mobile,
phoneCode: body.phoneCode,
smsCode: body.validateCode,
randomStr: body.randomStr,
throwError: true,
});
const newUser = await this.userService.register(body.type, {
phoneCode: body.phoneCode,
mobile: body.mobile,
password: body.password,
} as any);
return this.ok(newUser);
} else if (body.type === 'email') {
if (sysPublicSettings.emailRegisterEnabled === false) {
throw new Error('当前站点已禁止Email注册功能');
}
2024-11-30 01:57:09 +08:00
checkPlus();
2024-11-28 17:36:45 +08:00
this.codeService.checkEmailCode({
email: body.email,
randomStr: body.randomStr,
validateCode: body.validateCode,
throwError: true,
});
const newUser = await this.userService.register(body.type, {
email: body.email,
password: body.password,
} as any);
return this.ok(newUser);
}
2023-06-27 09:29:43 +08:00
}
}