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

198 lines
6.1 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { cache, isDev, randomNumber, simpleNanoId } from "@certd/basic";
import { AccessService, AccessSysGetter, CodeErrorException, SysSettingsService } from "@certd/lib-server";
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { ISmsService } from "../sms/api.js";
import { SmsServiceFactory } from "../sms/factory.js";
import { CaptchaService } from "./captcha-service.js";
2026-05-31 01:41:33 +08:00
import { EmailService } from "./email-service.js";
import { CaptchaRequest } from "../../../plugins/plugin-captcha/api.js";
2023-01-29 13:44:19 +08:00
// {data: '<svg.../svg>', text: 'abcd'}
/**
*/
@Provide()
2025-01-15 01:05:34 +08:00
@Scope(ScopeEnum.Request, { allowDowngrade: true })
2023-01-29 13:44:19 +08:00
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
@Inject()
captchaService: CaptchaService;
2023-01-29 13:44:19 +08:00
2026-05-31 01:41:33 +08:00
async checkCaptcha(body: any, req: CaptchaRequest) {
return await this.captchaService.doValidate({ form: body, req });
2023-01-29 13:44:19 +08:00
}
/**
*/
2025-07-24 16:56:22 +08:00
async sendSmsCode(
2026-05-31 01:41:33 +08:00
phoneCode = "86",
2025-07-24 16:56:22 +08:00
mobile: string,
opts?: {
2026-05-31 01:41:33 +08:00
duration?: number;
verificationType?: string;
verificationCodeLength?: number;
}
2025-07-24 16:56:22 +08:00
) {
2024-12-01 03:09:29 +08:00
if (!mobile) {
2026-05-31 01:41:33 +08:00
throw new Error("手机号不能为空");
2024-12-01 03:02:59 +08:00
}
2024-11-28 17:36:45 +08:00
2026-05-31 01:41:33 +08:00
const verificationCodeLength = Math.floor(Math.max(Math.min(opts?.verificationCodeLength || 4, 8), 4));
const duration = Math.floor(Math.max(Math.min(opts?.duration || 5, 15), 1));
2025-07-24 16:56:22 +08:00
2024-11-28 17:36:45 +08:00
const sysSettings = await this.sysSettingsService.getPrivateSettings();
if (!sysSettings.sms?.config?.accessId) {
2026-05-31 01:41:33 +08:00
throw new Error("当前站点还未配置短信");
2024-11-28 17:36:45 +08:00
}
const smsType = sysSettings.sms.type;
const smsConfig = sysSettings.sms.config;
2025-08-28 17:35:17 +08:00
const sender: ISmsService = await SmsServiceFactory.createSmsService(smsType);
2024-11-28 17:36:45 +08:00
const accessGetter = new AccessSysGetter(this.accessService);
sender.setCtx({
accessService: accessGetter,
config: smsConfig,
});
const smsCode = randomNumber(verificationCodeLength);
2024-11-28 17:36:45 +08:00
await sender.sendSmsCode({
mobile,
code: smsCode,
phoneCode,
});
2026-05-31 01:41:33 +08:00
const key = this.buildSmsCodeKey(phoneCode, mobile, opts?.verificationType);
2024-11-28 17:36:45 +08:00
cache.set(key, smsCode, {
2025-07-24 16:56:22 +08:00
ttl: duration * 60 * 1000, //5分钟
2024-11-28 17:36:45 +08:00
});
2024-11-30 01:57:09 +08:00
return smsCode;
2023-01-29 13:44:19 +08:00
}
/**
2025-07-24 16:56:22 +08:00
*
* @param email 收件邮箱
* @param opts title标题 content内容模版 duration有效时间单位分钟 verificationType验证类型
2023-01-29 13:44:19 +08:00
*/
2025-07-24 16:56:22 +08:00
async sendEmailCode(
email: string,
opts?: {
2026-05-31 01:41:33 +08:00
duration?: number;
verificationType?: string;
verificationCodeLength?: number;
}
2025-07-24 16:56:22 +08:00
) {
2024-12-01 03:02:59 +08:00
if (!email) {
2026-05-31 01:41:33 +08:00
throw new Error("Email不能为空");
2024-12-01 03:02:59 +08:00
}
2026-05-31 01:41:33 +08:00
const verificationCodeLength = Math.floor(Math.max(Math.min(opts?.verificationCodeLength || 4, 8), 4));
const duration = Math.floor(Math.max(Math.min(opts?.duration || 5, 15), 1));
const code = randomNumber(verificationCodeLength);
2026-05-31 01:41:33 +08:00
2025-12-12 23:39:09 +08:00
const templateData = {
2026-05-31 01:41:33 +08:00
code,
duration,
2025-12-14 01:36:20 +08:00
title: "验证码",
2026-05-31 01:41:33 +08:00
content: `您的验证码是${code},请勿泄露`,
notificationType: "registerCode",
};
if (opts?.verificationType === "forgotPassword") {
templateData.title = "找回密码";
templateData.notificationType = "forgotPassword";
2025-12-14 01:36:20 +08:00
}
await this.emailService.sendByTemplate({
2026-05-31 01:41:33 +08:00
type: templateData.notificationType,
data: templateData,
receivers: [email],
2024-11-28 17:36:45 +08:00
});
2026-05-31 01:41:33 +08:00
const key = this.buildEmailCodeKey(email, opts?.verificationType);
2024-11-28 17:36:45 +08:00
cache.set(key, code, {
2025-07-24 16:56:22 +08:00
ttl: duration * 60 * 1000, //5分钟
2024-11-28 17:36:45 +08:00
});
2024-11-30 01:57:09 +08:00
return code;
2024-11-28 17:36:45 +08:00
}
/**
* checkSms
*/
2026-05-31 01:41:33 +08:00
async checkSmsCode(opts: { mobile: string; phoneCode: string; smsCode: string; verificationType?: string; throwError: boolean; maxErrorCount?: number }) {
const key = this.buildSmsCodeKey(opts.phoneCode, opts.mobile, opts.verificationType);
2026-05-31 01:41:33 +08:00
return this.checkValidateCode("sms", key, opts.smsCode, opts.throwError, opts.maxErrorCount);
2024-11-28 17:36:45 +08:00
}
buildSmsCodeKey(phoneCode: string, mobile: string, verificationType?: string) {
2026-05-31 01:41:33 +08:00
return ["sms", verificationType, phoneCode, mobile].filter(item => !!item).join(":");
2024-11-28 17:36:45 +08:00
}
buildEmailCodeKey(email: string, verificationType?: string) {
2026-05-31 01:41:33 +08:00
return ["email", verificationType, email].filter(item => !!item).join(":");
2024-11-28 17:36:45 +08:00
}
2026-05-31 01:41:33 +08:00
checkValidateCode(type: string, key: string, userCode: string, throwError = true, maxErrorCount = 3) {
// 记录异常次数key
2026-05-31 01:41:33 +08:00
if (isDev() && userCode === "1234567") {
return true;
}
2026-05-31 01:41:33 +08:00
const err_num_key = key + ":err_num";
//验证邮件验证码
2024-11-28 17:36:45 +08:00
const code = cache.get(key);
if (code == null || code !== userCode) {
let maxRetryCount = false;
if (!!code && maxErrorCount > 0) {
2026-05-31 01:41:33 +08:00
const err_num = cache.get(err_num_key) || 0;
if (err_num >= maxErrorCount - 1) {
maxRetryCount = true;
cache.delete(key);
cache.delete(err_num_key);
} else {
cache.set(err_num_key, err_num + 1, {
2026-05-31 01:41:33 +08:00
ttl: 30 * 60 * 1000,
});
}
}
2024-11-28 17:36:45 +08:00
if (throwError) {
2026-05-31 01:41:33 +08:00
const label = type === "sms" ? "手机" : "邮箱";
throw new CodeErrorException(!maxRetryCount ? `${label}验证码错误` : `${label}验证码错误请获取新的验证码`);
2024-11-28 17:36:45 +08:00
}
return false;
}
cache.delete(key);
cache.delete(err_num_key);
2024-11-28 17:36:45 +08:00
return true;
}
checkEmailCode(opts: { validateCode: string; email: string; verificationType?: string; throwError: boolean; maxErrorCount?: number }) {
2026-05-31 01:41:33 +08:00
const key = this.buildEmailCodeKey(opts.email, opts.verificationType);
return this.checkValidateCode("email", key, opts.validateCode, opts.throwError, opts.maxErrorCount);
2023-01-29 13:44:19 +08:00
}
2025-07-24 16:56:22 +08:00
compile(templateString: string) {
return new Function(
"data",
` with(data || {}) {
return \`${templateString}\`;
}
`
);
}
2025-11-27 01:59:22 +08:00
2026-05-31 01:41:33 +08:00
buildValidationValueKey(code: string) {
2025-11-27 01:59:22 +08:00
return `validationValue:${code}`;
}
2026-05-31 01:41:33 +08:00
setValidationValue(value: any) {
2025-11-27 01:59:22 +08:00
const randomCode = simpleNanoId(12);
const key = this.buildValidationValueKey(randomCode);
cache.set(key, value, {
ttl: 5 * 60 * 1000, //5分钟
});
return randomCode;
}
2026-05-31 01:41:33 +08:00
getValidationValue(code: string) {
2025-11-27 01:59:22 +08:00
return cache.get(this.buildValidationValueKey(code));
}
2023-01-29 13:44:19 +08:00
}