'use strict'; const Service = require('egg').Service; class ApiService extends Service { /** * 调用业务方法 * @param {string} className 服务类路径 * @param {string} actionName 方法名 * @param {Object} jsonData 请求数据 */ async callMethod(className, actionName, jsonData) { 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;