filesite
7 months ago
5 changed files with 138 additions and 0 deletions
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
'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; |
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
'use strict'; |
||||
|
||||
const { readdir, readFile } = require('node:fs/promises'); |
||||
const path = require('node:path'); |
||||
const fs = require('node:fs'); |
||||
const { resolve } = require('node:path'); |
||||
|
||||
let myAccessKey = '你的AccessKey ID', |
||||
myAccessSecret = '你的AccessKey Secret'; |
||||
|
||||
//读取自定义配置文件
|
||||
exports.getCustomConfigs = async function(configFile) { |
||||
let data = null; |
||||
|
||||
let customConfigFile = resolve(configFile); |
||||
console.log(customConfigFile); |
||||
if (fs.existsSync(customConfigFile)) { |
||||
try { |
||||
const contents = await readFile(customConfigFile, { encoding: 'utf8' }); |
||||
if (contents) { |
||||
data = JSON.parse(contents); |
||||
} |
||||
} catch (err) { |
||||
console.error(`[FAILED] get config content from %s failed, error: %s`, customConfigFile, err.message); |
||||
} |
||||
} |
||||
|
||||
return data; |
||||
}; |
||||
|
||||
exports.default = { |
||||
"ALIBABA_CLOUD_ACCESS_KEY_ID": myAccessKey, |
||||
"ALIBABA_CLOUD_ACCESS_KEY_SECRET": myAccessSecret, |
||||
}; |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
{ |
||||
"name": "@filesite/service-3rd", |
||||
"description": "Service for api or sdk of the 3rd party.", |
||||
"version": "0.0.1", |
||||
"author": "filesite.io", |
||||
"repository": { |
||||
"type": "git", |
||||
"url": "https://git.filesite.io/filesite/service-3rd.git" |
||||
}, |
||||
"license": "MIT", |
||||
"engines": { |
||||
"node": ">=8.x" |
||||
}, |
||||
"dependencies": { |
||||
"@alicloud/dysmsapi20170525": "^2.0.24" |
||||
} |
||||
} |
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
'use strict'; |
||||
|
||||
const test = require('node:test'); |
||||
const assert = require('node:assert'); |
||||
|
||||
const {default: aliyunSmsClient} = require('../aliyunSmsClient'); |
||||
const {default: defaultConfig, getCustomConfigs: getCustomConfigs} = require('../conf/config'); |
||||
|
||||
test('Custom config load test', async (t) => { |
||||
console.log('Config data', defaultConfig); |
||||
assert.equal(defaultConfig.ALIBABA_CLOUD_ACCESS_KEY_ID, '你的AccessKey ID'); |
||||
|
||||
let myConfig = await getCustomConfigs('./conf/custom_config.json'); |
||||
console.log('Custom config data', myConfig); |
||||
assert.ok(myConfig); |
||||
}); |
||||
|
||||
test('AliyunSmsClient test', async (t) => { |
||||
let myConfig = await getCustomConfigs('./conf/custom_config.json'); |
||||
let client = aliyunSmsClient.createClient(myConfig); |
||||
|
||||
assert.ok(client); |
||||
|
||||
let signName = 'Ta荐', |
||||
templateCode = 'SMS_465915662', |
||||
templateParam = '{"code":"2345"}', |
||||
phoneNumber = '13168946847'; |
||||
|
||||
let sended = await aliyunSmsClient.send(myConfig, signName, templateCode, templateParam, phoneNumber); |
||||
|
||||
assert.equal(sended, true); |
||||
}); |
Loading…
Reference in new issue