1 | "use strict";
|
2 | var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
3 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
4 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
5 | 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;
|
6 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
7 | };
|
8 | var __metadata = (this && this.__metadata) || function (k, v) {
|
9 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
10 | };
|
11 | var __param = (this && this.__param) || function (paramIndex, decorator) {
|
12 | return function (target, key) { decorator(target, key, paramIndex); }
|
13 | };
|
14 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
15 | return new (P || (P = Promise))(function (resolve, reject) {
|
16 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
17 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
18 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
19 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
20 | });
|
21 | };
|
22 | Object.defineProperty(exports, "__esModule", { value: true });
|
23 | const common_1 = require("@nestjs/common");
|
24 | const Chance = require("chance");
|
25 | const crypto = require("crypto");
|
26 | const request_1 = require("request");
|
27 | const param_util_1 = require("../utils/param.util");
|
28 | let QcloudService = class QcloudService {
|
29 | constructor(paramUtil) {
|
30 | this.paramUtil = paramUtil;
|
31 | this.smsApiBase = "https://yun.tim.qq.com/v5/tlssmssvr/";
|
32 | }
|
33 | sendSms(smsRequest) {
|
34 | return __awaiter(this, void 0, void 0, function* () {
|
35 | let url = "";
|
36 | const random = new Chance().natural({ max: 100000 });
|
37 | const time = Math.floor(Date.now() / 1000);
|
38 | const sig = yield this.calculateSignature(smsRequest.appKey, random, time, smsRequest.mobile);
|
39 | let mobile;
|
40 | if (smsRequest.mobile.length === 1) {
|
41 | url = this.smsApiBase + "sendsms";
|
42 | mobile = { mobile: smsRequest.mobile[0], nationcode: "86" };
|
43 | }
|
44 | else {
|
45 | url = this.smsApiBase + "sendmultisms2";
|
46 | mobile = smsRequest.mobile.map(item => ({ mobile: item, nationcode: "86" }));
|
47 | }
|
48 | url += `?sdkappid=${smsRequest.appId}&random=${random}`;
|
49 | const reqBody = JSON.stringify({
|
50 | sig,
|
51 | time,
|
52 | sign: smsRequest.signName,
|
53 | tpl_id: smsRequest.templateId,
|
54 | params: smsRequest.templateParam,
|
55 | tel: mobile
|
56 | });
|
57 | return this.post(url, reqBody);
|
58 | });
|
59 | }
|
60 | calculateSignature(appKey, random, time, mobile) {
|
61 | return __awaiter(this, void 0, void 0, function* () {
|
62 | return crypto.createHash("sha256")
|
63 | .update(`appkey=${appKey}&random=${random}&time=${time}&mobile=${mobile.join()}`, "utf8")
|
64 | .digest("hex");
|
65 | });
|
66 | }
|
67 | post(url, reqBody) {
|
68 | return __awaiter(this, void 0, void 0, function* () {
|
69 | return new Promise(((resolve, reject) => {
|
70 | request_1.post(url, { body: reqBody, headers: { "Content-Type": "application/json" } }, (err, res, body) => {
|
71 | const responseBody = JSON.parse(body);
|
72 | if (responseBody.result === 0) {
|
73 | resolve({ code: responseBody.result, message: "发送成功" });
|
74 | }
|
75 | else {
|
76 | reject({ code: responseBody.result, message: responseBody.errmsg });
|
77 | }
|
78 | });
|
79 | }));
|
80 | });
|
81 | }
|
82 | };
|
83 | QcloudService = __decorate([
|
84 | common_1.Injectable(),
|
85 | __param(0, common_1.Inject(param_util_1.ParamUtil)),
|
86 | __metadata("design:paramtypes", [param_util_1.ParamUtil])
|
87 | ], QcloudService);
|
88 | exports.QcloudService = QcloudService;
|
89 |
|
90 |
|