UNPKG

@lantsang/nestjs-ucpaas-sms

Version:

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

68 lines (67 loc) 3.19 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UcpaasSendService = void 0; const common_1 = require("@nestjs/common"); const common_constant_1 = require("../constants/common.constant"); const ucpaas_constant_1 = require("../constants/ucpaas.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"); let UcpaasSendService = class UcpaasSendService { constructor(options, httpService, regexUtil) { this.options = options; this.httpService = httpService; this.regexUtil = regexUtil; } /** * 根据模板ID发送单条短信 * @param templateId 云之讯平台短信模板ID * @param mobile 接收人手机号码 * @param param 模板参数,中间以逗号(,)分割 * @param uid 透传uid,自定义 * @returns 单条短信发送结果 */ async send(templateId, mobile, param, uid) { if (!this.regexUtil.isMobile(mobile)) throw new ucpaas_error_1.UcpaasError('手机号格式不正确'); const { data } = await this.httpService .post(ucpaas_constant_1.SEND_SINGLE_URL, { sid: this.options.sid, token: this.options.token, appid: this.options.appid, templateid: templateId, param: param, mobile: mobile, uid: uid }) .toPromise(); if (parseInt(data.code)) throw new ucpaas_error_1.UcpaasError(data.msg); return { success: true, data: { smsid: data.smsid, uid: data.uid } }; } }; __decorate([ error_wrapper_decorator_1.UcpaasErrorWrapper(), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String, String, String]), __metadata("design:returntype", Promise) ], UcpaasSendService.prototype, "send", null); UcpaasSendService = __decorate([ common_1.Injectable(), __param(0, common_1.Inject(common_constant_1.OPTIONS_PROVIDER)), __metadata("design:paramtypes", [Object, common_1.HttpService, regex_util_1.RegexUtil]) ], UcpaasSendService); exports.UcpaasSendService = UcpaasSendService;