'use strict'; const crypto = require('crypto'); const http = require('http'); const https = require('https'); class QimenClient { constructor(app) { this.app = app; } resolveRuntime(runtime = {}) { const ctx = runtime.ctx || {}; const qimenService = ctx.service && ctx.service.qimen ? ctx.service.qimen : null; const signService = ctx.service && ctx.service.systems && ctx.service.systems.sign ? ctx.service.systems.sign : null; return { ctx, toXml: runtime.toXml || (signService && typeof signService.toXml === 'function' ? signService.toXml.bind(signService) : qimenService && typeof qimenService.formatResponse === 'function' ? qimenService.formatResponse.bind(qimenService) : null), parseXmlToArray: runtime.parseXmlToArray || (qimenService && typeof qimenService.parseXmlToArray === 'function' ? qimenService.parseXmlToArray.bind(qimenService) : null), saveQimenLog: runtime.saveQimenLog || (qimenService && typeof qimenService.saveQimenLog === 'function' ? qimenService.saveQimenLog.bind(qimenService) : async () => null), signTopRequest: runtime.signTopRequest || (signService && typeof signService.signTopRequest === 'function' ? signService.signTopRequest.bind(signService) : null), formatDate: runtime.formatDate || ctx.app?.formatDate || this.app.formatDate || (date => (date && typeof date.toISOString === 'function' ? date.toISOString() : String(date))), }; } normalizeRequestPayload(data) { if (!data || typeof data !== 'object' || Array.isArray(data)) { return { request: data }; } if (Object.prototype.hasOwnProperty.call(data, 'request')) { return data; } return { request: data }; } /** * 生成出站奇门请求的唯一日志 ID。 * 不复用入站请求的 ctx.qimenLogId,避免与入站文件队列落库产生唯一索引冲突。 * @return {string} 唯一日志 ID。 */ generateLogId() { return `QL-${Date.now()}-${crypto.randomBytes(3).toString('hex')}`; } replaceXmlTagsWithString(xmlString, oldTag, newTag) { const startTagRegex = new RegExp(`<\\s*${oldTag}(\\s+[^>]*)?>`, 'gi'); const endTagRegex = new RegExp(`<\\s*/\\s*${oldTag}\\s*>`, 'gi'); const selfClosingRegex = new RegExp(`<\\s*${oldTag}(\\s+[^>]*)?\\s*/>`, 'gi'); return xmlString .replace(startTagRegex, (match, attributes = '') => `<${newTag}${attributes}>`) .replace(endTagRegex, ``) .replace(selfClosingRegex, (match, attributes = '') => `<${newTag}${attributes}/>`); } replaceDeliveryConfirmOrderLineTags(xmlString) { const orderLinesBlockRegex = /<\s*orderLines(\s+[^>]*)?>[\s\S]*?<\s*\/\s*orderLines\s*>/gi; return String(xmlString || '').replace(orderLinesBlockRegex, block => { return this.replaceXmlTagsWithString(block, 'item', 'orderLine'); }); } replaceDeliveryConfirmPackageTags(xmlString) { const packagesBlockRegex = /<\s*packages(\s+[^>]*)?>[\s\S]*?<\s*\/\s*packages\s*>/gi; return String(xmlString || '').replace(packagesBlockRegex, block => { return block // 去掉 buildXmlNode 为数组元素添加的 包装器: → .replace(/<\s*package\s*>\s*<\s*item\s*>/gi, '') // 去掉对应的 包装器闭合,输出标准格式 XML(不再做交错标签变换) .replace(/<\s*\/\s*item\s*>\s*<\s*\/\s*package\s*>/gi, ''); }); } replaceDeliveryConfirmPayloadTags(xmlString) { const withPackages = this.replaceDeliveryConfirmPackageTags(xmlString); return this.replaceDeliveryConfirmOrderLineTags(withPackages); } addSign(baseQueryParams, sign) { return Object.assign({}, baseQueryParams, { sign }); } /** * 构建奇门请求查询参数。 * @param {string} method 接口方法名 * @param {string} appKey 应用 Key * @param {string} version 接口版本 * @param {?string} customerId 货主标识 * @param {Function} formatDate 时间格式化函数 * @return {Object} 查询参数对象 */ getQueryParams(method, appKey, version, customerId, formatDate) { const queryParams = { method, format: 'xml', app_key: appKey, v: version, sign_method: 'md5', timestamp: formatDate(new Date()), }; if (customerId != null) { queryParams.customerId = customerId; } return queryParams; } generateUrlWithParams(baseUrl, params) { if (!params || Object.keys(params).length === 0) { return baseUrl; } const queryString = Object.keys(params) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) .join('&'); const separator = baseUrl.includes('?') ? '&' : '?'; return `${baseUrl}${separator}${queryString}`; } /** * 发送奇门请求并记录请求日志。 * @param {string} method 接口方法名 * @param {?string} customerId 货主标识 * @param {Object} data 业务请求数据 * @param {Object} runtime 运行时依赖 * @return {Promise<{queryId: *, xmlArray: *}>} 查询结果 */ async send(method, customerId, data, runtime = {}) { const helpers = this.resolveRuntime(runtime); if (!helpers.toXml) { throw new Error('缺少奇门 XML 转换器'); } if (!helpers.parseXmlToArray) { throw new Error('缺少奇门响应解析器'); } if (!helpers.signTopRequest) { throw new Error('缺少奇门签名器'); } const wmsAppKey = this.app.config.qimen.appKey; const baseQueryParams = this.getQueryParams(method, wmsAppKey, '2.0', customerId, helpers.formatDate); const baseUrl = 'http://qimen.api.taobao.com/router/qimen/service'; const requestPayload = this.normalizeRequestPayload(data); const xmlData = await helpers.toXml(requestPayload); const queryData = method === 'taobao.qimen.deliveryorder.confirm' || method === 'taobao.qimen.stockout.confirm' ? this.replaceDeliveryConfirmPayloadTags(xmlData) : this.replaceXmlTagsWithString(xmlData, 'item', 'orderLine'); const sign = await helpers.signTopRequest(baseQueryParams, queryData, this.app.config.qimen.security, 'md5'); const queryParams = this.addSign(baseQueryParams, sign); const url = this.generateUrlWithParams(baseUrl, queryParams); const logData = { from_key: this.app.config.qimen.appKey, to_key: wmsAppKey, method, sign, v: '2.0', url, created_at: new Date(), body: '', result: '', use_time: 0, }; if (customerId != null) { logData.costomer_id = customerId; } console.log('发送奇门请求,URL:', url, '请求数据:', queryData); const result = await this.sendRequest(url, queryData, logData, helpers); console.log('奇门请求响应:', result); const xmlArray = await helpers.parseXmlToArray(result.res); return { queryId: result.queryId, xmlArray }; } /** * 发送出站奇门 HTTP 请求,并把出站请求日志直接落库。 * 出站日志使用独立生成的 log_id,不复用入站请求的 ctx.qimenLogId, * 避免与入站日志的文件队列落库路径产生 log_id 唯一索引冲突。 * @param {string} url 请求地址。 * @param {Object|string} requestData 请求数据(对象或 XML 字符串)。 * @param {Object} [logData={}] 日志附加字段。 * @param {Object} [runtime={}] 运行时依赖。 * @return {Promise<{queryId: number|undefined, res: string}>} 查询 ID 与响应内容。 */ async sendRequest(url, requestData, logData = {}, runtime = {}) { const helpers = this.resolveRuntime(runtime); const startTime = Date.now(); // 出站请求不复用入站 ctx.qimenLogId:入站日志统一由文件队列异步落库, // log_id 唯一索引下共用同一 ID 会触发 Duplicate entry 冲突。 const requestLogData = Object.assign({ body: requestData, result: '', log_id: this.generateLogId(), }, logData); requestLogData.body = requestLogData.body || requestData; requestLogData.result = requestLogData.result || ''; requestLogData.log_id = requestLogData.log_id || this.generateLogId(); const httpquery = url.indexOf('https://') > -1 ? https : http; let requestXml; try { if (typeof requestData === 'string') { requestXml = requestData; if (requestXml.trim() === '') { throw new Error('请求数据为空字符串'); } } else { requestXml = await helpers.toXml(requestData); } if (!requestXml || requestXml.trim() === '') { throw new Error('XML内容为空'); } } catch (error) { requestLogData.result = error.stack || `${error.name || 'Error'}: ${error.message || String(error)}`; await helpers.saveQimenLog(requestLogData, startTime); throw error; } const header = { 'Content-Type': 'application/xml', 'Content-Length': Buffer.byteLength(requestXml, 'utf8'), 'User-Agent': 'EggJS Client', }; const options = { method: 'POST', headers: header, }; let res; try { res = await new Promise((resolve, reject) => { let req; try { req = httpquery.request(url, options, response => { let datas = ''; response.on('data', chunk => { datas += chunk; }); response.on('end', () => { resolve(datas); }); }); } catch (error) { reject(error); return; } req.on('error', error => { reject(error); }); req.write(requestXml); req.end(); }); } catch (error) { requestLogData.result = error.stack || `${error.name || 'Error'}: ${error.message || String(error)}`; await helpers.saveQimenLog(requestLogData, startTime); throw error; } requestLogData.result = res; const queryId = await helpers.saveQimenLog(requestLogData, startTime); return { queryId, res }; } } module.exports = QimenClient;