'use strict'; const Dysmsapi20170525 = require('@alicloud/dysmsapi20170525'); const OpenApi = require('@alicloud/openapi-client'); const Util = require('@alicloud/tea-util'); const Tea = require('@alicloud/tea-typescript'); class AliyunSMSClient { /** * 使用AK&SK初始化账号Client * @return Client * @throws Exception */ static createClient(myConfig) { // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378664.html。 let config = new OpenApi.Config({ accessKeyId: myConfig.ALIBABA_CLOUD_ACCESS_KEY_ID, accessKeySecret: myConfig.ALIBABA_CLOUD_ACCESS_KEY_SECRET, }); // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi config.endpoint = `dysmsapi.aliyuncs.com`; return new Dysmsapi20170525.default(config); } static async send(myConfig, signName, templateCode, templateParam, phoneNumber) { let sendSmsRequest = new Dysmsapi20170525.SendSmsRequest({ signName: signName, templateCode: templateCode, templateParam: templateParam, phoneNumbers: phoneNumber, }); let runtime = new Util.RuntimeOptions({ }); let client = AliyunSMSClient.createClient(myConfig); let done = false; try { await client.sendSmsWithOptions(sendSmsRequest, runtime); done = true; } catch (error) { console.error("AliyunSMSClient send sms message failed: %s", error.message); } return done; } } exports.default = AliyunSMSClient;