- 新增步骤 5.5:启动前通过 ss + fuser -k 强制释放目标端口残留进程 - 健康检查重试次数从 5 次增加到 10 次,最长等待从 10s 延长至 20s - 健康检查失败日志增加启动日志路径提示
275 lines
7.1 KiB
JavaScript
275 lines
7.1 KiB
JavaScript
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
const WAREHOUSE_INTERFACE_TYPE_ID = 3;
|
||
|
||
class WarehouseService extends Service {
|
||
/**
|
||
* 解析 warehouse method,拆分为服务路径与动作名。
|
||
* @param {string} methodName method 名称。
|
||
* @return {{ servicePath: string, actionName: string }} 解析结果。
|
||
*/
|
||
resolveWarehouseMethod(methodName = '') {
|
||
const method = String(methodName || '').trim();
|
||
const parts = method.split('.').filter(Boolean);
|
||
const actionName = parts.pop() || '';
|
||
const servicePath = parts.join('.');
|
||
|
||
return {
|
||
servicePath,
|
||
actionName,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 根据服务路径查找 warehouseMethods 下的目标服务。
|
||
* @param {string} servicePath 服务路径。
|
||
* @return {Object|null} 目标服务实例。
|
||
*/
|
||
getWarehouseMethodService(servicePath = '') {
|
||
const { ctx } = this;
|
||
const pathSegments = String(servicePath || '').split('.').filter(Boolean);
|
||
let methodService = ctx.service.warehouseMethods;
|
||
|
||
if (pathSegments.length === 0) {
|
||
return methodService;
|
||
}
|
||
|
||
for (const segment of pathSegments) {
|
||
methodService = methodService && methodService[segment];
|
||
}
|
||
|
||
return methodService || null;
|
||
}
|
||
|
||
/**
|
||
* 分发 warehouse method 到 warehouseMethods 目录。
|
||
* 分发前统一注入仓库上下文(warehouseId/appKey)。
|
||
* @param {string} methodName method 名称。
|
||
* @param {Object} body 请求体。
|
||
* @param {Object} query 请求 query。
|
||
* @param {Object} warehouseInfo 仓库上下文信息。
|
||
* @return {Promise<Object>} method 执行结果。
|
||
*/
|
||
async dispatchWarehouseMethod(methodName, body, query, warehouseInfo) {
|
||
const { servicePath, actionName } = this.resolveWarehouseMethod(methodName);
|
||
const methodService = this.getWarehouseMethodService(servicePath);
|
||
|
||
if (!methodService || typeof methodService[actionName] !== 'function') {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 400,
|
||
code: 400,
|
||
message: `warehouse method 不存在: ${methodName}`,
|
||
data: null,
|
||
};
|
||
}
|
||
|
||
// 统一注入仓库上下文到方法服务实例
|
||
if (warehouseInfo && typeof methodService.setInfo === 'function') {
|
||
methodService.setInfo(
|
||
null, // storeId
|
||
warehouseInfo.warehouseId,
|
||
warehouseInfo.appKey,
|
||
null, // customerId
|
||
null // warehouseCode
|
||
);
|
||
}
|
||
|
||
const methodResult = await methodService[actionName]({
|
||
body,
|
||
query,
|
||
});
|
||
|
||
return {
|
||
ok: true,
|
||
httpStatus: 200,
|
||
code: 1,
|
||
message: 'ok',
|
||
data: methodResult,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 校验 warehouse 基础 query 参数。
|
||
* @param {Object} query 请求 query 参数。
|
||
* @return {boolean} 是否通过基础参数校验。
|
||
*/
|
||
validateWarehouseParams(query) {
|
||
const requiredFields = [ 'method', 'v', 'timestamp' ];
|
||
for (const field of requiredFields) {
|
||
if (!query || !query[field]) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
if (typeof query.method !== 'string' || !query.method.includes('.')) {
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 读取并解析 JSON 请求体。
|
||
* @return {Promise<Object>} 解析后的 JSON 对象。
|
||
*/
|
||
async parseJsonBody() {
|
||
const { ctx } = this;
|
||
let jsonData = '';
|
||
|
||
if (ctx.request.rawBody) {
|
||
jsonData = ctx.request.rawBody;
|
||
} else if (ctx.request.body !== undefined) {
|
||
jsonData = typeof ctx.request.body === 'string'
|
||
? ctx.request.body
|
||
: JSON.stringify(ctx.request.body);
|
||
} else {
|
||
jsonData = await new Promise((resolve, reject) => {
|
||
let data = '';
|
||
ctx.req.on('data', chunk => {
|
||
data += chunk;
|
||
});
|
||
ctx.req.on('end', () => {
|
||
resolve(data);
|
||
});
|
||
ctx.req.on('error', reject);
|
||
});
|
||
}
|
||
|
||
jsonData = jsonData ? String(jsonData) : '';
|
||
if (!jsonData || jsonData.trim() === '') {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 400,
|
||
code: 400,
|
||
message: 'json数据为空',
|
||
data: null,
|
||
};
|
||
}
|
||
|
||
try {
|
||
return JSON.parse(jsonData);
|
||
} catch (error) {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 400,
|
||
code: 400,
|
||
message: 'json数据解析错误',
|
||
data: null,
|
||
};
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 提取并归一化 warehouse 所需的 appKey 与 sign。
|
||
* @param {Object} query 请求 query 参数。
|
||
* @return {{ appKey: string, providedSign: string }} 归一化后的关键信息。
|
||
*/
|
||
normalizeSignFields(query = {}) {
|
||
const appKey = String(query.app_key || query.appkey || query.self_app_key || '').trim();
|
||
const providedSign = String(query.sign || query.signature || '').trim();
|
||
|
||
return {
|
||
appKey,
|
||
providedSign,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 处理 warehouse 请求并返回统一结果。
|
||
* 核心差异:使用 interface_type_id=3(仓库级),无需合约关联,直接取 warehouse_id。
|
||
* @return {Promise<Object>} 统一结果对象。
|
||
*/
|
||
async processRequest() {
|
||
const { ctx } = this;
|
||
const query = ctx.query || {};
|
||
|
||
if (!this.validateWarehouseParams(query)) {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 400,
|
||
code: 400,
|
||
message: '参数缺失或错误: method/v/timestamp',
|
||
data: null,
|
||
};
|
||
}
|
||
|
||
// 时间戳时效校验(±10分钟)
|
||
const nowTs = Math.floor(Date.now() / 1000);
|
||
const reqTs = Number(query.timestamp);
|
||
if (!Number.isFinite(reqTs) || Math.abs(nowTs - reqTs) > 600) {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 400,
|
||
code: 400,
|
||
message: `时间戳已过期或无效,服务器时间: ${nowTs}`,
|
||
data: null,
|
||
};
|
||
}
|
||
|
||
const body = await this.parseJsonBody();
|
||
if (body && body.ok === false) {
|
||
return body;
|
||
}
|
||
|
||
const { appKey, providedSign } = this.normalizeSignFields(query);
|
||
if (!appKey) {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 400,
|
||
code: 400,
|
||
message: '参数缺失或错误: app_key',
|
||
data: null,
|
||
};
|
||
}
|
||
|
||
const { systems: systemsService } = ctx.service;
|
||
const apiSignService = systemsService.apiSign;
|
||
|
||
// 仓库级验签:interface_type_id = 3
|
||
const verifyResult = await apiSignService.verifyRequestSignByAppKey({
|
||
appKey,
|
||
query,
|
||
body,
|
||
providedSign,
|
||
interfaceTypeId: WAREHOUSE_INTERFACE_TYPE_ID,
|
||
});
|
||
|
||
if (!verifyResult.ok) {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 401,
|
||
code: 401,
|
||
message: `签名校验失败: ${verifyResult.message}`,
|
||
data: null,
|
||
};
|
||
}
|
||
|
||
// 仓库级无需合约关联,直接使用 api_app.warehouse_id
|
||
const warehouseId = verifyResult.warehouseId;
|
||
if (!warehouseId) {
|
||
return {
|
||
ok: false,
|
||
httpStatus: 403,
|
||
code: 403,
|
||
message: 'API 应用未绑定仓库',
|
||
data: null,
|
||
};
|
||
}
|
||
|
||
const queryWithAppKey = {
|
||
...query,
|
||
app_key: verifyResult.appKey,
|
||
};
|
||
|
||
return await this.dispatchWarehouseMethod(query.method, body, queryWithAppKey, {
|
||
warehouseId,
|
||
appKey: verifyResult.appKey,
|
||
});
|
||
}
|
||
}
|
||
|
||
module.exports = WarehouseService;
|