'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;