去除支付与短信依赖,改为插件化

This commit is contained in:
cool 2024-02-05 15:00:19 +08:00
parent 913e3089a9
commit df43853ccd
6 changed files with 62 additions and 20 deletions

View File

@ -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",

View File

@ -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,
{

View File

@ -34,6 +34,8 @@ import { PluginService } from '../../service/info';
'a.description',
'a.pluginJson',
'a.config',
'a.createTime',
'a.updateTime',
],
},
})

View File

@ -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;
}
}

View File

@ -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('插件不存在或已禁用');
}
}

View File

@ -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('发送过于频繁,请稍后再试');