kd100.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const crypto = require('crypto');
  7. class Kd100Service extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'kd100');
  10. this.httpUtil = this.ctx.service.util.httpUtil;
  11. this.kd100Api = 'https://poll.kuaidi100.com/poll/query.do';
  12. }
  13. async search({ no, type }) {
  14. const customer = '5EC65D1966B410C333013563B7156AEE';
  15. const key = 'jrwohIUD2299';
  16. const param = { com: type, num: no, resultv2: '4' };
  17. const md5Str = `${JSON.stringify(param)}${key}${customer}`;
  18. const sign = _.toUpper(await this.getSign(md5Str));
  19. const body = { customer, sign, param: JSON.stringify(param) };
  20. const res = await this.ctx.curl(this.kd100Api, {
  21. method: 'POST',
  22. data: body,
  23. dataType: 'json',
  24. contentType: 'application/x-www-form-urlencoded',
  25. });
  26. if (res.status === 200) {
  27. const { ischeck, data, nu, status, returnCode, message } = res.data;
  28. if (!returnCode) {
  29. const list = this.resetData(data);
  30. const is_check = this.isCheck(ischeck);
  31. console.log(is_check);
  32. return { list, is_check, no: nu, status };
  33. } else if (returnCode !== '200') throw new BusinessError(ErrorCode.SERVICE_FAUL, message);
  34. }
  35. }
  36. isCheck(value) {
  37. return value === '0' ? '未签收' : '已签收';
  38. }
  39. resetData(list) {
  40. const newList = list.map(i => {
  41. const { ftime, context, statusCode } = i;
  42. const obj = { time: ftime, context, statsu: this.getStatus(statusCode) };
  43. return obj;
  44. });
  45. return newList;
  46. }
  47. getStatus(code) {
  48. const arr = [
  49. { code: '1', text: '快递揽件' },
  50. { code: '101', text: '已经下快件单' },
  51. { code: '102', text: '待快递公司揽收' },
  52. { code: '103', text: '快递公司已经揽收' },
  53. { code: '0', text: '快件在途中' },
  54. { code: '1001', text: '快件到达收件人城市' },
  55. { code: '1002', text: '快件处于运输过程中' },
  56. { code: '1003', text: '快件发往到新的收件地址' },
  57. { code: '5', text: '快件正在派件' },
  58. { code: '501', text: '快件已经投递到快递柜或者快递驿站' },
  59. { code: '3', text: '快件已签收' },
  60. { code: '301', text: '收件人正常签收' },
  61. { code: '302', text: '快件显示派件异常,但后续正常签收' },
  62. { code: '303', text: '快件已被代签' },
  63. { code: '304', text: '快件已从快递柜或者驿站取出签收' },
  64. { code: '6', text: '快件正处于返回发货人的途中' },
  65. { code: '4', text: '此快件单已退签' },
  66. { code: '401', text: '此快件单已撤销' },
  67. { code: '14', text: '收件人拒签快件' },
  68. { code: '7', text: '快件转给其他快递公司邮寄' },
  69. { code: '2', text: '快件存在疑难' },
  70. { code: '201', text: '快件长时间派件后未签收' },
  71. { code: '202', text: '快件长时间没有派件或签收' },
  72. { code: '203', text: '收件人发起拒收快递,待发货方确认' },
  73. { code: '204', text: '快件派件时遇到异常情况' },
  74. { code: '205', text: '快件在快递柜或者驿站长时间未取' },
  75. { code: '206', text: '无法联系到收件人' },
  76. { code: '207', text: '超出快递公司的服务区范围' },
  77. { code: '208', text: '快件滞留在网点,没有派送' },
  78. { code: '209', text: '快件破损' },
  79. { code: '8', text: '超出快件清关递公司的服务区范围' },
  80. { code: '10', text: '快件等待清关' },
  81. { code: '11', text: '快件正在清关流程中' },
  82. { code: '12', text: '快件已完成清关流程' },
  83. { code: '13', text: '货物在清关过程中出现异常' },
  84. { code: '\\', text: '收件人拒签快件' },
  85. ];
  86. return _.get(arr[code], 'text');
  87. }
  88. async getSign(data) {
  89. const md5 = crypto.createHash('md5');
  90. return md5.update(data).digest('hex');
  91. }
  92. }
  93. module.exports = Kd100Service;