kd100.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 };
  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. console.log(body);
  21. const res = await this.ctx.curl(this.kd100Api, {
  22. method: 'POST',
  23. data: body,
  24. dataType: 'json',
  25. contentType: 'application/x-www-form-urlencoded',
  26. });
  27. if (res.status === 200) return res.data;
  28. }
  29. async getSign(data) {
  30. const md5 = crypto.createHash('md5');
  31. return md5.update(data).digest('hex');
  32. }
  33. }
  34. module.exports = Kd100Service;