From ed72f546d4b2ddff69589325db5c276dcbeff782 Mon Sep 17 00:00:00 2001 From: cool Date: Wed, 21 Feb 2024 15:19:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=A0=A1=E9=AA=8C=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/user/service/sms.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/modules/user/service/sms.ts b/src/modules/user/service/sms.ts index 44ee608..2508b62 100644 --- a/src/modules/user/service/sms.ts +++ b/src/modules/user/service/sms.ts @@ -1,4 +1,4 @@ -import { Provide, Config, Inject } from '@midwayjs/decorator'; +import { Provide, Config, Inject, Init } from '@midwayjs/decorator'; import { BaseService, CoolCommException } from '@cool-midway/core'; import * as _ from 'lodash'; import { CacheManager } from '@midwayjs/cache'; @@ -19,6 +19,23 @@ export class UserSmsService extends BaseService { @Inject() pluginService: PluginService; + plugin; + + @Init() + async init() { + for (const key of ['sms-tx', 'sms-ali']) { + try { + this.plugin = await this.pluginService.getInstance(key); + if (this.plugin) { + this.config.pluginKey = key; + break; + } + } catch (e) { + continue; + } + } + } + /** * 发送验证码 * @param phone @@ -27,13 +44,13 @@ export class UserSmsService extends BaseService { // 随机四位验证码 const code = _.random(1000, 9999); const pluginKey = this.config.pluginKey; - if (!pluginKey) throw new CoolCommException('未配置短信插件'); + if (!this.plugin) throw new CoolCommException('未配置短信插件'); try { if (pluginKey == 'sms-tx') { - await this.pluginService.invoke('sms-tx', 'send', [code], [code]); + await this.plugin.send([phone], [code]); } if (pluginKey == 'sms-ali') { - await this.pluginService.invoke('sms-ali', 'send', [phone], { + await this.plugin.send([phone], { code, }); }