2024-11-28 17:36:45 +08:00
|
|
|
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
|
2024-10-13 21:59:29 +08:00
|
|
|
import { LoginService } from '../../modules/login/service/login-service.js';
|
2024-11-28 17:36:45 +08:00
|
|
|
import { BaseController, Constants, SysPublicSettings, SysSettingsService } from '@certd/lib-server';
|
|
|
|
|
import { CodeService } from '../../modules/basic/service/code-service.js';
|
2024-11-30 01:57:09 +08:00
|
|
|
import { checkComm } from '@certd/plus-core';
|
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;
|
|
|
|
|
|
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)
|
2024-10-03 22:03:49 +08:00
|
|
|
user: any
|
2023-01-29 15:26:58 +08:00
|
|
|
) {
|
2024-11-28 17:36:45 +08:00
|
|
|
const token = await this.loginService.loginByPassword(user);
|
2023-06-27 22:45:27 +08:00
|
|
|
this.ctx.cookies.set('token', token.token, {
|
|
|
|
|
maxAge: 1000 * token.expire,
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-29 15:26:58 +08:00
|
|
|
return this.ok(token);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
this.ctx.cookies.set('token', token.token, {
|
|
|
|
|
maxAge: 1000 * token.expire,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.ok(token);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-27 09:29:43 +08:00
|
|
|
@Post('/logout', { summary: Constants.per.authOnly })
|
2023-01-29 15:26:58 +08:00
|
|
|
public logout() {}
|
|
|
|
|
}
|