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";
|
|
|
|
|
import { RegisterType, UserService } from "../../../modules/sys/authority/service/user-service.js";
|
|
|
|
|
import { CodeService } from "../../../modules/basic/service/code-service.js";
|
|
|
|
|
import { checkComm, checkPlus } from "@certd/plus-core";
|
|
|
|
|
import { InviteService } from "@certd/commercial-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;
|
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()
|
|
|
|
|
userService: UserService;
|
2024-11-28 17:36:45 +08:00
|
|
|
@Inject()
|
|
|
|
|
codeService: CodeService;
|
2024-06-16 00:20:02 +08:00
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
|
sysSettingsService: SysSettingsService;
|
|
|
|
|
|
2026-05-18 13:25:35 +08:00
|
|
|
@Inject()
|
|
|
|
|
inviteService: InviteService;
|
|
|
|
|
|
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)
|
2026-01-29 17:21:39 +08:00
|
|
|
body: RegisterReq,
|
|
|
|
|
@RequestIP() remoteIp: string
|
2023-06-27 09:29:43 +08:00
|
|
|
) {
|
2024-06-16 00:20:02 +08:00
|
|
|
const sysPublicSettings = await this.sysSettingsService.getPublicSettings();
|
|
|
|
|
if (sysPublicSettings.registerEnabled === false) {
|
2026-05-31 01:41:33 +08:00
|
|
|
throw new Error("当前站点已禁止自助注册功能");
|
2024-06-16 00:20:02 +08:00
|
|
|
}
|
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") {
|
2024-11-29 19:00:05 +08:00
|
|
|
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;
|
|
|
|
|
const newUser = await this.userService.register(body.type, registerUser, async txManager => {
|
2026-05-31 01:01:30 +08:00
|
|
|
await this.inviteService.bindInvitee({ manager: txManager }, { inviteeUserId: registerUser.id, inviteCode: body.inviteCode });
|
2026-05-18 13:25:35 +08:00
|
|
|
});
|
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") {
|
2024-11-29 19:00:05 +08:00
|
|
|
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;
|
|
|
|
|
const newUser = await this.userService.register(body.type, registerUser, async txManager => {
|
2026-05-31 01:01:30 +08:00
|
|
|
await this.inviteService.bindInvitee({ manager: txManager }, { inviteeUserId: registerUser.id, inviteCode: body.inviteCode });
|
2026-05-18 13:25:35 +08:00
|
|
|
});
|
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;
|
|
|
|
|
const newUser = await this.userService.register(body.type, registerUser, async txManager => {
|
2026-05-31 01:01:30 +08:00
|
|
|
await this.inviteService.bindInvitee({ manager: txManager }, { inviteeUserId: registerUser.id, inviteCode: body.inviteCode });
|
2026-05-18 13:25:35 +08:00
|
|
|
});
|
2024-11-28 17:36:45 +08:00
|
|
|
return this.ok(newUser);
|
|
|
|
|
}
|
2023-06-27 09:29:43 +08:00
|
|
|
}
|
|
|
|
|
}
|