'use strict'; const Service = require('egg').Service; const { XMLParser } = require('fast-xml-parser'); class ApiService extends Service { /** * 调用业务方法 * @param className * @param actionName * @param jsonData * @param format */ async callMethod(className, actionName, jsonData, format = 'json') { try { // 构建服务类路径 const servicePath = `${className}`; const classs = servicePath.split('.'); let apiMethods = this.ctx.service.apiMethods; for (let i = 0; i < classs.length; i++) { const className = classs[i]; apiMethods = apiMethods[className]; } // 检查服务是否存在 if (!apiMethods) { throw new Error(`服务 ${servicePath} 不存在`); } const serviceInstance = apiMethods; // 检查方法是否存在 if (typeof serviceInstance[actionName] !== 'function') { throw new Error(`方法 ${actionName} 不存在`); } // 调用方法 const result = await serviceInstance[actionName](jsonData); // 根据format参数返回相应格式 return result; } catch (error) { throw error; } } } module.exports = ApiService;