'use strict'; const Service = require('egg').Service; const apiLogSink = require('../lib/api_log_sink'); class pdaService extends Service { async callMethod(className, actionName, requestData, authHeader) { try { // 构建服务类路径 const servicePath = `${className}`; const classs = servicePath.split('.'); let systemMethods = this.ctx.service.systemMethods; for (let i = 0; i < classs.length; i++) { const className = classs[i]; systemMethods = systemMethods[className]; } // 检查服务是否存在 if (!systemMethods) { throw new Error(`服务 ${servicePath} 不存在`); } const serviceInstance = systemMethods; // 检查方法是否存在 if (typeof serviceInstance[actionName] !== 'function') { throw new Error(`方法 ${actionName} 不存在`); } const result = { message: '', status: 401, }; // 调用方法 const authPass = await serviceInstance.checkAuth(servicePath, actionName, authHeader); const isLog = authPass.isLog; if (authPass.status === 200) { result.data = await serviceInstance[actionName](requestData); result.status = 200; } else { result.message = authPass.message; result.status = authPass.status; } return { isLog, result }; } catch (error) { throw error; } } validateParams(params) { const required = [ 'method', 'v', 'timestamp' ]; for (const field of required) { if (!params[field]) { return false; } } // 验证method格式 if (typeof params.method !== 'string' || !params.method.includes('.')) { return false; } return true; } /** * 保存接口日志。 * * 经 Egg logger(apiLog)平铺写出到日志文件;wms_system_log 表已下线,不再有任何入库路径。 * * @param {Object} logData 日志数据 * @param {number} startTime 开始时间戳 * @return {Promise} 写出的平铺记录 */ async saveLog(logData, startTime) { const { ctx, } = this; try { return await apiLogSink.write(ctx.app, { logData, startTime, device: 'pda', }); } catch (error) { ctx.logger.error('保存日志失败: ' + error.message); // 兜底:至少把原始数据落到 app 日志,避免整条丢失 ctx.logger.info('日志数据: ', JSON.stringify(logData)); } } } module.exports = pdaService;