前端转vue
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PluginHandle.ts 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // 模拟引入相关模块,实际需要根据项目结构调整
  2. interface TransitEntity {
  3. ServiceName: string;
  4. FuncName: string;
  5. Parameters: { [key: string]: string };
  6. }
  7. interface AttachFile {
  8. AttachValue: any;
  9. }
  10. // 模拟服务管理器类
  11. class ServiceManager {
  12. static getInstance() {
  13. return new ServiceManager();
  14. }
  15. Commit(entity: TransitEntity): AttachFile[] | null {
  16. // 这里是模拟实现,实际需要根据业务逻辑实现
  17. return null;
  18. }
  19. }
  20. // 模拟日志类
  21. class PlatformLogger {
  22. static CommunicationInfo(message: string, category: string) {
  23. // 这里是模拟实现,实际需要根据业务逻辑实现
  24. }
  25. static SystemErrorInfo(message: string, ex: Error) {
  26. // 这里是模拟实现,实际需要根据业务逻辑实现
  27. }
  28. }
  29. // 定义 RestfulItem 类
  30. class RestfulItem {
  31. Prefix: string;
  32. Method: string;
  33. Url: string;
  34. Data: string;
  35. Headers: { [key: string]: string };
  36. Resp: [number, string] | null;
  37. constructor() {
  38. this.Prefix = "";
  39. this.Method = "";
  40. this.Url = "";
  41. this.Data = "";
  42. this.Headers = {};
  43. this.Resp = null;
  44. }
  45. }
  46. // 定义 PluginHandle 类
  47. export class PluginHandle {
  48. /**
  49. * 调用插件,使用简洁模式,1输入,1输出
  50. * @param pluginName 插件名称
  51. * @param methodName 方法名称
  52. * @param input 输入数据
  53. * @returns [是否成功, 输出数据]
  54. */
  55. static CallPlugin2Simple(pluginName: string, methodName: string, input: string): [boolean, string] {
  56. let output = "";
  57. try {
  58. PlatformLogger.CommunicationInfo(`插件调用【${pluginName}.${methodName}】,输入数据:\n${input}`, "CallPlugin");
  59. const entity: TransitEntity = {
  60. ServiceName: "UtilService",
  61. FuncName: "CallPlugin",
  62. Parameters: { "1": pluginName, "2": methodName, "3": input }
  63. };
  64. const tmpresult = ServiceManager.getInstance().Commit(entity);
  65. if (tmpresult && tmpresult.length === 1) {
  66. output = tmpresult[0].AttachValue.toString();
  67. return [true, output];
  68. }
  69. } catch (ex) {
  70. if (ex instanceof Error) {
  71. PlatformLogger.SystemErrorInfo("PluginHandle.CallPlugin:发生异常!", ex);
  72. }
  73. } finally {
  74. PlatformLogger.CommunicationInfo(`插件调用【${pluginName}.${methodName}】,输出数据:\n${output}`, "CallPlugin");
  75. }
  76. return [false, ""];
  77. }
  78. /**
  79. * 调用Restful插件
  80. * @param item Restful 插件调用信息
  81. * @returns 是否调用成功
  82. */
  83. static Restful(item: RestfulItem): boolean {
  84. try {
  85. // 参数输入
  86. const dict = {
  87. "prefix": item.Prefix,
  88. "method": item.Method,
  89. "url": item.Url,
  90. "data": item.Data,
  91. "headers": item.Headers
  92. };
  93. // 调用插件
  94. const [success, output] = this.CallPlugin2Simple("restful-1.0-SNAPSHOT", "com.dhcc.cloud.plugins.restful.method.Restful", JSON.stringify(dict));
  95. if (success) {
  96. const resultDict = JSON.parse(output) as { [key: string]: any };
  97. item.Resp = [parseInt(resultDict["code"].toString()), resultDict["data"].toString()];
  98. return true;
  99. }
  100. } catch (ex) {
  101. if (ex instanceof Error) {
  102. PlatformLogger.SystemErrorInfo("PluginHandle.Restful:发生异常!", ex);
  103. }
  104. }
  105. return false;
  106. }
  107. /**
  108. * 测试Api
  109. * @param name 测试名称
  110. * @returns 测试结果
  111. */
  112. static Test(name: string): string | null {
  113. const [flag, output] = this.CallPlugin2Simple("simple-demo-1.0-SNAPSHOT", "com.dhcc.cloud.plugins.simple_demo.DemoMethod", name);
  114. return flag ? output : null;
  115. }
  116. }