mirror of
https://github.com/certd/certd.git
synced 2026-04-15 13:32:37 +08:00
🔱: [server] sync upgrade with 21 commits [trident-sync]
Update README.md
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { Rule,RuleType } from '@midwayjs/validate';
|
||||
import { ALL, Inject } from '@midwayjs/decorator';
|
||||
import { Body } from '@midwayjs/decorator';
|
||||
import { Controller, Post, Provide } from '@midwayjs/decorator';
|
||||
import { BaseController } from '../../../basic/base-controller';
|
||||
import { CodeService } from '../service/code-service';
|
||||
export class SmsCodeReq {
|
||||
@Rule(RuleType.number().required())
|
||||
phoneCode: number;
|
||||
|
||||
@Rule(RuleType.string().required())
|
||||
mobile: string;
|
||||
|
||||
@Rule(RuleType.string().required().max(10))
|
||||
randomStr: string;
|
||||
|
||||
@Rule(RuleType.number().required().max(4))
|
||||
imgCode: string;
|
||||
}
|
||||
|
||||
// const enumsMap = {};
|
||||
// glob('src/modules/**/enums/*.ts', {}, (err, matches) => {
|
||||
// console.log('matched', matches);
|
||||
// for (const filePath of matches) {
|
||||
// const module = require('/' + filePath);
|
||||
// console.log('modules', module);
|
||||
// }
|
||||
// });
|
||||
|
||||
/**
|
||||
*/
|
||||
@Provide()
|
||||
@Controller('/api/basic')
|
||||
export class BasicController extends BaseController {
|
||||
@Inject()
|
||||
codeService: CodeService;
|
||||
@Post('/sendSmsCode')
|
||||
public sendSmsCode(
|
||||
@Body(ALL)
|
||||
body: SmsCodeReq
|
||||
) {
|
||||
// 设置缓存内容
|
||||
return this.ok(null);
|
||||
}
|
||||
|
||||
@Post('/captcha')
|
||||
public async getCaptcha(
|
||||
@Body()
|
||||
randomStr
|
||||
) {
|
||||
console.assert(randomStr < 10, 'randomStr 过长');
|
||||
const captcha = await this.codeService.generateCaptcha(randomStr);
|
||||
return this.ok(captcha.data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { Inject, Provide } from '@midwayjs/decorator';
|
||||
import { CacheManager } from '@midwayjs/cache';
|
||||
const svgCaptcha = require('svg-captcha');
|
||||
|
||||
// {data: '<svg.../svg>', text: 'abcd'}
|
||||
/**
|
||||
*/
|
||||
@Provide()
|
||||
export class CodeService {
|
||||
@Inject()
|
||||
cache: CacheManager; // 依赖注入CacheManager
|
||||
|
||||
/**
|
||||
*/
|
||||
async generateCaptcha(randomStr) {
|
||||
console.assert(randomStr < 10, 'randomStr 过长');
|
||||
const c = svgCaptcha.create();
|
||||
//{data: '<svg.../svg>', text: 'abcd'}
|
||||
const imgCode = c.text; // = RandomUtil.randomStr(4, true);
|
||||
await this.cache.set('imgCode:' + randomStr, imgCode, {
|
||||
ttl: 2 * 60 * 1000, //过期时间 2分钟
|
||||
});
|
||||
return c;
|
||||
}
|
||||
|
||||
async getCaptchaText(randomStr) {
|
||||
return await this.cache.get('imgCode:' + randomStr);
|
||||
}
|
||||
|
||||
async removeCaptcha(randomStr) {
|
||||
await this.cache.del('imgCode:' + randomStr);
|
||||
}
|
||||
|
||||
async checkCaptcha(randomStr, userCaptcha) {
|
||||
const code = await this.getCaptchaText(randomStr);
|
||||
if (code == null) {
|
||||
throw new Error('验证码已过期');
|
||||
}
|
||||
if (code !== userCaptcha) {
|
||||
throw new Error('验证码不正确');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
*/
|
||||
async sendSms(phoneCode, mobile, smsCode) {
|
||||
console.assert(phoneCode != null && mobile != null, '手机号不能为空');
|
||||
console.assert(smsCode != null, '验证码不能为空');
|
||||
}
|
||||
|
||||
/**
|
||||
* loginBySmsCode
|
||||
*/
|
||||
async loginBySmsCode(user, smsCode) {
|
||||
console.assert(user.mobile != null, '手机号不能为空');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user