WMS_MAIN/app/service/systems/polo_api.js

200 lines
7.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const Service = require('egg').Service;
const poloApiConfig = require('../../../config/poloapi');
const crypto = require('crypto');
class PoloApiService extends Service {
constructor(ctx) {
super(ctx);
this.configPoloApi = poloApiConfig;
}
// 批量组装订单JSON
getJsonDatas(expressConfig, list) {
const cal = new Date();
const year = cal.getFullYear();
const month = cal.getMonth() + 1;
const day = cal.getDate() + 3;
const day1 = cal.getDate() + 4;
const sb1 = this._formatPickupDate(year, month, day);
const sb2 = this._formatPickupDate(year, month, day1);
const orders = [];
for (const wmsOrder of list) {
let txLogisticID = wmsOrder.sourceId;
txLogisticID = txLogisticID.length < 6 ? txLogisticID.padStart(6, '0') : txLogisticID;
if (wmsOrder.postcode === 'p10') {
if (txLogisticID.length > 19) txLogisticID = txLogisticID.slice(-19);
} else {
if (txLogisticID.length > 28) txLogisticID = txLogisticID.slice(-28);
}
txLogisticID = txLogisticID.replace(/_/g, '');
// 快递公司代码
const cpcode = this.configPoloApi.postCodeMap[wmsOrder.postcode] || '';
// 商品名
let cnname = '';
const ycflag = !(wmsOrder.wareHouseId === 5400 || wmsOrder.storeId === 58303);
if (ycflag) {
const items = wmsOrder.items || '';
const splitStr = items.split('|');
if (splitStr.length > 0) {
const s = splitStr[0];
const sArr = s.split(',');
if (sArr.length > 2) cnname = sArr[2];
}
} else {
cnname = 'sp';
}
// 收件人手机号
const mobile = wmsOrder.tempMobile || wmsOrder.tel || '';
/*
order对象结构(41-77):
{
businessplat, ismultiplepieces, businesstype, cpcode, customercode, customername,
goods: [goods对象结构(49-56)],
logisticsproductname, logistictype, mainsubpaymode, needencrypt, numpackage,
orderno, ordersource, ordertype, plattradeno, ProviderID, providercode,
receiver: receiver对象结构(59-65), sender: sender对象结构(66-75), shipperno, sitecode, Inputsite, transtype
}
goods对象结构(49-56):
{
cnname, count, unit, currencytype, Unit, weight, price
}
receiver对象结构(59-65):
{
address, name, area, city, mobile, province
}
sender对象结构(66-75):
{
country, Company, address, name, area, town, city, phone, mobile, tel, province
}
*/
// 组装订单对象(下标范围:约第41-77行)
const order = { // [order对象开始 41]
businessplat: 'OTHERS', // 41
ismultiplepieces: '0',
businesstype: 'JH_Normal',
cpcode,
customercode: '',
customername: expressConfig.storeProperty || '测试',
goods: [{ // [goods对象开始 49]
cnname,
count: String(wmsOrder.goodsCount),
unit: '属性',
currencytype: 'CNY',
Unit: '',
weight: String(wmsOrder.orderWeight),
price: '0.0',
}], // [goods对象结束 56]
logisticsproductname: 'JH_02',
logistictype: cpcode,
mainsubpaymode: '1',
needencrypt: '1',
numpackage: '1',
orderno: wmsOrder.sourceOrderCode || (wmsOrder.id && wmsOrder.id.length > 13 ? wmsOrder.id.substring(0, 14) : wmsOrder.id),
ordersource: 'KUAI_SHOU',
ordertype: 'JH_Normal',
plattradeno: wmsOrder.sourceOrderCode || txLogisticID,
ProviderID: wmsOrder.postcode === 'p03' ? 1327 : (wmsOrder.postcode === 'p04' ? 1499 : ''),
providercode: cpcode,
receiver: { // [receiver对象开始 59]
address: wmsOrder.address,
name: wmsOrder.name,
area: wmsOrder.expAreaName,
city: wmsOrder.cityName,
mobile,
province: wmsOrder.province,
}, // [receiver对象结束 65]
sender: { // [sender对象开始 66]
country: 'CN',
Company: expressConfig.storeName,
address: expressConfig.address,
name: expressConfig.senderName,
area: expressConfig.districtName && expressConfig.districtName.indexOf(';') === -1 ? expressConfig.districtName : expressConfig.districtName.split(';')[0],
town: expressConfig.districtName && expressConfig.districtName.indexOf(';') !== -1 ? expressConfig.districtName.split(';')[1] : undefined,
city: expressConfig.cityName,
phone: expressConfig.senderTel,
mobile: expressConfig.senderTel,
tel: expressConfig.senderTel,
province: expressConfig.provinceName,
}, // [sender对象结束 75]
shipperno: [ 'p22', 'p35', 'p10' ].includes(wmsOrder.postcode) ? expressConfig.siteCode : expressConfig.clientId,
sitecode: expressConfig.siteCode || '',
Inputsite: expressConfig.siteCode || '',
transtype: '0',
// 省略部分特殊字段和extraInfo,后续可补充
}; // [order对象结束 77]
// 特殊字段
if (wmsOrder.postcode === 'p10') {
order.pickUpDate = sb1;
order.pickUpEndDate = sb2;
}
orders.push(order);
}
const body = JSON.stringify({ orders, shoptype: 'JH_001' });
// 组装url参数
const urlParams = {
method: 'Differ.JH.Logistics.PostOrder',
appkey: this.configPoloApi.appKey,
token: expressConfig.sessionKey,
platid: this.configPoloApi.platid,
version: this.configPoloApi.version,
bizcontent: encodeURIComponent(body),
contenttype: this.configPoloApi.contenttype,
timestamp: encodeURIComponent(this._formatDate(new Date())),
};
// 签名
const signMap = { ...urlParams, bizcontent: body, timestamp: this._formatDate(new Date()) };
const signText = this.getSignText(signMap, this.configPoloApi.appSecret);
urlParams.sign = this.md5Encrypt(signText.toLowerCase(), 'utf8');
// 返回请求体和参数
return { body, urlParams };
}
// 单个订单JSON(略,结构同上,建议复用 getJsonDatas 逻辑)
getJsonData(expressConfig, wmsOrder) {
// 可根据 type 判断调用 getJsonDatas 或单独组装
return this.getJsonDatas(expressConfig, [ wmsOrder ]);
}
// 生成签名字符串
getSignText(signMap, secret) {
let sb = '' + secret;
Object.keys(signMap).sort().forEach(key => {
sb += key + signMap[key];
});
sb += secret;
return sb;
}
// MD5加密
md5Encrypt(text, encoding = 'utf8') {
return crypto.createHash('md5').update(text, encoding).digest('hex');
}
// 工具:格式化日期
_formatDate(date) {
// 仅支持 yyyy-MM-dd HH:mm:ss
const pad = n => (n < 10 ? '0' + n : n);
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' '
+ pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds());
}
// 工具:组装取件日期
_formatPickupDate(year, month, day) {
const pad = n => (n < 10 ? '0' + n : n);
return `${year}-${pad(month)}-${pad(day)} 18:00:00`;
}
}
module.exports = PoloApiService;