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 typeorm_1 = require("@nestjs/typeorm");
|
25 | const moment = require("moment");
|
26 | const typeorm_2 = require("typeorm");
|
27 | const sms_log_entity_1 = require("../entities/sms-log.entity");
|
28 | const sms_template_entity_1 = require("../entities/sms-template.entity");
|
29 | const sms_entity_1 = require("../entities/sms.entity");
|
30 | const param_util_1 = require("../utils/param.util");
|
31 | const qcloud_service_1 = require("./qcloud.service");
|
32 | let SmsService = class SmsService {
|
33 | constructor(qcloudService, paramUtil, smsRepository, smsTemplateRepository, smsLogRepository) {
|
34 | this.qcloudService = qcloudService;
|
35 | this.paramUtil = paramUtil;
|
36 | this.smsRepository = smsRepository;
|
37 | this.smsTemplateRepository = smsTemplateRepository;
|
38 | this.smsLogRepository = smsLogRepository;
|
39 | }
|
40 | createSms(sms) {
|
41 | return __awaiter(this, void 0, void 0, function* () {
|
42 | const existSms = yield this.smsRepository.createQueryBuilder("sms").where(`sms.app_id='${sms.appId}' OR sms.sign_name='${sms.signName}'`).getOne();
|
43 | if (existSms) {
|
44 | const existError = existSms.appId === sms.appId ? `appId=${sms.appId}` : `signName=${sms.signName}`;
|
45 | throw new common_1.HttpException(`短信插件'${existError}'已存在`, 400);
|
46 | }
|
47 | if (sms.templates && sms.templates.length !== 0) {
|
48 | const templates = (yield this.smsTemplateRepository.findByIds(sms.templates.map(item => item.templateId))).concat(sms.templates);
|
49 | const sameTemplateId = yield this.paramUtil.findSameTemplateId(templates);
|
50 | if (sameTemplateId.length !== 0) {
|
51 | throw new common_1.HttpException(`存在相同模板templateId='[${sameTemplateId.toString()}]'`, 400);
|
52 | }
|
53 | }
|
54 | try {
|
55 | sms.appKey = yield this.paramUtil.encryptor(sms.appId, sms.appKey);
|
56 | yield this.smsRepository.save(this.smsRepository.create(sms));
|
57 | }
|
58 | catch (error) {
|
59 | throw new common_1.HttpException(`数据库错误:${error.toString()}`, 501);
|
60 | }
|
61 | });
|
62 | }
|
63 | addTemplateToSms(appId, smsTemplate) {
|
64 | return __awaiter(this, void 0, void 0, function* () {
|
65 | const existSms = yield this.smsRepository.findOne(appId);
|
66 | if (!existSms) {
|
67 | throw new common_1.HttpException(`指定短信插件'appId=${appId}'不存在`, 400);
|
68 | }
|
69 | const templates = (yield this.smsTemplateRepository.findByIds(smsTemplate.map(item => item.templateId))).concat(smsTemplate);
|
70 | const sameTemplateId = yield this.paramUtil.findSameTemplateId(templates);
|
71 | if (sameTemplateId.length !== 0) {
|
72 | throw new common_1.HttpException(`存在相同模板templateId='[${sameTemplateId.toString()}]'`, 400);
|
73 | }
|
74 | const queryRunner = typeorm_2.getConnection().createQueryRunner();
|
75 | yield queryRunner.startTransaction();
|
76 | try {
|
77 | const newSmsTemplate = yield queryRunner.manager.save(smsTemplate);
|
78 | yield queryRunner.manager.createQueryBuilder().relation(sms_entity_1.Sms, "templates").of(existSms).add(newSmsTemplate);
|
79 | yield queryRunner.commitTransaction();
|
80 | }
|
81 | catch (error) {
|
82 | yield queryRunner.rollbackTransaction();
|
83 | throw new common_1.HttpException(`数据库错误:${error.toString()}`, 501);
|
84 | }
|
85 | });
|
86 | }
|
87 | deleteSms(appId) {
|
88 | return __awaiter(this, void 0, void 0, function* () {
|
89 | const existSms = yield this.smsRepository.findOne(appId);
|
90 | if (!existSms) {
|
91 | throw new common_1.HttpException(`指定短信插件'appId=${appId}'不存在`, 400);
|
92 | }
|
93 | try {
|
94 | yield this.smsRepository.delete(appId);
|
95 | }
|
96 | catch (error) {
|
97 | throw new common_1.HttpException(`数据库错误:${error.toString()}`, 501);
|
98 | }
|
99 | });
|
100 | }
|
101 | deleteSmsTemplate(templateId) {
|
102 | return __awaiter(this, void 0, void 0, function* () {
|
103 | try {
|
104 | yield this.smsTemplateRepository.delete(templateId);
|
105 | }
|
106 | catch (error) {
|
107 | throw new common_1.HttpException(`数据库错误:${error.toString()}`, 501);
|
108 | }
|
109 | });
|
110 | }
|
111 | updateSms(appId, newSignName, newValidationTime) {
|
112 | return __awaiter(this, void 0, void 0, function* () {
|
113 | const existSms = yield this.smsRepository.findOne(appId);
|
114 | if (!existSms) {
|
115 | throw new common_1.HttpException(`指定短信插件'appId=${appId}'不存在`, 400);
|
116 | }
|
117 | else if (yield this.smsRepository.findOne({ signName: newSignName })) {
|
118 | throw new common_1.HttpException(`指定签名'signName=${newSignName}'已存在`, 400);
|
119 | }
|
120 | try {
|
121 | existSms.signName = newSignName;
|
122 | existSms.validationTime = newValidationTime;
|
123 | yield this.smsRepository.save(existSms);
|
124 | }
|
125 | catch (error) {
|
126 | throw new common_1.HttpException(`数据库错误:${error.toString()}`, 501);
|
127 | }
|
128 | });
|
129 | }
|
130 | updateSmsTemplate(templateId, name, remark) {
|
131 | return __awaiter(this, void 0, void 0, function* () {
|
132 | const existTemplate = yield this.smsTemplateRepository.findOne(templateId);
|
133 | if (!existTemplate) {
|
134 | throw new common_1.HttpException(`指定短信模板'templateId=${templateId}'不存在`, 400);
|
135 | }
|
136 | try {
|
137 | existTemplate.name = name;
|
138 | existTemplate.remark = remark;
|
139 | yield this.smsTemplateRepository.save(existTemplate);
|
140 | }
|
141 | catch (error) {
|
142 | throw new common_1.HttpException(`数据库错误:${error.toString()}`, 501);
|
143 | }
|
144 | });
|
145 | }
|
146 | findOneSms(appId) {
|
147 | return __awaiter(this, void 0, void 0, function* () {
|
148 | return this.smsRepository.findOne(appId, { relations: ["templates"] });
|
149 | });
|
150 | }
|
151 | findAllSms() {
|
152 | return __awaiter(this, void 0, void 0, function* () {
|
153 | return this.smsRepository.find({ relations: ["templates"] });
|
154 | });
|
155 | }
|
156 | findOneSmsLog(templateId) {
|
157 | return __awaiter(this, void 0, void 0, function* () {
|
158 | const existTemplate = yield this.smsTemplateRepository.findOne(templateId);
|
159 | if (!existTemplate) {
|
160 | throw new common_1.HttpException(`指定短信模板'templateId=${templateId}'不存在`, 400);
|
161 | }
|
162 | return this.smsTemplateRepository.createQueryBuilder().relation(sms_template_entity_1.SmsTemplate, "smsLogs").of(templateId).loadMany();
|
163 | });
|
164 | }
|
165 | findAllSmsLog() {
|
166 | return __awaiter(this, void 0, void 0, function* () {
|
167 | return this.smsLogRepository.find();
|
168 | });
|
169 | }
|
170 | sendMessageByQCloud(type, smsRequest) {
|
171 | return __awaiter(this, void 0, void 0, function* () {
|
172 | const existSms = yield this.smsRepository.findOne(smsRequest.appId);
|
173 | if (!existSms) {
|
174 | throw new common_1.HttpException(`指定短信插件'appId=${smsRequest.appId}'不存在`, 400);
|
175 | }
|
176 | else {
|
177 | const existTemplate = yield this.smsTemplateRepository.findOne(smsRequest.templateId);
|
178 | if (!existTemplate) {
|
179 | throw new common_1.HttpException(`指定短信模板'templateId=${smsRequest.templateId}'不存在`, 400);
|
180 | }
|
181 | smsRequest.signName = existSms.signName;
|
182 | smsRequest.appKey = yield this.paramUtil.decryptor(existSms.appId, existSms.appKey);
|
183 | let validationCode;
|
184 | let validationTime;
|
185 | smsRequest.templateParam = [];
|
186 | if (type === 1) {
|
187 | validationCode = yield this.paramUtil.genValidationCode();
|
188 | validationTime = existSms.validationTime;
|
189 | smsRequest.templateParam = [`${validationCode}`, `${validationTime}`];
|
190 | }
|
191 | yield this.qcloudService.sendSms(smsRequest).then(resolve => {
|
192 | this.saveSmsLog(true, resolve.code, resolve.message, smsRequest, new sms_log_entity_1.SmsLog());
|
193 | }).catch(reject => {
|
194 | const rejectCode = reject.code ? reject.code : 500;
|
195 | this.saveSmsLog(false, rejectCode, reject.message, smsRequest, new sms_log_entity_1.SmsLog());
|
196 | throw new common_1.HttpException(`发送失败,原因:${reject.message}`, rejectCode);
|
197 | });
|
198 | return { code: 200, message: "发送短信成功" };
|
199 | }
|
200 | });
|
201 | }
|
202 | validator(mobile, validationCode) {
|
203 | return __awaiter(this, void 0, void 0, function* () {
|
204 | const exist = yield this.smsLogRepository.findOne({ where: { targetMobile: mobile, validationCode } });
|
205 | if (!exist) {
|
206 | throw new common_1.HttpException("验证码错误", 406);
|
207 | }
|
208 | if (moment().isAfter(moment(exist.sendTime, "YYYY-MM-DD HH:mm:ss").add(exist.validationTime, "m"))) {
|
209 | throw new common_1.HttpException("验证超时", 408);
|
210 | }
|
211 | });
|
212 | }
|
213 | saveSmsLog(isSuccess, responseCode, responseMessage, smsRequest, smsLog) {
|
214 | return __awaiter(this, void 0, void 0, function* () {
|
215 | smsLog.targetMobile = smsRequest.mobile.join();
|
216 | if (smsRequest.templateParam.length !== 0) {
|
217 | smsLog.validationCode = parseInt(smsRequest.templateParam[0]);
|
218 | smsLog.validationTime = parseInt(smsRequest.templateParam[1]);
|
219 | }
|
220 | smsLog.isSuccess = isSuccess;
|
221 | smsLog.responseCode = responseCode;
|
222 | smsLog.responseMessage = responseMessage;
|
223 | smsLog.sendTime = moment().format("YYYY-MM-DD HH:mm:ss");
|
224 | const queryRunner = typeorm_2.getConnection().createQueryRunner();
|
225 | yield queryRunner.startTransaction();
|
226 | try {
|
227 | const newLog = yield queryRunner.manager.save(smsLog);
|
228 | yield queryRunner.manager.createQueryBuilder().relation(sms_template_entity_1.SmsTemplate, "smsLogs").of(smsRequest.templateId).add(newLog);
|
229 | yield queryRunner.commitTransaction();
|
230 | }
|
231 | catch (error) {
|
232 | yield queryRunner.rollbackTransaction();
|
233 | throw new common_1.HttpException(`数据库错误:${error.toString()}`, 501);
|
234 | }
|
235 | });
|
236 | }
|
237 | };
|
238 | SmsService = __decorate([
|
239 | common_1.Injectable(),
|
240 | __param(0, common_1.Inject(qcloud_service_1.QcloudService)),
|
241 | __param(1, common_1.Inject(param_util_1.ParamUtil)),
|
242 | __param(2, typeorm_1.InjectRepository(sms_entity_1.Sms)),
|
243 | __param(3, typeorm_1.InjectRepository(sms_template_entity_1.SmsTemplate)),
|
244 | __param(4, typeorm_1.InjectRepository(sms_log_entity_1.SmsLog)),
|
245 | __metadata("design:paramtypes", [qcloud_service_1.QcloudService,
|
246 | param_util_1.ParamUtil,
|
247 | typeorm_2.Repository,
|
248 | typeorm_2.Repository,
|
249 | typeorm_2.Repository])
|
250 | ], SmsService);
|
251 | exports.SmsService = SmsService;
|
252 |
|
253 |
|