2025-09-11 23:47:05 +08:00
|
|
|
import { ALL, Body, Controller, Inject, Post, Provide } from "@midwayjs/core";
|
|
|
|
|
import { LoginService } from "../../../modules/login/service/login-service.js";
|
|
|
|
|
import { AddonService, BaseController, Constants, SysPublicSettings, SysSettingsService } from "@certd/lib-server";
|
|
|
|
|
import { CodeService } from "../../../modules/basic/service/code-service.js";
|
|
|
|
|
import { checkComm } from "@certd/plus-core";
|
|
|
|
|
import { logger } from "@certd/basic";
|
|
|
|
|
import { ICaptchaAddon } from "../../../plugins/plugin-captcha/api.js";
|
2023-01-29 15:26:58 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
@Provide()
|
|
|
|
|
@Controller('/api/')
|
|
|
|
|
export class LoginController extends BaseController {
|
|
|
|
|
@Inject()
|
|
|
|
|
loginService: LoginService;
|
2024-11-28 17:36:45 +08:00
|
|
|
@Inject()
|
|
|
|
|
codeService: CodeService;
|
|
|
|
|
|
|
|
|
|
@Inject()
|
|
|
|
|
sysSettingsService: SysSettingsService;
|
2025-09-11 23:47:05 +08:00
|
|
|
@Inject()
|
|
|
|
|
addonService: AddonService;
|
2024-11-28 17:36:45 +08:00
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/login', { summary: Constants.per.guest })
|
2023-01-29 15:26:58 +08:00
|
|
|
public async login(
|
|
|
|
|
@Body(ALL)
|
2025-09-11 23:47:05 +08:00
|
|
|
body: any
|
2023-01-29 15:26:58 +08:00
|
|
|
) {
|
2025-09-11 23:47:05 +08:00
|
|
|
await this.loginService.doCaptchaValidate({form:body.captcha})
|
|
|
|
|
const token = await this.loginService.loginByPassword(body);
|
2025-04-17 01:15:55 +08:00
|
|
|
this.writeTokenCookie(token);
|
2023-01-29 15:26:58 +08:00
|
|
|
return this.ok(token);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-17 01:15:55 +08:00
|
|
|
private writeTokenCookie(token: { expire: any; token: any }) {
|
2025-04-19 14:25:56 +08:00
|
|
|
this.ctx.cookies.set("certd_token", token.token, {
|
2025-04-17 01:15:55 +08:00
|
|
|
maxAge: 1000 * token.expire
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 11:10:57 +08:00
|
|
|
@Post('/loginBySms', { summary: Constants.per.guest })
|
|
|
|
|
public async loginBySms(
|
|
|
|
|
@Body(ALL)
|
|
|
|
|
body: any
|
|
|
|
|
) {
|
2024-11-28 17:36:45 +08:00
|
|
|
const settings = await this.sysSettingsService.getSetting<SysPublicSettings>(SysPublicSettings);
|
|
|
|
|
if (settings.smsLoginEnabled !== true) {
|
|
|
|
|
throw new Error('当前站点禁止短信验证码登录');
|
|
|
|
|
}
|
2024-11-30 01:57:09 +08:00
|
|
|
checkComm();
|
2024-11-28 17:36:45 +08:00
|
|
|
|
|
|
|
|
const token = await this.loginService.loginBySmsCode({
|
|
|
|
|
phoneCode: body.phoneCode,
|
|
|
|
|
mobile: body.mobile,
|
|
|
|
|
smsCode: body.smsCode,
|
|
|
|
|
randomStr: body.randomStr,
|
|
|
|
|
});
|
2024-11-28 11:10:57 +08:00
|
|
|
|
2025-04-17 01:15:55 +08:00
|
|
|
this.writeTokenCookie(token);
|
|
|
|
|
|
|
|
|
|
return this.ok(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post('/loginByTwoFactor', { summary: Constants.per.guest })
|
|
|
|
|
public async loginByTwoFactor(
|
|
|
|
|
@Body(ALL)
|
|
|
|
|
body: any
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
const token = await this.loginService.loginByTwoFactor({
|
2025-04-17 22:34:21 +08:00
|
|
|
loginId: body.loginId,
|
2025-04-17 01:15:55 +08:00
|
|
|
verifyCode: body.verifyCode,
|
2024-11-28 11:10:57 +08:00
|
|
|
});
|
|
|
|
|
|
2025-04-17 01:15:55 +08:00
|
|
|
this.writeTokenCookie(token);
|
2024-11-28 11:10:57 +08:00
|
|
|
return this.ok(token);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/logout', { summary: Constants.per.authOnly })
|
2025-04-19 14:25:56 +08:00
|
|
|
public logout() {
|
|
|
|
|
this.ctx.cookies.set("certd_token", "", {
|
|
|
|
|
maxAge: 0
|
|
|
|
|
});
|
|
|
|
|
return this.ok();
|
|
|
|
|
}
|
2025-09-11 23:47:05 +08:00
|
|
|
|
|
|
|
|
@Post('/captcha/getParams', { summary: Constants.per.guest })
|
|
|
|
|
async getCaptchaParams() {
|
|
|
|
|
|
|
|
|
|
const settings = await this.sysSettingsService.getPublicSettings()
|
|
|
|
|
if (settings.captchaEnabled) {
|
|
|
|
|
const addonId = settings.captchaAddonId;
|
|
|
|
|
|
|
|
|
|
const addon:ICaptchaAddon = await this.addonService.getAddonById(addonId,true,0)
|
|
|
|
|
if (!addon) {
|
|
|
|
|
logger.warn('验证码插件还未配置')
|
|
|
|
|
return this.ok({});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const params = await addon.getClientParams()
|
|
|
|
|
return this.ok(params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.ok({});
|
|
|
|
|
}
|
2023-01-29 15:26:58 +08:00
|
|
|
}
|