UNPKG

@iot9x.com/nestjs-ucpaas-sms

Version:

nestjs 云之讯平台短信集成插件

82 lines (81 loc) 3.76 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UcpaasSendService = void 0; const axios_1 = require("@nestjs/axios"); const common_1 = require("@nestjs/common"); const rxjs_1 = require("rxjs"); const common_constant_1 = require("../constants/common.constant"); const error_wrapper_decorator_1 = require("../decorators/error-wrapper.decorator"); const ucpaas_error_1 = require("../errors/ucpaas.error"); const regex_util_1 = require("../utils/regex.util"); const md5_1 = __importDefault(require("md5")); let UcpaasSendService = class UcpaasSendService { options; httpService; regexUtil; constructor(options, httpService, regexUtil) { this.options = options; this.httpService = httpService; this.regexUtil = regexUtil; } /** * 发送变量模板短信,支持向多用户发送 * @param templateId 云之讯平台短信模板ID * @param mobileList 接收人手机号码列表 * @param params 附加参数 * @returns 短信发送结果 */ async variable(templateId, mobileList, params) { if (!mobileList.length) throw new ucpaas_error_1.UcpaasError('至少要有一个手机号'); for (const mobile of mobileList) { if (!this.regexUtil.isMobile(mobile)) throw new ucpaas_error_1.UcpaasError(`手机号${mobile}格式不正确`); } const { data } = await (0, rxjs_1.lastValueFrom)(this.httpService.post("http://open2.ucpaas.com/sms-server/variablesms" /* UcpaasApi.SendVariableSMS */, { clientid: this.options.clientid, password: (0, md5_1.default)(this.options.password), templateid: templateId, mobile: mobileList.join(','), ...(params || {}) })); if (data.code) throw new ucpaas_error_1.UcpaasError(`错误码:${data.code},错误信息:${data.msg}`); return { success: true, data: { total_fee: data.total_fee, uid: data.uid, data: data.data } }; } }; __decorate([ (0, error_wrapper_decorator_1.UcpaasErrorWrapper)(), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Array, Object]), __metadata("design:returntype", Promise) ], UcpaasSendService.prototype, "variable", null); UcpaasSendService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(common_constant_1.OPTIONS_PROVIDER)), __metadata("design:paramtypes", [Object, axios_1.HttpService, regex_util_1.RegexUtil]) ], UcpaasSendService); exports.UcpaasSendService = UcpaasSendService;