UNPKG

node-easysms

Version:

EasySMS is an SMS sender for Node.js

68 lines (67 loc) 3.3 kB
'use strict'; 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.YunpianGateway = void 0; const GatewayErrorException_1 = require("../Core/Exceptions/GatewayErrorException"); const Gateway_1 = require("../Core/Gateway"); /** * 云片短信服务 * @see [按内容发送](https://www.yunpian.com/doc/zh_CN/domestic/single_send.html) * @see [按模板发送](https://www.yunpian.com/doc/zh_CN/domestic/tpl_single_send.html) */ class YunpianGateway extends Gateway_1.Gateway { send(to, message) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; let template = yield message.getTemplate(this); let func = 'single_send'; let domain = (_a = this.config['domain']) !== null && _a !== void 0 ? _a : YunpianGateway.ENDPOINT_DOMAIN; let params = { apikey: this.config['api_key'], mobile: to.getUniversalNumber(), }; if (template) { func = 'tpl_single_send'; let data = []; let templateData = yield message.getData(this); if (templateData) { for (let key in templateData) { data.push(encodeURIComponent(`#${key}#`) + '=' + encodeURIComponent(templateData[key])); } } params['tpl_id'] = template; params['tpl_value'] = data.join('&'); } else { let content = yield message.getContent(this); if (!(/^【.+】.+/.test(content))) { let signName = (_c = (_b = (yield message.getSignName(this))) !== null && _b !== void 0 ? _b : this.config['sign_name']) !== null && _c !== void 0 ? _c : ''; content = signName + content; } params['text'] = content; } let result = yield this.post(this.buildEndpoint(func, domain), params, { 'Accept': 'application/json;charset=utf-8', 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', }); if (result['code']) { throw new GatewayErrorException_1.GatewayErrorException(result['msg'], result); } return result; }); } buildEndpoint(func, domain) { return `https://${domain}/${YunpianGateway.ENDPOINT_VERSION}/sms/${func}.json`; } } exports.YunpianGateway = YunpianGateway; YunpianGateway.ENDPOINT_DOMAIN = 'sms.yunpian.com'; YunpianGateway.ENDPOINT_VERSION = 'v2';