Files
certd/packages/ui/certd-server/src/modules/basic/sms/tencent-sms.ts
T

120 lines
3.1 KiB
TypeScript
Raw Normal View History

import { TencentAccess } from "../../../plugins/plugin-lib/tencent/access.js";
import { importRuntime } from "@certd/pipeline";
import { ISmsService, PluginInputs } from "./api.js";
2025-08-28 17:35:17 +08:00
export type TencentSmsConfig = {
accessId: string;
signName: string;
codeTemplateId: string;
appId: string;
region: string;
};
export class TencentSmsService implements ISmsService {
static getDefine() {
return {
2026-05-31 01:41:33 +08:00
name: "tencent",
desc: "腾讯云短信服务",
2025-08-28 17:35:17 +08:00
input: {
accessId: {
2026-05-31 01:41:33 +08:00
title: "腾讯云授权",
2025-08-28 17:35:17 +08:00
component: {
2026-05-31 01:41:33 +08:00
name: "access-selector",
type: "tencent",
2025-08-28 17:35:17 +08:00
},
required: true,
},
region: {
2026-05-31 01:41:33 +08:00
title: "区域",
value: "ap-beijing",
2025-08-28 17:35:17 +08:00
component: {
2026-05-31 01:41:33 +08:00
name: "a-select",
vModel: "value",
options: [
{ value: "ap-beijing", label: "华北地区(北京)" },
{ value: "ap-guangzhou", label: "华南地区(广州)" },
{ value: "ap-nanjing", label: "华东地区(南京)" },
],
2025-08-28 17:35:17 +08:00
},
2026-05-31 01:41:33 +08:00
helper: "随便选一个",
2025-08-28 17:35:17 +08:00
required: true,
},
signName: {
2026-05-31 01:41:33 +08:00
title: "签名",
2025-08-28 17:35:17 +08:00
component: {
2026-05-31 01:41:33 +08:00
name: "a-input",
vModel: "value",
2025-08-28 17:35:17 +08:00
},
required: true,
},
appId: {
2026-05-31 01:41:33 +08:00
title: "应用ID",
2025-08-28 17:35:17 +08:00
component: {
2026-05-31 01:41:33 +08:00
name: "a-input",
vModel: "value",
2025-08-28 17:35:17 +08:00
},
required: true,
},
codeTemplateId: {
2026-05-31 01:41:33 +08:00
title: "验证码模板Id",
2025-08-28 17:35:17 +08:00
component: {
2026-05-31 01:41:33 +08:00
name: "a-input",
vModel: "value",
2025-08-28 17:35:17 +08:00
},
required: true,
},
} as PluginInputs<TencentSmsConfig>,
};
}
ctx: { accessService: any; config: TencentSmsConfig };
2025-08-28 17:35:17 +08:00
async setCtx(ctx: any) {
2025-08-28 17:35:17 +08:00
this.ctx = ctx;
}
async getClient() {
const sdk = await importRuntime("tencentcloud-sdk-nodejs/tencentcloud/services/sms/v20210111/index.js");
2025-08-28 17:35:17 +08:00
const client = sdk.v20210111.Client;
const access: TencentAccess = await this.ctx.accessService.getById(this.ctx.config.accessId);
2025-08-28 17:35:17 +08:00
// const region = this.region;
const clientConfig = {
credential: {
secretId: access.secretId,
secretKey: access.secretKey,
},
region: this.ctx.config.region,
profile: {
httpProfile: {
endpoint: `sms.${access.intlDomain()}tencentcloudapi.com`,
},
},
};
return new client(clientConfig);
}
async sendSmsCode(opts: { mobile: string; code: string; phoneCode: string }) {
const { mobile, code, phoneCode } = opts;
const client = await this.getClient();
const smsConfig = this.ctx.config;
const params = {
2026-05-31 01:41:33 +08:00
PhoneNumberSet: [`+${phoneCode}${mobile}`],
SmsSdkAppId: smsConfig.appId,
TemplateId: smsConfig.codeTemplateId,
SignName: smsConfig.signName,
TemplateParamSet: [code],
2025-08-28 17:35:17 +08:00
};
const ret = await client.SendSms(params);
this.checkRet(ret);
}
checkRet(ret: any) {
if (!ret || ret.Error) {
2026-05-31 01:41:33 +08:00
throw new Error("执行失败:" + ret.Error.Code + "," + ret.Error.Message);
2025-08-28 17:35:17 +08:00
}
}
}