forked from tumobi/nideshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
324 additions
and
59 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module.exports = class extends think.Model { | ||
get tableName() { | ||
return this.tablePrefix + 'order_express'; | ||
} | ||
|
||
/** | ||
* 获取最新的订单物流信息 | ||
* @param orderId | ||
* @returns {Promise.<*>} | ||
*/ | ||
async getLatestOrderExpress(orderId) { | ||
const returnExpressInfo = { | ||
shipper_code: '', | ||
shipper_name: '', | ||
logistic_code: '', | ||
is_finish: 0, | ||
request_time: 0, | ||
traces: [] | ||
}; | ||
const orderExpress = await this.where({ order_id: orderId }).find(); | ||
if (think.isEmpty(orderExpress)) { | ||
return returnExpressInfo; | ||
} | ||
if (think.isEmpty(orderExpress.shipper_code) || think.isEmpty(orderExpress.logistic_code)) { | ||
return returnExpressInfo; | ||
} | ||
|
||
returnExpressInfo.shipper_code = orderExpress.shipper_code; | ||
returnExpressInfo.shipper_name = orderExpress.shipper_name; | ||
returnExpressInfo.logistic_code = orderExpress.logistic_code; | ||
returnExpressInfo.is_finish = orderExpress.is_finish; | ||
returnExpressInfo.request_time = think.datetime(orderExpress.request_time * 1000); | ||
returnExpressInfo.traces = think.isEmpty(orderExpress.traces) ? [] : JSON.parse(orderExpress.traces); | ||
|
||
// 如果物流配送已完成,直接返回 | ||
if (orderExpress.is_finish) { | ||
return returnExpressInfo; | ||
} | ||
|
||
// 查询最新物流信息 | ||
const ExpressSerivce = think.service('express', 'api'); | ||
const latestExpressInfo = await ExpressSerivce.queryExpress(orderExpress.shipper_code, orderExpress.logistic_code); | ||
const nowTime = Number.parseInt(Date.now() / 1000); | ||
const updateData = { | ||
request_time: nowTime, | ||
update_time: nowTime, | ||
request_count: ['EXP', 'request_count+1'] | ||
}; | ||
if (latestExpressInfo.success) { | ||
returnExpressInfo.traces = latestExpressInfo.traces; | ||
returnExpressInfo.is_finish = latestExpressInfo.isFinish; | ||
// 查询成功则更新订单物流信息 | ||
updateData.traces = JSON.stringify(latestExpressInfo.traces); | ||
returnExpressInfo.request_time = think.datetime(nowTime * 1000); | ||
updateData.is_finish = latestExpressInfo.isFinish; | ||
} | ||
await this.where({ id: orderExpress.id }).update(updateData); | ||
return returnExpressInfo; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module.exports = class extends think.Model { | ||
/** | ||
* 根据快递公司编码获取名称 | ||
* @param shipperCode | ||
* @returns {Promise.<*>} | ||
*/ | ||
async getShipperNameByCode(shipperCode) { | ||
return this.where({ code: shipperCode }).getField('name', true); | ||
} | ||
|
||
/** | ||
* 根据 id 获取快递公司信息 | ||
* @param shipperId | ||
* @returns {Promise.<*>} | ||
*/ | ||
async getShipperById(shipperId) { | ||
return this.where({ id: shipperId }).find(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const rp = require('request-promise'); | ||
const _ = require('lodash'); | ||
|
||
module.exports = class extends think.Service { | ||
async queryExpress(shipperCode, logisticCode, orderCode = '') { | ||
let expressInfo = { | ||
success: false, | ||
shipperCode: shipperCode, | ||
shipperName: '', | ||
logisticCode: logisticCode, | ||
isFinish: 0, | ||
traces: [] | ||
}; | ||
const fromData = this.generateFromData(shipperCode, logisticCode, orderCode); | ||
if (think.isEmpty(fromData)) { | ||
return expressInfo; | ||
} | ||
|
||
const sendOptions = { | ||
method: 'POST', | ||
url: think.config('express.request_url'), | ||
headers: { | ||
'content-type': 'application/x-www-form-urlencoded' | ||
}, | ||
form: fromData | ||
}; | ||
|
||
try { | ||
const requestResult = await rp(sendOptions); | ||
if (think.isEmpty(requestResult)) { | ||
return expressInfo; | ||
} | ||
expressInfo = this.parseExpressResult(requestResult); | ||
expressInfo.shipperCode = shipperCode; | ||
expressInfo.logisticCode = logisticCode; | ||
return expressInfo; | ||
} catch (err) { | ||
return expressInfo; | ||
} | ||
} | ||
|
||
parseExpressResult(requestResult) { | ||
const expressInfo = { | ||
success: false, | ||
shipperCode: '', | ||
shipperName: '', | ||
logisticCode: '', | ||
isFinish: 0, | ||
traces: [] | ||
}; | ||
|
||
if (think.isEmpty(requestResult)) { | ||
return expressInfo; | ||
} | ||
|
||
try { | ||
if (_.isString(requestResult)) { | ||
requestResult = JSON.parse(requestResult); | ||
} | ||
} catch (err) { | ||
return expressInfo; | ||
} | ||
|
||
if (think.isEmpty(requestResult.Success)) { | ||
return expressInfo; | ||
} | ||
|
||
// 判断是否已签收 | ||
if (Number.parseInt(requestResult.State) === 3) { | ||
expressInfo.isFinish = 1; | ||
} | ||
expressInfo.success = true; | ||
if (!think.isEmpty(requestResult.Traces) && Array.isArray(requestResult.Traces)) { | ||
expressInfo.traces = _.map(requestResult.Traces, item => { | ||
return { datetime: item.AcceptTime, content: item.AcceptStation }; | ||
}); | ||
_.reverse(expressInfo.traces); | ||
} | ||
return expressInfo; | ||
} | ||
|
||
generateFromData(shipperCode, logisticCode, orderCode) { | ||
const requestData = this.generateRequestData(shipperCode, logisticCode, orderCode); | ||
const fromData = { | ||
RequestData: encodeURI(requestData), | ||
EBusinessID: think.config('express.appid'), | ||
RequestType: '1002', | ||
DataSign: this.generateDataSign(requestData), | ||
DataType: '2' | ||
}; | ||
return fromData; | ||
} | ||
|
||
generateRequestData(shipperCode, logisticCode, orderCode = '') { | ||
// 参数验证 | ||
const requestData = { | ||
OrderCode: orderCode, | ||
ShipperCode: shipperCode, | ||
LogisticCode: logisticCode | ||
}; | ||
return JSON.stringify(requestData); | ||
} | ||
|
||
generateDataSign(requestData) { | ||
return encodeURI(Buffer.from(think.md5(requestData + think.config('express.appkey'))).toString('base64')); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters