Files
certd/packages/ui/certd-server/src/modules/basic/service/code-service.ts
T

161 lines
4.6 KiB
TypeScript
Raw Normal View History

2024-07-15 00:30:33 +08:00
import { Inject, Provide } from '@midwayjs/core';
2024-11-28 17:36:45 +08:00
import { cache, isDev, randomNumber } from '@certd/basic';
2024-12-11 11:30:32 +08:00
import { SysSettingsService, SysSiteInfo } from '@certd/lib-server';
2024-11-28 17:36:45 +08:00
import { SmsServiceFactory } from '../sms/factory.js';
import { ISmsService } from '../sms/api.js';
import { CodeErrorException } from '@certd/lib-server/dist/basic/exception/code-error-exception.js';
import { EmailService } from './email-service.js';
2024-12-22 14:00:46 +08:00
import { AccessService } from '@certd/lib-server';
import { AccessSysGetter } from '@certd/lib-server';
2024-12-11 11:30:32 +08:00
import { isComm } from '@certd/plus-core';
2023-01-29 13:44:19 +08:00
// {data: '<svg.../svg>', text: 'abcd'}
/**
*/
@Provide()
export class CodeService {
@Inject()
2024-11-28 17:36:45 +08:00
sysSettingsService: SysSettingsService;
@Inject()
emailService: EmailService;
@Inject()
accessService: AccessService;
2023-01-29 13:44:19 +08:00
/**
*/
async generateCaptcha(randomStr) {
2024-08-28 14:40:50 +08:00
const svgCaptcha = await import('svg-captcha');
2023-01-29 13:44:19 +08:00
const c = svgCaptcha.create();
//{data: '<svg.../svg>', text: 'abcd'}
const imgCode = c.text; // = RandomUtil.randomStr(4, true);
2024-11-28 17:36:45 +08:00
cache.set('imgCode:' + randomStr, imgCode, {
2023-01-29 13:44:19 +08:00
ttl: 2 * 60 * 1000, //过期时间 2分钟
});
return c;
}
async getCaptchaText(randomStr) {
2024-11-28 17:36:45 +08:00
return cache.get('imgCode:' + randomStr);
2023-01-29 13:44:19 +08:00
}
async removeCaptcha(randomStr) {
2024-11-28 17:36:45 +08:00
cache.delete('imgCode:' + randomStr);
2023-01-29 13:44:19 +08:00
}
2024-11-28 17:36:45 +08:00
async checkCaptcha(randomStr: string, userCaptcha: string) {
2023-01-29 13:44:19 +08:00
const code = await this.getCaptchaText(randomStr);
if (code == null) {
throw new Error('验证码已过期');
}
2024-11-28 17:36:45 +08:00
if (code.toLowerCase() !== userCaptcha.toLowerCase()) {
2023-01-29 13:44:19 +08:00
throw new Error('验证码不正确');
}
2024-11-28 17:36:45 +08:00
await this.removeCaptcha(randomStr);
2023-01-29 13:44:19 +08:00
return true;
}
/**
*/
2024-11-30 01:57:09 +08:00
async sendSmsCode(phoneCode = '86', mobile: string, randomStr: string) {
2024-12-01 03:09:29 +08:00
if (!mobile) {
2024-12-01 03:02:59 +08:00
throw new Error('手机号不能为空');
}
if (!randomStr) {
throw new Error('randomStr不能为空');
}
2024-11-28 17:36:45 +08:00
const sysSettings = await this.sysSettingsService.getPrivateSettings();
if (!sysSettings.sms?.config?.accessId) {
throw new Error('当前站点还未配置短信');
}
const smsType = sysSettings.sms.type;
const smsConfig = sysSettings.sms.config;
const sender: ISmsService = SmsServiceFactory.createSmsService(smsType);
const accessGetter = new AccessSysGetter(this.accessService);
sender.setCtx({
accessService: accessGetter,
config: smsConfig,
});
const smsCode = randomNumber(4);
await sender.sendSmsCode({
mobile,
code: smsCode,
phoneCode,
});
const key = this.buildSmsCodeKey(phoneCode, mobile, randomStr);
cache.set(key, smsCode, {
ttl: 5 * 60 * 1000, //5分钟
});
2024-11-30 01:57:09 +08:00
return smsCode;
2023-01-29 13:44:19 +08:00
}
/**
*/
2024-11-28 17:36:45 +08:00
async sendEmailCode(email: string, randomStr: string) {
2024-12-01 03:02:59 +08:00
if (!email) {
throw new Error('Email不能为空');
}
if (!randomStr) {
throw new Error('randomStr不能为空');
}
2024-11-28 17:36:45 +08:00
2024-12-11 11:30:32 +08:00
let siteTitle = 'Certd';
if (isComm()) {
const siteInfo = await this.sysSettingsService.getSetting<SysSiteInfo>(SysSiteInfo);
if (siteInfo) {
siteTitle = siteInfo.title || siteTitle;
}
}
2024-11-28 17:36:45 +08:00
const code = randomNumber(4);
await this.emailService.send({
2024-12-11 11:30:32 +08:00
subject: `${siteTitle}】验证码`,
2024-11-28 17:36:45 +08:00
content: `您的验证码是${code},请勿泄露`,
receivers: [email],
});
const key = this.buildEmailCodeKey(email, randomStr);
2024-11-28 17:36:45 +08:00
cache.set(key, code, {
ttl: 5 * 60 * 1000, //5分钟
});
2024-11-30 01:57:09 +08:00
return code;
2024-11-28 17:36:45 +08:00
}
/**
* checkSms
*/
async checkSmsCode(opts: { mobile: string; phoneCode: string; smsCode: string; randomStr: string; throwError: boolean }) {
const key = this.buildSmsCodeKey(opts.phoneCode, opts.mobile, opts.randomStr);
if (isDev()) {
return true;
}
return this.checkValidateCode(key, opts.smsCode, opts.throwError);
}
buildSmsCodeKey(phoneCode: string, mobile: string, randomStr: string) {
return `sms:${phoneCode}${mobile}:${randomStr}`;
}
buildEmailCodeKey(email: string, randomStr: string) {
return `email:${email}:${randomStr}`;
}
checkValidateCode(key: string, userCode: string, throwError = true) {
//验证图片验证码
const code = cache.get(key);
if (code == null || code !== userCode) {
if (throwError) {
throw new CodeErrorException('验证码错误');
}
return false;
}
cache.delete(key);
return true;
}
checkEmailCode(opts: { randomStr: string; validateCode: string; email: string; throwError: boolean }) {
const key = this.buildEmailCodeKey(opts.email, opts.randomStr);
return this.checkValidateCode(key, opts.validateCode, opts.throwError);
2023-01-29 13:44:19 +08:00
}
}