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

104 lines
3.3 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { ALL, Body, Controller, Inject, Post, Provide, RequestIP } from "@midwayjs/core";
import { BaseController, Constants, SysSettingsService } from "@certd/lib-server";
2026-06-04 18:22:21 +08:00
import { RegisterType } from "../../../modules/sys/authority/service/user-service.js";
2026-05-31 01:41:33 +08:00
import { CodeService } from "../../../modules/basic/service/code-service.js";
import { checkComm, checkPlus } from "@certd/plus-core";
2026-06-04 18:22:21 +08:00
import { LoginService } from "../../../modules/login/service/login-service.js";
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;
2026-05-31 01:41:33 +08:00
captcha: any;
2026-05-18 13:25:35 +08:00
inviteCode?: string;
2024-11-28 17:36:45 +08:00
};
2023-06-27 09:29:43 +08:00
/**
*/
@Provide()
2026-05-31 01:41:33 +08:00
@Controller("/api/")
2023-06-27 09:29:43 +08:00
export class RegisterController extends BaseController {
@Inject()
2026-06-04 18:22:21 +08:00
loginService: LoginService;
2024-11-28 17:36:45 +08:00
@Inject()
codeService: CodeService;
@Inject()
sysSettingsService: SysSettingsService;
2026-05-31 01:41:33 +08:00
@Post("/register", { description: Constants.per.guest })
2023-06-27 09:29:43 +08:00
public async register(
@Body(ALL)
body: RegisterReq,
@RequestIP() remoteIp: string
2023-06-27 09:29:43 +08:00
) {
const sysPublicSettings = await this.sysSettingsService.getPublicSettings();
if (sysPublicSettings.registerEnabled === false) {
2026-05-31 01:41:33 +08:00
throw new Error("当前站点已禁止自助注册功能");
}
2024-11-28 17:36:45 +08:00
2026-05-31 01:41:33 +08:00
if (body.username && ["admin", "certd"].includes(body.username)) {
throw new Error("用户名不能为保留字");
2025-08-07 10:36:34 +08:00
}
2026-05-31 01:41:33 +08:00
if (body.type === "username") {
if (sysPublicSettings.usernameRegisterEnabled === false) {
2026-05-31 01:41:33 +08:00
throw new Error("当前站点已禁止用户名注册功能");
2024-11-28 17:36:45 +08:00
}
2025-08-07 10:28:21 +08:00
if (!body.username) {
2026-05-31 01:41:33 +08:00
throw new Error("用户名不能为空");
2025-08-07 10:28:21 +08:00
}
2025-08-07 10:36:34 +08:00
2026-05-31 01:41:33 +08:00
await this.codeService.checkCaptcha(body.captcha, { remoteIp });
2026-05-18 13:25:35 +08:00
const registerUser = {
2024-11-28 17:36:45 +08:00
username: body.username,
password: body.password,
2026-05-18 13:25:35 +08:00
} as any;
2026-06-04 18:22:21 +08:00
const newUser = await this.loginService.register(body.type, registerUser, body.inviteCode);
2024-11-28 17:36:45 +08:00
return this.ok(newUser);
2026-05-31 01:41:33 +08:00
} else if (body.type === "mobile") {
if (sysPublicSettings.mobileRegisterEnabled === false) {
2026-05-31 01:41:33 +08:00
throw new Error("当前站点已禁止手机号注册功能");
2024-11-28 17:36:45 +08:00
}
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,
throwError: true,
});
2026-05-18 13:25:35 +08:00
const registerUser = {
2025-08-07 10:36:34 +08:00
username: body.username,
2024-11-28 17:36:45 +08:00
phoneCode: body.phoneCode,
mobile: body.mobile,
password: body.password,
2026-05-18 13:25:35 +08:00
} as any;
2026-06-04 18:22:21 +08:00
const newUser = await this.loginService.register(body.type, registerUser, body.inviteCode);
2024-11-28 17:36:45 +08:00
return this.ok(newUser);
2026-05-31 01:41:33 +08:00
} else if (body.type === "email") {
2024-11-28 17:36:45 +08:00
if (sysPublicSettings.emailRegisterEnabled === false) {
2026-05-31 01:41:33 +08:00
throw new Error("当前站点已禁止Email注册功能");
2024-11-28 17:36:45 +08:00
}
2024-11-30 01:57:09 +08:00
checkPlus();
2024-11-28 17:36:45 +08:00
this.codeService.checkEmailCode({
email: body.email,
validateCode: body.validateCode,
throwError: true,
});
2026-05-18 13:25:35 +08:00
const registerUser = {
2025-08-07 18:52:20 +08:00
username: body.username,
2024-11-28 17:36:45 +08:00
email: body.email,
password: body.password,
2026-05-18 13:25:35 +08:00
} as any;
2026-06-04 18:22:21 +08:00
const newUser = await this.loginService.register(body.type, registerUser, body.inviteCode);
2024-11-28 17:36:45 +08:00
return this.ok(newUser);
}
2023-06-27 09:29:43 +08:00
}
}