From df43853ccda9227f482d432c8005b943b13b79a3 Mon Sep 17 00:00:00 2001 From: cool Date: Mon, 5 Feb 2024 15:00:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E6=94=AF=E4=BB=98=E4=B8=8E?= =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E4=BE=9D=E8=B5=96=EF=BC=8C=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 -- src/configuration.ts | 6 ----- src/modules/plugin/controller/admin/info.ts | 2 ++ src/modules/plugin/service/center.ts | 29 +++++++++++++++++---- src/modules/plugin/service/info.ts | 26 +++++++++++++++--- src/modules/user/service/sms.ts | 17 +++++++++--- 6 files changed, 62 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index e65d692..61984eb 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,7 @@ "@cool-midway/core": "^7.1.3", "@cool-midway/file": "^7.0.5", "@cool-midway/iot": "^7.0.0", - "@cool-midway/pay": "^7.0.0", "@cool-midway/rpc": "^7.0.0", - "@cool-midway/sms": "^7.0.1", "@cool-midway/task": "^7.0.0", "@midwayjs/bootstrap": "^3.14.4", "@midwayjs/cache": "^3.14.0", diff --git a/src/configuration.ts b/src/configuration.ts index e432eed..5b7eb47 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -11,13 +11,11 @@ import * as localTask from '@midwayjs/task'; import * as cool from '@cool-midway/core'; import * as cloud from '@cool-midway/cloud'; import * as file from '@cool-midway/file'; -import * as sms from '@cool-midway/sms'; import { ILogger } from '@midwayjs/logger'; import { IMidwayApplication } from '@midwayjs/core'; // import * as swagger from '@midwayjs/swagger'; // import * as rpc from '@cool-midway/rpc'; // import * as task from '@cool-midway/task'; -// import * as pay from '@cool-midway/pay'; // import * as iot from '@cool-midway/iot'; @Configuration({ @@ -46,12 +44,8 @@ import { IMidwayApplication } from '@midwayjs/core'; // task, // cool-admin 云开发组件 cloud, - // 支付(微信、支付宝) https://cool-js.com/admin/node/core/pay.html - // pay, // 物联网开发,如MQTT支持等 // iot, - // 短信 - sms, // swagger 文档 // swagger, { diff --git a/src/modules/plugin/controller/admin/info.ts b/src/modules/plugin/controller/admin/info.ts index 4f867cc..bb9e3bf 100644 --- a/src/modules/plugin/controller/admin/info.ts +++ b/src/modules/plugin/controller/admin/info.ts @@ -34,6 +34,8 @@ import { PluginService } from '../../service/info'; 'a.description', 'a.pluginJson', 'a.config', + 'a.createTime', + 'a.updateTime', ], }, }) diff --git a/src/modules/plugin/service/center.ts b/src/modules/plugin/service/center.ts index a981958..dba4c42 100644 --- a/src/modules/plugin/service/center.ts +++ b/src/modules/plugin/service/center.ts @@ -12,6 +12,7 @@ import { PluginInfoEntity } from '../entity/info'; import { InjectEntityModel } from '@midwayjs/typeorm'; import { Repository } from 'typeorm'; import { PluginInfo } from '../interface'; +import * as _ from 'lodash'; /** * 插件中心 @@ -80,7 +81,7 @@ export class PluginCenterService { const instance = await this.getInstance(plugin.content.data); this.pluginInfos.set(plugin.keyName, { ...plugin.pluginJson, - config: plugin.config, + config: this.getConfig(plugin.config), }); if (plugin.hook) { await this.register(plugin.hook, instance); @@ -90,6 +91,23 @@ export class PluginCenterService { } } + /** + * 获得配置 + * @param config + * @returns + */ + private getConfig(config: any) { + const env = this.app.getEnv(); + let isMulti = false; + for (const key in config) { + if (key.includes('@')) { + isMulti = true; + break; + } + } + return isMulti ? config[`@${env}`] : config; + } + /** * 获得实例 * @param content @@ -97,10 +115,11 @@ export class PluginCenterService { */ async getInstance(content: string) { let _instance; - eval(` - ${content} - _instance = Plugin; - `); + const script = ` + ${content} + _instance = Plugin; + `; + eval(script); return _instance; } } diff --git a/src/modules/plugin/service/info.ts b/src/modules/plugin/service/info.ts index f4e557c..a4f5f15 100644 --- a/src/modules/plugin/service/info.ts +++ b/src/modules/plugin/service/info.ts @@ -51,6 +51,14 @@ export class PluginService extends BaseService { await super.update(param); } + /** + * 获得插件配置 + * @param key + */ + async getConfig(key: string) { + return this.pluginCenterService.pluginInfos.get(key)?.config; + } + /** * 调用插件 * @param key 插件key @@ -59,15 +67,25 @@ export class PluginService extends BaseService { * @returns */ async invoke(key: string, method: string, ...params) { + // 实例 + const instance = await this.getInstance(key); + return await instance[method](...params); + } + + /** + * 获得插件实例 + * @param key + * @returns + */ + async getInstance(key: string) { await this.checkStatus(key); - // 实例化 const instance = new (await this.pluginCenterService.plugins.get(key))(); await instance.init( this.pluginCenterService.pluginInfos.get(key), this.ctx, this.app ); - return await instance[method](...params); + return instance; } /** @@ -77,9 +95,9 @@ export class PluginService extends BaseService { async checkStatus(key: string) { const info = await this.pluginInfoEntity.findOneBy({ keyName: Equal(key), - status: 0, + status: 1, }); - if (info) { + if (!info) { throw new CoolCommException('插件不存在或已禁用'); } } diff --git a/src/modules/user/service/sms.ts b/src/modules/user/service/sms.ts index 0afc4b9..44ee608 100644 --- a/src/modules/user/service/sms.ts +++ b/src/modules/user/service/sms.ts @@ -2,7 +2,7 @@ import { Provide, Config, Inject } from '@midwayjs/decorator'; import { BaseService, CoolCommException } from '@cool-midway/core'; import * as _ from 'lodash'; import { CacheManager } from '@midwayjs/cache'; -import { CoolSms } from '@cool-midway/sms'; +import { PluginService } from '../../plugin/service/info'; /** * 描述 @@ -17,15 +17,26 @@ export class UserSmsService extends BaseService { cacheManager: CacheManager; @Inject() - coolSms: CoolSms; + pluginService: PluginService; /** * 发送验证码 * @param phone */ async sendSms(phone) { + // 随机四位验证码 + const code = _.random(1000, 9999); + const pluginKey = this.config.pluginKey; + if (!pluginKey) throw new CoolCommException('未配置短信插件'); try { - const code = await this.coolSms.sendCode(phone); + if (pluginKey == 'sms-tx') { + await this.pluginService.invoke('sms-tx', 'send', [code], [code]); + } + if (pluginKey == 'sms-ali') { + await this.pluginService.invoke('sms-ali', 'send', [phone], { + code, + }); + } this.cacheManager.set(`sms:${phone}`, code, this.config.timeout); } catch (error) { throw new CoolCommException('发送过于频繁,请稍后再试');