node-easysms
Version:
EasySMS is an SMS sender for Node.js
70 lines (69 loc) • 3.6 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AliyunGateway = void 0;
const GatewayErrorException_1 = require("../Core/Exceptions/GatewayErrorException");
const Gateway_1 = require("../Core/Gateway");
const Utils_1 = require("../Core/Support/Utils");
/**
* 阿里云短信服务
* @see https://help.aliyun.com/document_detail/419273.html
*/
class AliyunGateway extends Gateway_1.Gateway {
send(to, message) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
let data = yield message.getData(this);
let signName = (_b = (_a = (yield message.getSignName(this))) !== null && _a !== void 0 ? _a : this.config['sign_name']) !== null && _b !== void 0 ? _b : '';
let query = {
RegionId: this.config['region'] || 'cn-hangzhou',
AccessKeyId: this.config['access_key_id'] || '',
Format: AliyunGateway.ENDPOINT_FORMAT,
SignatureMethod: AliyunGateway.ENDPOINT_SIGNATURE_METHOD,
SignatureVersion: AliyunGateway.ENDPOINT_SIGNATURE_VERSION,
SignatureNonce: (0, Utils_1.randomString)(),
Timestamp: (0, Utils_1.timestampUTC)('YYYY-MM-DD\THH:mm:ss\Z'),
Action: AliyunGateway.ENDPOINT_METHOD,
Version: AliyunGateway.ENDPOINT_VERSION,
PhoneNumbers: to.getIDDCode() ? to.getZeroPrefixedNumber() : to.getNumber(),
SignName: signName,
TemplateCode: yield message.getTemplate(this),
TemplateParam: JSON.stringify(data),
};
query['Signature'] = this.generateSign(query, this.config['access_key_secret']);
let result = yield this.get(AliyunGateway.ENDPOINT_URL, query);
if ('OK' !== result['Code']) {
throw new GatewayErrorException_1.GatewayErrorException(result['Message'], result);
}
return result;
});
}
generateSign(params, key) {
let paramsString = '';
let sparator = '';
let keys = Object.keys(params);
keys = keys.sort();
for (let i = 0; i < keys.length; i++) {
paramsString += sparator + encodeURIComponent(keys[i]) + '=' + encodeURIComponent(params[keys[i]]);
sparator = '&';
}
paramsString = 'GET&%2F&' + encodeURIComponent(paramsString);
paramsString = paramsString.replace(/%7E/g, '~');
return (0, Utils_1.createHmac)(paramsString, key + '&', 'sha1', 'base64');
}
}
exports.AliyunGateway = AliyunGateway;
AliyunGateway.ENDPOINT_URL = 'http://dysmsapi.aliyuncs.com';
AliyunGateway.ENDPOINT_METHOD = 'SendSms';
AliyunGateway.ENDPOINT_VERSION = '2017-05-25';
AliyunGateway.ENDPOINT_FORMAT = 'JSON';
AliyunGateway.ENDPOINT_SIGNATURE_METHOD = 'HMAC-SHA1';
AliyunGateway.ENDPOINT_SIGNATURE_VERSION = '1.0';