perf: 支持短信验证码登录

This commit is contained in:
xiaojunnuo
2024-11-28 17:36:45 +08:00
parent 5a20242111
commit 387bcc5fa4
28 changed files with 950 additions and 309 deletions

View File

@@ -1,14 +1,12 @@
import { Rule, RuleType } from '@midwayjs/validate';
import { ALL, Inject } from '@midwayjs/core';
import { Body } from '@midwayjs/core';
import { Controller, Post, Provide } from '@midwayjs/core';
import { BaseController } from '@certd/lib-server';
import { ALL, Body, Controller, Get, Inject, Post, Provide, Query } from '@midwayjs/core';
import { BaseController, Constants } from '@certd/lib-server';
import { CodeService } from '../../modules/basic/service/code-service.js';
import { EmailService } from '../../modules/basic/service/email-service.js';
import { Constants } from '@certd/lib-server';
export class SmsCodeReq {
@Rule(RuleType.number().required())
phoneCode: number;
@Rule(RuleType.string().required())
phoneCode: string;
@Rule(RuleType.string().required())
mobile: string;
@@ -16,7 +14,18 @@ export class SmsCodeReq {
@Rule(RuleType.string().required().max(10))
randomStr: string;
@Rule(RuleType.number().required().max(4))
@Rule(RuleType.string().required().max(4))
imgCode: string;
}
export class EmailCodeReq {
@Rule(RuleType.string().required())
email: string;
@Rule(RuleType.string().required().max(10))
randomStr: string;
@Rule(RuleType.string().required().max(4))
imgCode: string;
}
@@ -32,21 +41,30 @@ export class BasicController extends BaseController {
emailService: EmailService;
@Post('/sendSmsCode', { summary: Constants.per.guest })
public sendSmsCode(
public async sendSmsCode(
@Body(ALL)
body: SmsCodeReq
) {
await this.codeService.checkCaptcha(body.randomStr, body.imgCode);
await this.codeService.sendSmsCode(body.phoneCode, body.mobile, body.randomStr);
return this.ok(null);
}
@Post('/sendEmailCode', { summary: Constants.per.guest })
public async sendEmailCode(
@Body(ALL)
body: EmailCodeReq
) {
await this.codeService.checkCaptcha(body.randomStr, body.imgCode);
await this.codeService.sendEmailCode(body.email, body.randomStr);
// 设置缓存内容
return this.ok(null);
}
@Post('/captcha', { summary: Constants.per.guest })
public async getCaptcha(
@Body()
randomStr
) {
console.assert(randomStr < 10, 'randomStr 过长');
@Get('/captcha', { summary: Constants.per.guest })
public async getCaptcha(@Query('randomStr') randomStr: any) {
const captcha = await this.codeService.generateCaptcha(randomStr);
return this.ok(captcha.data);
this.ctx.res.setHeader('Content-Type', 'image/svg+xml');
return captcha.data;
}
}