mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-15 03:42:51 +00:00
去除支付与短信依赖,改为插件化
This commit is contained in:
parent
913e3089a9
commit
df43853ccd
@ -9,9 +9,7 @@
|
|||||||
"@cool-midway/core": "^7.1.3",
|
"@cool-midway/core": "^7.1.3",
|
||||||
"@cool-midway/file": "^7.0.5",
|
"@cool-midway/file": "^7.0.5",
|
||||||
"@cool-midway/iot": "^7.0.0",
|
"@cool-midway/iot": "^7.0.0",
|
||||||
"@cool-midway/pay": "^7.0.0",
|
|
||||||
"@cool-midway/rpc": "^7.0.0",
|
"@cool-midway/rpc": "^7.0.0",
|
||||||
"@cool-midway/sms": "^7.0.1",
|
|
||||||
"@cool-midway/task": "^7.0.0",
|
"@cool-midway/task": "^7.0.0",
|
||||||
"@midwayjs/bootstrap": "^3.14.4",
|
"@midwayjs/bootstrap": "^3.14.4",
|
||||||
"@midwayjs/cache": "^3.14.0",
|
"@midwayjs/cache": "^3.14.0",
|
||||||
|
|||||||
@ -11,13 +11,11 @@ import * as localTask from '@midwayjs/task';
|
|||||||
import * as cool from '@cool-midway/core';
|
import * as cool from '@cool-midway/core';
|
||||||
import * as cloud from '@cool-midway/cloud';
|
import * as cloud from '@cool-midway/cloud';
|
||||||
import * as file from '@cool-midway/file';
|
import * as file from '@cool-midway/file';
|
||||||
import * as sms from '@cool-midway/sms';
|
|
||||||
import { ILogger } from '@midwayjs/logger';
|
import { ILogger } from '@midwayjs/logger';
|
||||||
import { IMidwayApplication } from '@midwayjs/core';
|
import { IMidwayApplication } from '@midwayjs/core';
|
||||||
// import * as swagger from '@midwayjs/swagger';
|
// import * as swagger from '@midwayjs/swagger';
|
||||||
// import * as rpc from '@cool-midway/rpc';
|
// import * as rpc from '@cool-midway/rpc';
|
||||||
// import * as task from '@cool-midway/task';
|
// import * as task from '@cool-midway/task';
|
||||||
// import * as pay from '@cool-midway/pay';
|
|
||||||
// import * as iot from '@cool-midway/iot';
|
// import * as iot from '@cool-midway/iot';
|
||||||
|
|
||||||
@Configuration({
|
@Configuration({
|
||||||
@ -46,12 +44,8 @@ import { IMidwayApplication } from '@midwayjs/core';
|
|||||||
// task,
|
// task,
|
||||||
// cool-admin 云开发组件
|
// cool-admin 云开发组件
|
||||||
cloud,
|
cloud,
|
||||||
// 支付(微信、支付宝) https://cool-js.com/admin/node/core/pay.html
|
|
||||||
// pay,
|
|
||||||
// 物联网开发,如MQTT支持等
|
// 物联网开发,如MQTT支持等
|
||||||
// iot,
|
// iot,
|
||||||
// 短信
|
|
||||||
sms,
|
|
||||||
// swagger 文档
|
// swagger 文档
|
||||||
// swagger,
|
// swagger,
|
||||||
{
|
{
|
||||||
|
|||||||
@ -34,6 +34,8 @@ import { PluginService } from '../../service/info';
|
|||||||
'a.description',
|
'a.description',
|
||||||
'a.pluginJson',
|
'a.pluginJson',
|
||||||
'a.config',
|
'a.config',
|
||||||
|
'a.createTime',
|
||||||
|
'a.updateTime',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import { PluginInfoEntity } from '../entity/info';
|
|||||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { PluginInfo } from '../interface';
|
import { PluginInfo } from '../interface';
|
||||||
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 插件中心
|
* 插件中心
|
||||||
@ -80,7 +81,7 @@ export class PluginCenterService {
|
|||||||
const instance = await this.getInstance(plugin.content.data);
|
const instance = await this.getInstance(plugin.content.data);
|
||||||
this.pluginInfos.set(plugin.keyName, {
|
this.pluginInfos.set(plugin.keyName, {
|
||||||
...plugin.pluginJson,
|
...plugin.pluginJson,
|
||||||
config: plugin.config,
|
config: this.getConfig(plugin.config),
|
||||||
});
|
});
|
||||||
if (plugin.hook) {
|
if (plugin.hook) {
|
||||||
await this.register(plugin.hook, instance);
|
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
|
* @param content
|
||||||
@ -97,10 +115,11 @@ export class PluginCenterService {
|
|||||||
*/
|
*/
|
||||||
async getInstance(content: string) {
|
async getInstance(content: string) {
|
||||||
let _instance;
|
let _instance;
|
||||||
eval(`
|
const script = `
|
||||||
${content}
|
${content}
|
||||||
_instance = Plugin;
|
_instance = Plugin;
|
||||||
`);
|
`;
|
||||||
|
eval(script);
|
||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -51,6 +51,14 @@ export class PluginService extends BaseService {
|
|||||||
await super.update(param);
|
await super.update(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得插件配置
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
async getConfig(key: string) {
|
||||||
|
return this.pluginCenterService.pluginInfos.get(key)?.config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用插件
|
* 调用插件
|
||||||
* @param key 插件key
|
* @param key 插件key
|
||||||
@ -59,15 +67,25 @@ export class PluginService extends BaseService {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async invoke(key: string, method: string, ...params) {
|
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);
|
await this.checkStatus(key);
|
||||||
// 实例化
|
|
||||||
const instance = new (await this.pluginCenterService.plugins.get(key))();
|
const instance = new (await this.pluginCenterService.plugins.get(key))();
|
||||||
await instance.init(
|
await instance.init(
|
||||||
this.pluginCenterService.pluginInfos.get(key),
|
this.pluginCenterService.pluginInfos.get(key),
|
||||||
this.ctx,
|
this.ctx,
|
||||||
this.app
|
this.app
|
||||||
);
|
);
|
||||||
return await instance[method](...params);
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,9 +95,9 @@ export class PluginService extends BaseService {
|
|||||||
async checkStatus(key: string) {
|
async checkStatus(key: string) {
|
||||||
const info = await this.pluginInfoEntity.findOneBy({
|
const info = await this.pluginInfoEntity.findOneBy({
|
||||||
keyName: Equal(key),
|
keyName: Equal(key),
|
||||||
status: 0,
|
status: 1,
|
||||||
});
|
});
|
||||||
if (info) {
|
if (!info) {
|
||||||
throw new CoolCommException('插件不存在或已禁用');
|
throw new CoolCommException('插件不存在或已禁用');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { Provide, Config, Inject } from '@midwayjs/decorator';
|
|||||||
import { BaseService, CoolCommException } from '@cool-midway/core';
|
import { BaseService, CoolCommException } from '@cool-midway/core';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
import { CacheManager } from '@midwayjs/cache';
|
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;
|
cacheManager: CacheManager;
|
||||||
|
|
||||||
@Inject()
|
@Inject()
|
||||||
coolSms: CoolSms;
|
pluginService: PluginService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送验证码
|
* 发送验证码
|
||||||
* @param phone
|
* @param phone
|
||||||
*/
|
*/
|
||||||
async sendSms(phone) {
|
async sendSms(phone) {
|
||||||
|
// 随机四位验证码
|
||||||
|
const code = _.random(1000, 9999);
|
||||||
|
const pluginKey = this.config.pluginKey;
|
||||||
|
if (!pluginKey) throw new CoolCommException('未配置短信插件');
|
||||||
try {
|
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);
|
this.cacheManager.set(`sms:${phone}`, code, this.config.timeout);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new CoolCommException('发送过于频繁,请稍后再试');
|
throw new CoolCommException('发送过于频繁,请稍后再试');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user