// 模拟引入相关模块,实际需要根据项目结构调整 interface TransitEntity { ServiceName: string; FuncName: string; Parameters: { [key: string]: string }; } interface AttachFile { AttachValue: any; } // 模拟服务管理器类 class ServiceManager { static getInstance() { return new ServiceManager(); } Commit(entity: TransitEntity): AttachFile[] | null { // 这里是模拟实现,实际需要根据业务逻辑实现 return null; } } // 模拟日志类 class PlatformLogger { static CommunicationInfo(message: string, category: string) { // 这里是模拟实现,实际需要根据业务逻辑实现 } static SystemErrorInfo(message: string, ex: Error) { // 这里是模拟实现,实际需要根据业务逻辑实现 } } // 定义 RestfulItem 类 class RestfulItem { Prefix: string; Method: string; Url: string; Data: string; Headers: { [key: string]: string }; Resp: [number, string] | null; constructor() { this.Prefix = ""; this.Method = ""; this.Url = ""; this.Data = ""; this.Headers = {}; this.Resp = null; } } // 定义 PluginHandle 类 export class PluginHandle { /** * 调用插件,使用简洁模式,1输入,1输出 * @param pluginName 插件名称 * @param methodName 方法名称 * @param input 输入数据 * @returns [是否成功, 输出数据] */ static CallPlugin2Simple(pluginName: string, methodName: string, input: string): [boolean, string] { let output = ""; try { PlatformLogger.CommunicationInfo(`插件调用【${pluginName}.${methodName}】,输入数据:\n${input}`, "CallPlugin"); const entity: TransitEntity = { ServiceName: "UtilService", FuncName: "CallPlugin", Parameters: { "1": pluginName, "2": methodName, "3": input } }; const tmpresult = ServiceManager.getInstance().Commit(entity); if (tmpresult && tmpresult.length === 1) { output = tmpresult[0].AttachValue.toString(); return [true, output]; } } catch (ex) { if (ex instanceof Error) { PlatformLogger.SystemErrorInfo("PluginHandle.CallPlugin:发生异常!", ex); } } finally { PlatformLogger.CommunicationInfo(`插件调用【${pluginName}.${methodName}】,输出数据:\n${output}`, "CallPlugin"); } return [false, ""]; } /** * 调用Restful插件 * @param item Restful 插件调用信息 * @returns 是否调用成功 */ static Restful(item: RestfulItem): boolean { try { // 参数输入 const dict = { "prefix": item.Prefix, "method": item.Method, "url": item.Url, "data": item.Data, "headers": item.Headers }; // 调用插件 const [success, output] = this.CallPlugin2Simple("restful-1.0-SNAPSHOT", "com.dhcc.cloud.plugins.restful.method.Restful", JSON.stringify(dict)); if (success) { const resultDict = JSON.parse(output) as { [key: string]: any }; item.Resp = [parseInt(resultDict["code"].toString()), resultDict["data"].toString()]; return true; } } catch (ex) { if (ex instanceof Error) { PlatformLogger.SystemErrorInfo("PluginHandle.Restful:发生异常!", ex); } } return false; } /** * 测试Api * @param name 测试名称 * @returns 测试结果 */ static Test(name: string): string | null { const [flag, output] = this.CallPlugin2Simple("simple-demo-1.0-SNAPSHOT", "com.dhcc.cloud.plugins.simple_demo.DemoMethod", name); return flag ? output : null; } }