优化插件校验

This commit is contained in:
cool 2024-01-30 16:02:33 +08:00
parent bb5d19841c
commit 4c9ae93d21
2 changed files with 18 additions and 2 deletions

View File

@ -1,5 +1,5 @@
import { BaseEntity } from '@cool-midway/core';
import { Column, Entity, DataSource } from 'typeorm';
import { Column, Entity, DataSource, Index } from 'typeorm';
console.log(DataSource);
@ -14,6 +14,7 @@ export class PluginInfoEntity extends BaseEntity {
@Column({ comment: '简介' })
description: string;
@Index()
@Column({ comment: 'Key名' })
keyName: string;

View File

@ -1,7 +1,7 @@
import { App, Inject, Provide } from '@midwayjs/decorator';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Not, Repository } from 'typeorm';
import { Equal, Not, Repository } from 'typeorm';
import { PluginInfoEntity } from '../entity/info';
import { IMidwayApplication, IMidwayContext } from '@midwayjs/core';
import * as _ from 'lodash';
@ -59,6 +59,7 @@ export class PluginService extends BaseService {
* @returns
*/
async invoke(key: string, method: string, ...params) {
await this.checkStatus(key);
// 实例化
const instance = new (await this.pluginCenterService.plugins.get(key))();
await instance.init(
@ -69,6 +70,20 @@ export class PluginService extends BaseService {
return await instance[method](...params);
}
/**
*
* @param key
*/
async checkStatus(key: string) {
const info = await this.pluginInfoEntity.findOneBy({
keyName: Equal(key),
status: 0,
});
if (info) {
throw new CoolCommException('插件不存在或已禁用');
}
}
/**
*
* @param filePath