lrf 2 年之前
父节点
当前提交
35f3ae6250
共有 4 个文件被更改,包括 114 次插入0 次删除
  1. 4 0
      app/controller/config/.zrOrder.js
  2. 99 0
      app/service/util/kd100.js
  3. 10 0
      app/service/zrOrder.js
  4. 1 0
      app/z_router/zrOrder.js

+ 4 - 0
app/controller/config/.zrOrder.js

@@ -45,4 +45,8 @@ module.exports = {
   toMakeOrder: {
     requestBody: ['!key'],
   },
+  sot: {
+    params: ['!id'],
+    service: 'searchOrderTransport',
+  },
 };

+ 99 - 0
app/service/util/kd100.js

@@ -0,0 +1,99 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+const crypto = require('crypto');
+
+class Kd100Service extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'kd100');
+    this.httpUtil = this.ctx.service.util.httpUtil;
+    this.kd100Api = 'https://poll.kuaidi100.com/poll/query.do';
+  }
+
+  async search({ no, type }) {
+    const customer = '5EC65D1966B410C333013563B7156AEE';
+    const key = 'jrwohIUD2299';
+    const param = { com: type, num: no, resultv2: '4' };
+    const md5Str = `${JSON.stringify(param)}${key}${customer}`;
+    const sign = _.toUpper(await this.getSign(md5Str));
+    const body = { customer, sign, param: JSON.stringify(param) };
+    const res = await this.ctx.curl(this.kd100Api, {
+      method: 'POST',
+      data: body,
+      dataType: 'json',
+      contentType: 'application/x-www-form-urlencoded',
+    });
+    if (res.status === 200) {
+      const { ischeck, data, nu, status, returnCode, message } = res.data;
+      if (!returnCode) {
+        const list = this.resetData(data);
+        const is_check = this.isCheck(ischeck);
+        console.log(is_check);
+        return { list, is_check, no: nu, status };
+      } else if (returnCode !== '200') throw new BusinessError(ErrorCode.SERVICE_FAUL, message);
+    }
+  }
+
+  isCheck(value) {
+    return value === '0' ? '未签收' : '已签收';
+  }
+
+  resetData(list) {
+    const newList = list.map(i => {
+      const { ftime, context, statusCode } = i;
+      const obj = { time: ftime, context, statsu: this.getStatus(statusCode) };
+      return obj;
+    });
+    return newList;
+  }
+  getStatus(code) {
+    const arr = [
+      { code: '1', text: '快递揽件' },
+      { code: '101', text: '已经下快件单' },
+      { code: '102', text: '待快递公司揽收' },
+      { code: '103', text: '快递公司已经揽收' },
+      { code: '0', text: '快件在途中' },
+      { code: '1001', text: '快件到达收件人城市' },
+      { code: '1002', text: '快件处于运输过程中' },
+      { code: '1003', text: '快件发往到新的收件地址' },
+      { code: '5', text: '快件正在派件' },
+      { code: '501', text: '快件已经投递到快递柜或者快递驿站' },
+      { code: '3', text: '快件已签收' },
+      { code: '301', text: '收件人正常签收' },
+      { code: '302', text: '快件显示派件异常,但后续正常签收' },
+      { code: '303', text: '快件已被代签' },
+      { code: '304', text: '快件已从快递柜或者驿站取出签收' },
+      { code: '6', text: '快件正处于返回发货人的途中' },
+      { code: '4', text: '此快件单已退签' },
+      { code: '401', text: '此快件单已撤销' },
+      { code: '14', text: '收件人拒签快件' },
+      { code: '7', text: '快件转给其他快递公司邮寄' },
+      { code: '2', text: '快件存在疑难' },
+      { code: '201', text: '快件长时间派件后未签收' },
+      { code: '202', text: '快件长时间没有派件或签收' },
+      { code: '203', text: '收件人发起拒收快递,待发货方确认' },
+      { code: '204', text: '快件派件时遇到异常情况' },
+      { code: '205', text: '快件在快递柜或者驿站长时间未取' },
+      { code: '206', text: '无法联系到收件人' },
+      { code: '207', text: '超出快递公司的服务区范围' },
+      { code: '208', text: '快件滞留在网点,没有派送' },
+      { code: '209', text: '快件破损' },
+      { code: '8', text: '超出快件清关递公司的服务区范围' },
+      { code: '10', text: '快件等待清关' },
+      { code: '11', text: '快件正在清关流程中' },
+      { code: '12', text: '快件已完成清关流程' },
+      { code: '13', text: '货物在清关过程中出现异常' },
+      { code: '\\', text: '收件人拒签快件' },
+    ];
+    return _.get(arr[code], 'text');
+  }
+
+  async getSign(data) {
+    const md5 = crypto.createHash('md5');
+    return md5.update(data).digest('hex');
+  }
+}
+
+module.exports = Kd100Service;

+ 10 - 0
app/service/zrOrder.js

@@ -24,6 +24,16 @@ class ZrOrderService extends CrudService {
   // 下单,走同样的函数判断.
   // 直接生成订单,扣除积分
 
+  async searchOrderTransport({ id }) {
+    const order = await this.model.findById(id);
+    if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
+    const { transport = {} } = order;
+    const { shop_transport_no: no, shop_transport_type: type } = transport;
+    if (!no || !type) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '缺少快递信息');
+    const res = await this.ctx.service.util.kd100.search({ no, type });
+    return res;
+  }
+
   async fetch(filter, { sort, desc, projection } = {}) {
     assert(filter);
     filter = await this.beforeFetch(filter);

+ 1 - 0
app/z_router/zrOrder.js

@@ -7,6 +7,7 @@ const rkey = 'zrOrder';
 const ckey = 'zrOrder';
 const keyZh = '尊荣订单';
 const routes = [
+  { method: 'get', path: `${rkey}/sot/:id`, controller: `${ckey}.sot`, name: `${ckey}sot`, zh: `${keyZh}查询物流` },
   { method: 'post', path: `${rkey}/toMakeOrder`, controller: `${ckey}.toMakeOrder`, name: `${ckey}toMakeOrder`, zh: `进入订单页面-${keyZh}` },
   { method: 'post', path: `${rkey}/checkCanBuy`, controller: `${ckey}.checkCanBuy`, name: `${ckey}checkCanBuy`, zh: `检查是否可以购买-${keyZh}` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },