build: trident-sync prepare

This commit is contained in:
xiaojunnuo
2023-01-29 13:44:19 +08:00
parent dcd1023a39
commit 07a45b4530
589 changed files with 36886 additions and 2 deletions

View File

@@ -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, '手机号不能为空');
}
}