【定时任务】sync_outbound_orders_to_delivery: 执行日志写缓存 + 错误详情提取(parent/validation) 【基础设施】system.js: snake_case→camelCase 服务路由转换(兼容 express_interface 等) 【看板】dashboard: queryWarehouseStats 支持 storeId 货主维度过滤 【订单基类】order/base: getOrderModel 兼容无上下文时传入 warehouseId(货主侧场景) 【库存】inventory: 全量移库软删除→硬删除(force destroy)释放唯一键 + 唯一约束/校验错误友好提示 【快递平台】express_platform: KS(快手)平台 receiver.phone 不传(平台要求) 【迁移】delivery 自增起点 + supervisor CS 权限修复
210 lines
5.7 KiB
JavaScript
210 lines
5.7 KiB
JavaScript
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
const ALLOWED_PLATFORMS = new Set([ 'web', 'pda', 'app', 'wx', 'miniprogram', 'other', 'workerstation', 'workstation' ]);
|
||
// project: all=全部, warehouse=仓库, store=货主, tms=物流
|
||
const ALLOWED_PROJECTS = new Set([ 'all', 'warehouse', 'store', 'tms', 'wms', 'scm', 'other' ]);
|
||
|
||
class systemService 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 rawClassName = classs[i];
|
||
// Egg.js 将文件名 snake_case 自动转为 camelCase,需做对应转换
|
||
const camelName = rawClassName.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
||
systemMethods = systemMethods[camelName] || systemMethods[rawClassName];
|
||
}
|
||
// 检查服务是否存在
|
||
if (!systemMethods) {
|
||
throw new Error(`服务 ${servicePath} 不存在`);
|
||
}
|
||
let serviceInstance = systemMethods;
|
||
|
||
if (typeof serviceInstance[actionName] !== 'function' && serviceInstance?.index) {
|
||
serviceInstance = serviceInstance.index;
|
||
}
|
||
|
||
// 检查方法是否存在
|
||
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;
|
||
}
|
||
|
||
validateRequestContext(params = {}, headers = {}) {
|
||
if (!this.validateParams(params)) {
|
||
return {
|
||
ok: false,
|
||
status: 400,
|
||
code: 400,
|
||
message: '参数缺失或错误: method/v/timestamp',
|
||
};
|
||
}
|
||
|
||
const rawTimestamp = String(params.timestamp || '').trim();
|
||
if (!/^\d+$/.test(rawTimestamp)) {
|
||
return {
|
||
ok: false,
|
||
status: 400,
|
||
code: 400,
|
||
message: 'timestamp 必须为数字时间戳',
|
||
};
|
||
}
|
||
|
||
const timestampNum = Number(rawTimestamp);
|
||
if (!Number.isFinite(timestampNum) || timestampNum <= 0) {
|
||
return {
|
||
ok: false,
|
||
status: 400,
|
||
code: 400,
|
||
message: 'timestamp 非法',
|
||
};
|
||
}
|
||
|
||
const platform = String(headers.platform || '').trim();
|
||
const project = String(headers.project || '').trim();
|
||
const authorization = String(headers.authorization || '').trim();
|
||
|
||
if (!platform) {
|
||
return {
|
||
ok: false,
|
||
status: 400,
|
||
code: 400,
|
||
message: '缺少请求头 platform',
|
||
};
|
||
}
|
||
|
||
if (!project) {
|
||
return {
|
||
ok: false,
|
||
status: 400,
|
||
code: 400,
|
||
message: '缺少请求头 project',
|
||
};
|
||
}
|
||
|
||
if (!ALLOWED_PLATFORMS.has(platform.toLowerCase())) {
|
||
return {
|
||
ok: false,
|
||
status: 400,
|
||
code: 400,
|
||
message: `platform 不支持: ${platform}`,
|
||
};
|
||
}
|
||
|
||
if (!ALLOWED_PROJECTS.has(project.toLowerCase())) {
|
||
return {
|
||
ok: false,
|
||
status: 400,
|
||
code: 400,
|
||
message: `project 不支持: ${project}`,
|
||
};
|
||
}
|
||
|
||
if (authorization && !authorization.startsWith('Bearer ')) {
|
||
return {
|
||
ok: false,
|
||
status: 401,
|
||
code: 401,
|
||
message: 'Authorization 格式错误,应为 Bearer <token>',
|
||
};
|
||
}
|
||
|
||
return {
|
||
ok: true,
|
||
status: 200,
|
||
code: 0,
|
||
message: 'ok',
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 保存接口日志到系统日志模型
|
||
* @param {Object} logData 日志数据
|
||
* @param {number} startTime 开始时间戳
|
||
* @return {Promise<Object|undefined>} 写入结果
|
||
*/
|
||
async saveLog(logData, startTime) {
|
||
const {
|
||
ctx,
|
||
} = this;
|
||
|
||
try {
|
||
// 限制日志内容长度
|
||
if (logData.body && logData.body.length > 65535) {
|
||
logData.body = logData.body.substring(0, 65535);
|
||
}
|
||
|
||
if (logData.result && logData.result.length > 65535) {
|
||
logData.result = logData.result.substring(0, 65535);
|
||
}
|
||
|
||
logData.use_time = Date.now() - startTime;
|
||
|
||
// 使用 SystemLog model 保存日志到数据库
|
||
if (!ctx.model || !ctx.model.SystemLog) {
|
||
throw new Error('SystemLog model 不存在,无法写入系统日志');
|
||
}
|
||
const result = await ctx.model.SystemLog.create({
|
||
method: logData.method || '',
|
||
v: logData.v || '',
|
||
url: logData.url || '',
|
||
headers: logData.headers || '',
|
||
auth: logData.auth || '',
|
||
created_at: new Date(),
|
||
body: logData.body || '',
|
||
result: logData.result || '',
|
||
use_time: logData.use_time || 0,
|
||
store_id: logData.store_id || null,
|
||
device: ctx.header.platform,
|
||
});
|
||
return result;
|
||
} catch (error) {
|
||
ctx.logger.error('保存奇门接口日志失败: ' + error.message);
|
||
// 可以选择记录到文件日志作为备用
|
||
ctx.logger.info('奇门接口日志数据: ', JSON.stringify(logData));
|
||
}
|
||
}
|
||
}
|
||
module.exports = systemService;
|