小程序对接快递鸟接口也是先在快递鸟的官网下载一个 nodejs的示例代码。因为我是小程序新手。所以花的时候有点多了。小程序的加密和解密需要用到两个js类。我先传上来。
md5.js
function array(n) {
for (var i = 0; i < n; i++) this[i] = 0;
this.length = n;
}
/* Quelques fonctions fondamentales doivent ¨ºtre transform¨¦es ¨¤ cause
* d'erreurs Javascript.
* Essayez par exemple de calculer 0xffffffff >> 4 ...
* Les fonctions utilis¨¦es maintenant sont il est vrai plus lentes que les
* fonctions originales mais elles fonctionnent.
*/
function integer(n) { return n % (0xffffffff + 1); }
function shr(a, b) {
a = integer(a);
b = integer(b);
if (a - 0x80000000 >= 0) {
a = a % 0x80000000;
a >>= b;
a += 0x40000000 >> (b - 1);
} else
a >>= b;
return a;
}
function shl1(a) {
a = a % 0x80000000;
if (a & 0x40000000 == 0x40000000) {
a -= 0x40000000;
a *= 2;
a += 0x80000000;
} else
a *= 2;
return a;
}
function shl(a, b) {
a = integer(a);
b = integer(b);
for (var i = 0; i < b; i++) a = shl1(a);
return a;
}
function and(a, b) {
a = integer(a);
b = integer(b);
var t1 = (a - 0x80000000);
var t2 = (b - 0x80000000);
if (t1 >= 0)
if (t2 >= 0)
return ((t1 & t2) + 0x80000000);
else
return (t1 & b);
else
if (t2 >= 0)
return (a & t2);
else
return (a & b);
}
function or(a, b) {
a = integer(a);
b = integer(b);
var t1 = (a - 0x80000000);
var t2 = (b - 0x80000000);
if (t1 >= 0)
if (t2 >= 0)
return ((t1 | t2) + 0x80000000);
else
return ((t1 | b) + 0x80000000);
else
if (t2 >= 0)
return ((a | t2) + 0x80000000);
else
return (a | b);
}
function xor(a, b) {
a = integer(a);
b = integer(b);
var t1 = (a - 0x80000000);
var t2 = (b - 0x80000000);
if (t1 >= 0)
if (t2 >= 0)
return (t1 ^ t2);
else
return ((t1 ^ b) + 0x80000000);
else
if (t2 >= 0)
return ((a ^ t2) + 0x80000000);
else
return (a ^ b);
}
function not(a) {
a = integer(a);
return (0xffffffff - a);
}
/* D¨¦but de l'algorithme */
var state = new array(4);
var count = new array(2);
count[0] = 0;
count[1] = 0;
var buffer = new array(64);
var transformBuffer = new array(16);
var digestBits = new array(16);
var S11 = 7;
var S12 = 12;
var S13 = 17;
var S14 = 22;
var S21 = 5;
var S22 = 9;
var S23 = 14;
var S24 = 20;
var S31 = 4;
var S32 = 11;
var S33 = 16;
var S34 = 23;
var S41 = 6;
var S42 = 10;
var S43 = 15;
var S44 = 21;
function F(x, y, z) {
return or(and(x, y), and(not(x), z));
}
function G(x, y, z) {
return or(and(x, z), and(y, not(z)));
}
function H(x, y, z) {
return xor(xor(x, y), z);
}
function I(x, y, z) {
return xor(y, or(x, not(z)));
}
function rotateLeft(a, n) {
return or(shl(a, n), (shr(a, (32 - n))));
}
function FF(a, b, c, d, x, s, ac) {
a = a + F(b, c, d) + x + ac;
a = rotateLeft(a, s);
a = a + b;
return a;
}
function GG(a, b, c, d, x, s, ac) {
a = a + G(b, c, d) + x + ac;
a = rotateLeft(a, s);
a = a + b;
return a;
}
function HH(a, b, c, d, x, s, ac) {
a = a + H(b, c, d) + x + ac;
a = rotateLeft(a, s);
a = a + b;
return a;
}
function II(a, b, c, d, x, s, ac) {
a = a + I(b, c, d) + x + ac;
a = rotateLeft(a, s);
a = a + b;
return a;
}
function transform(buf, offset) {
var a = 0, b = 0, c = 0, d = 0;
var x = transformBuffer;
a = state[0];
b = state[1];
c = state[2];
d = state[3];
for (var i = 0; i < 16; i++) {
x[i] = and(buf[i * 4 + offset], 0xff);
for (var j = 1; j < 4; j++) {
x[i] += shl(and(buf[i * 4 + j + offset], 0xff), j * 8);
}
}
/* tour 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
/* tour 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
/* tour 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
/* tour 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
/* Avec l'initialisation de Dobbertin:
state[0] = 0x12ac2375;
state[1] = 0x3b341042;
state[2] = 0x5f62b97c;
state[3] = 0x4ba763ed;
s'il y a une collision:
begin 644 Message1
M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
39GIK9>TF$W()/MEHR%C4:G1R:Q"=
`
end
begin 644 Message2
M7MH=JO6_>MG!X?!51$)W,CXV!A"=(!AR71,<X`Y-IIT9^Z&8L$2N'Y*Y:R.;
39GIK9>TF$W()/MEHREC4:G1R:Q"=
`
end
*/
function init() {
count[0] = count[1] = 0;
state[0] = 0x67452301;
state[1] = 0xefcdab89;
state[2] = 0x98badcfe;
state[3] = 0x10325476;
for (var i = 0; i < digestBits.length; i++)
digestBits[i] = 0;
}
function update(b) {
var index, i;
index = and(shr(count[0], 3), 0x3f);
if (count[0] < 0xffffffff - 7)
count[0] += 8;
else {
count[1]++;
count[0] -= 0xffffffff + 1;
count[0] += 8;
}
buffer[index] = and(b, 0xff);
if (index >= 63) {
transform(buffer, 0);
}
}
function finish() {
var bits = new array(8);
var padding;
var i = 0, index = 0, padLen = 0;
for (i = 0; i < 4; i++) {
bits[i] = and(shr(count[0], (i * 8)), 0xff);
}
for (i = 0; i < 4; i++) {
bits[i + 4] = and(shr(count[1], (i * 8)), 0xff);
}
index = and(shr(count[0], 3), 0x3f);
padLen = (index < 56) ? (56 - index) : (120 - index);
padding = new array(64);
padding[0] = 0x80;
for (i = 0; i < padLen; i++)
update(padding[i]);
for (i = 0; i < 8; i++)
update(bits[i]);
for (i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
digestBits[i * 4 + j] = and(shr(state[i], (j * 8)), 0xff);
}
}
}
/* Fin de l'algorithme MD5 */
function hexa(n) {
var hexa_i;
var hexa_h = "0123456789abcdef";
var hexa_c = "";
var hexa_m = n;
for (hexa_i = 0; hexa_i < 8; hexa_i++) {
hexa_c = hexa_h.charAt(Math.abs(hexa_m) % 16) + hexa_c;
hexa_m = Math.floor(hexa_m / 16);
}
return hexa_c;
}
var ascii = "01234567890123456789012345678901" +
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
function MD5(message) {
var l, s, k, ka, kb, kc, kd, i;
init();
for (k = 0; k < message.length; k++) {
l = message.charAt(k);
update(ascii.lastIndexOf(l));
}
finish();
ka = kb = kc = kd = 0;
for (i = 0; i < 4; i++) ka += shl(digestBits[15 - i], (i * 8));
for (i = 4; i < 8; i++) kb += shl(digestBits[15 - i], ((i - 4) * 8));
for (i = 8; i < 12; i++) kc += shl(digestBits[15 - i], ((i - 8) * 8));
for (i = 12; i < 16; i++) kd += shl(digestBits[15 - i], ((i - 12) * 8));
s = hexa(kd) + hexa(kc) + hexa(kb) + hexa(ka);
return s;
}
module.exports = {
md5: MD5
}
base64.js
//
// THIS FILE IS AUTOMATICALLY GENERATED! DO NOT EDIT BY HAND!
//
;(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? module.exports = factory()
: typeof define === 'function' && define.amd
? define(factory) :
// cf. https://github.com/dankogai/js-base64/issues/119
(function() {
// existing version for noConflict()
const _Base64 = global.Base64;
const gBase64 = factory();
gBase64.noConflict = () => {
global.Base64 = _Base64;
return gBase64;
};
if (global.Meteor) { // Meteor.js
Base64 = gBase64;
}
global.Base64 = gBase64;
})();
}((typeof self !== 'undefined' ? self
: typeof window !== 'undefined' ? window
: typeof global !== 'undefined' ? global
: this
), function() {
'use strict';
/**
* base64.ts
*
* Licensed under the BSD 3-Clause License.
* http://opensource.org/licenses/BSD-3-Clause
*
* References:
* http://en.wikipedia.org/wiki/Base64
*
* @author Dan Kogai (https://github.com/dankogai)
*/
const version = '3.6.1';
/**
* @deprecated use lowercase `version`.
*/
const VERSION = version;
const _hasatob = typeof atob === 'function';
const _hasbtoa = typeof btoa === 'function';
const _hasBuffer = typeof Buffer === 'function';
const _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined;
const _TE = typeof TextEncoder === 'function' ? new TextEncoder() : undefined;
const b64ch = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const b64chs = [...b64ch];
const b64tab = ((a) => {
let tab = {};
a.forEach((c, i) => tab[c] = i);
return tab;
})(b64chs);
const b64re = /^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/;
const _fromCC = String.fromCharCode.bind(String);
const _U8Afrom = typeof Uint8Array.from === 'function'
? Uint8Array.from.bind(Uint8Array)
: (it, fn = (x) => x) => new Uint8Array(Array.prototype.slice.call(it, 0).map(fn));
const _mkUriSafe = (src) => src
.replace(/[+\/]/g, (m0) => m0 == '+' ? '-' : '_')
.replace(/=+$/m, '');
const _tidyB64 = (s) => s.replace(/[^A-Za-z0-9\+\/]/g, '');
/**
* polyfill version of `btoa`
*/
const btoaPolyfill = (bin) => {
// console.log('polyfilled');
let u32, c0, c1, c2, asc = '';
const pad = bin.length % 3;
for (let i = 0; i < bin.length;) {
if ((c0 = bin.charCodeAt(i++)) > 255 ||
(c1 = bin.charCodeAt(i++)) > 255 ||
(c2 = bin.charCodeAt(i++)) > 255)
throw new TypeError('invalid character found');
u32 = (c0 << 16) | (c1 << 8) | c2;
asc += b64chs[u32 >> 18 & 63]
+ b64chs[u32 >> 12 & 63]
+ b64chs[u32 >> 6 & 63]
+ b64chs[u32 & 63];
}
return pad ? asc.slice(0, pad - 3) + "===".substring(pad) : asc;
};
/**
* does what `window.btoa` of web browsers do.
* @param {String} bin binary string
* @returns {string} Base64-encoded string
*/
const _btoa = _hasbtoa ? (bin) => btoa(bin)
: _hasBuffer ? (bin) => Buffer.from(bin, 'binary').toString('base64')
: btoaPolyfill;
const _fromUint8Array = _hasBuffer
? (u8a) => Buffer.from(u8a).toString('base64')
: (u8a) => {
// cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
const maxargs = 0x1000;
let strs = [];
for (let i = 0, l = u8a.length; i < l; i += maxargs) {
strs.push(_fromCC.apply(null, u8a.subarray(i, i + maxargs)));
}
return _btoa(strs.join(''));
};
/**
* converts a Uint8Array to a Base64 string.
* @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5
* @returns {string} Base64 string
*/
const fromUint8Array = (u8a, urlsafe = false) => urlsafe ? _mkUriSafe(_fromUint8Array(u8a)) : _fromUint8Array(u8a);
// This trick is found broken https://github.com/dankogai/js-base64/issues/130
// const utob = (src: string) => unescape(encodeURIComponent(src));
// reverting good old fationed regexp
const cb_utob = (c) => {
if (c.length < 2) {
var cc = c.charCodeAt(0);
return cc < 0x80 ? c
: cc < 0x800 ? (_fromCC(0xc0 | (cc >>> 6))
+ _fromCC(0x80 | (cc & 0x3f)))
: (_fromCC(0xe0 | ((cc >>> 12) & 0x0f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | (cc & 0x3f)));
}
else {
var cc = 0x10000
+ (c.charCodeAt(0) - 0xD800) * 0x400
+ (c.charCodeAt(1) - 0xDC00);
return (_fromCC(0xf0 | ((cc >>> 18) & 0x07))
+ _fromCC(0x80 | ((cc >>> 12) & 0x3f))
+ _fromCC(0x80 | ((cc >>> 6) & 0x3f))
+ _fromCC(0x80 | (cc & 0x3f)));
}
};
const re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
/**
* @deprecated should have been internal use only.
* @param {string} src UTF-8 string
* @returns {string} UTF-16 string
*/
const utob = (u) => u.replace(re_utob, cb_utob);
//
const _encode = _hasBuffer
? (s) => Buffer.from(s, 'utf8').toString('base64')
: _TE
? (s) => _fromUint8Array(_TE.encode(s))
: (s) => _btoa(utob(s));
/**
* converts a UTF-8-encoded string to a Base64 string.
* @param {boolean} [urlsafe] if `true` make the result URL-safe
* @returns {string} Base64 string
*/
const encode = (src, urlsafe = false) => urlsafe
? _mkUriSafe(_encode(src))
: _encode(src);
/**
* converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5.
* @returns {string} Base64 string
*/
const encodeURI = (src) => encode(src, true);
// This trick is found broken https://github.com/dankogai/js-base64/issues/130
// const btou = (src: string) => decodeURIComponent(escape(src));
// reverting good old fationed regexp
const re_btou = /[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g;
const cb_btou = (cccc) => {
switch (cccc.length) {
case 4:
var cp = ((0x07 & cccc.charCodeAt(0)) << 18)
| ((0x3f & cccc.charCodeAt(1)) << 12)
| ((0x3f & cccc.charCodeAt(2)) << 6)
| (0x3f & cccc.charCodeAt(3)), offset = cp - 0x10000;
return (_fromCC((offset >>> 10) + 0xD800)
+ _fromCC((offset & 0x3FF) + 0xDC00));
case 3:
return _fromCC(((0x0f & cccc.charCodeAt(0)) << 12)
| ((0x3f & cccc.charCodeAt(1)) << 6)
| (0x3f & cccc.charCodeAt(2)));
default:
return _fromCC(((0x1f & cccc.charCodeAt(0)) << 6)
| (0x3f & cccc.charCodeAt(1)));
}
};
/**
* @deprecated should have been internal use only.
* @param {string} src UTF-16 string
* @returns {string} UTF-8 string
*/
const btou = (b) => b.replace(re_btou, cb_btou);
/**
* polyfill version of `atob`
*/
const atobPolyfill = (asc) => {
// console.log('polyfilled');
asc = asc.replace(/\s+/g, '');
if (!b64re.test(asc))
throw new TypeError('malformed base64.');
asc += '=='.slice(2 - (asc.length & 3));
let u24, bin = '', r1, r2;
for (let i = 0; i < asc.length;) {
u24 = b64tab[asc.charAt(i++)] << 18
| b64tab[asc.charAt(i++)] << 12
| (r1 = b64tab[asc.charAt(i++)]) << 6
| (r2 = b64tab[asc.charAt(i++)]);
bin += r1 === 64 ? _fromCC(u24 >> 16 & 255)
: r2 === 64 ? _fromCC(u24 >> 16 & 255, u24 >> 8 & 255)
: _fromCC(u24 >> 16 & 255, u24 >> 8 & 255, u24 & 255);
}
return bin;
};
/**
* does what `window.atob` of web browsers do.
* @param {String} asc Base64-encoded string
* @returns {string} binary string
*/
const _atob = _hasatob ? (asc) => atob(_tidyB64(asc))
: _hasBuffer ? (asc) => Buffer.from(asc, 'base64').toString('binary')
: atobPolyfill;
//
const _toUint8Array = _hasBuffer
? (a) => _U8Afrom(Buffer.from(a, 'base64'))
: (a) => _U8Afrom(_atob(a), c => c.charCodeAt(0));
/**
* converts a Base64 string to a Uint8Array.
*/
const toUint8Array = (a) => _toUint8Array(_unURI(a));
//
const _decode = _hasBuffer
? (a) => Buffer.from(a, 'base64').toString('utf8')
: _TD
? (a) => _TD.decode(_toUint8Array(a))
: (a) => btou(_atob(a));
const _unURI = (a) => _tidyB64(a.replace(/[-_]/g, (m0) => m0 == '-' ? '+' : '/'));
/**
* converts a Base64 string to a UTF-8 string.
* @param {String} src Base64 string. Both normal and URL-safe are supported
* @returns {string} UTF-8 string
*/
const decode = (src) => _decode(_unURI(src));
/**
* check if a value is a valid Base64 string
* @param {String} src a value to check
*/
const isValid = (src) => {
if (typeof src !== 'string')
return false;
const s = src.replace(/\s+/g, '').replace(/=+$/, '');
return !/[^\s0-9a-zA-Z\+/]/.test(s) || !/[^\s0-9a-zA-Z\-_]/.test(s);
};
//
const _noEnum = (v) => {
return {
value: v, enumerable: false, writable: true, configurable: true
};
};
/**
* extend String.prototype with relevant methods
*/
const extendString = function () {
const _add = (name, body) => Object.defineProperty(String.prototype, name, _noEnum(body));
_add('fromBase64', function () { return decode(this); });
_add('toBase64', function (urlsafe) { return encode(this, urlsafe); });
_add('toBase64URI', function () { return encode(this, true); });
_add('toBase64URL', function () { return encode(this, true); });
_add('toUint8Array', function () { return toUint8Array(this); });
};
/**
* extend Uint8Array.prototype with relevant methods
*/
const extendUint8Array = function () {
const _add = (name, body) => Object.defineProperty(Uint8Array.prototype, name, _noEnum(body));
_add('toBase64', function (urlsafe) { return fromUint8Array(this, urlsafe); });
_add('toBase64URI', function () { return fromUint8Array(this, true); });
_add('toBase64URL', function () { return fromUint8Array(this, true); });
};
/**
* extend Builtin prototypes with relevant methods
*/
const extendBuiltins = () => {
extendString();
extendUint8Array();
};
const gBase64 = {
version: version,
VERSION: VERSION,
atob: _atob,
atobPolyfill: atobPolyfill,
btoa: _btoa,
btoaPolyfill: btoaPolyfill,
fromBase64: decode,
toBase64: encode,
encode: encode,
encodeURI: encodeURI,
encodeURL: encodeURI,
utob: utob,
btou: btou,
decode: decode,
isValid: isValid,
fromUint8Array: fromUint8Array,
toUint8Array: toUint8Array,
extendString: extendString,
extendUint8Array: extendUint8Array,
extendBuiltins: extendBuiltins,
};
//
// export Base64 to the namespace
//
// ES5 is yet to have Object.assign() that may make transpilers unhappy.
// gBase64.Base64 = Object.assign({}, gBase64);
gBase64.Base64 = {};
Object.keys(gBase64).forEach(k => gBase64.Base64[k] = gBase64[k]);
return gBase64;
}));
快递鸟对接的时候。我传递的所有数据都是一样的。但是请求一直说我参数格式错误。我刚开始一直以为是md5的问题。所以不停的去更换md5的工具类。后来把java代码里的字符串拷贝过来。用小程序的md5去加密。才发现加密后的数据是一样的。说明md5是没问题的。后来仔细的观察加密前的字符串。才发现了问题的症结。小程序生成的字符串和java生成的字符串有差异。
java加密前的字符串是:{'CustomerName':'','OrderCode':'','ShipperCode':'ANE','LogisticCode':'300471491371'}
小程序加密前的字符串是:{"CustomerName":"","OrderCode":"","ShipperCode":"ANE","LogisticCode":"300471491371"}
所以二者加密后的字符串不一致导致请求后,返回我们参数格式不正确。此时将双引号替换为单引号去请求。数据就可以拿到了。见下方代码:
快递鸟请求代码 将此代码复制到小程序的 utils目录下命名KdApiSearchDemo.js 同上方md5.js和base64.js同级
// 快递鸟接口查询 安能快递
var md5 = require('md5.js')
var base64 = require("base64.js")
/**
* 系统级参数
* RequestData String R 请求内容为JSON格式 详情可参考接口技术文档:https://www.kdniao.com/documents
* EBusinessID String R 用户ID
* RequestType String R 请求接口指令
* DataSign String R 数据内容签名,加密方法为:把(请求内容(未编码)+ApiKey)进行MD5加密--32位小写,然后Base64编码,最后进行URL(utf-8)编码
* DataType String R DataType=2,请求、返回数据类型均为JSON格式
* 应用级参数
* R-必填(Required),O-可选(Optional),C-报文中该参数在一定条件下可选(Conditional)
* OrderCode String(30) O 订单编号
* ShipperCode String(10) R 快递公司编码 详细编码参考《快递鸟接口支持快递公司编码.xlsx》 https://www.kdniao.com/documents
* LogisticCode String(30) R 快递单号
* CustomerName String(50) C ShipperCode为SF时必填,对应寄件人/收件人手机号后四位;ShipperCode为其他快递时,可不填或保留字段,不可传值
* 请求示例
* ANE请求示例:
* {
* "OrderCode": "",
* "ShipperCode": "ANE",
* "LogisticCode": "638650888018",
* }
*
* SF请求示例:
* {
* "OrderCode": "",
* "CustomerName": "1234",
* "ShipperCode": "SF",
* "LogisticCode": "SF00003618100",
* }
*/
//请求url --正式地址
const Url = 'https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx'
//用户ID
const EBusinessID = '';//输入自己 申请到的 ID
//API key
const ApiKey = '';//即API key,输入自己申请的 key值
function checkData(OrderCode, ShipperCode, LogisticCode) {
return new Promise((resolve, reject) => {
var RequestData = {
'CustomerName': "",//订单编号 可不传
'OrderCode': "",//联系人 顺丰需要传递收/发放手机号码后四位// 安能不用传
'ShipperCode': ShipperCode,//快递公司 安能物流 ANE 顺丰 SF
'LogisticCode': LogisticCode//快递单号
};
// 18b3eb639773fd431ead064005a2700b
//MThiM2ViNjM5NzczZmQ0MzFlYWQwNjQwMDVhMjcwMGI=
//需要将双引号"" 替换为 ''
RequestData = JSON.stringify(RequestData).replace(/\"/g, "'")
var msgDigestbefore = RequestData + ApiKey
console.log(msgDigestbefore)
var md5Str = md5.md5(msgDigestbefore)
console.log(md5Str)
var msgDigest = base64.encode(md5Str)
console.log(msgDigest)
wx.request({
url: Url,
method: "post",
data: {
RequestData: encodeURIComponent(RequestData),
EBusinessID: EBusinessID,
RequestType: "1002",
DataSign: encodeURIComponent(msgDigest),
DataType: "2"
},
header: {
"Content-type": "application/x-www-form-urlencoded;charset=UTF-8"
},
success(res) {
console.log("success", res);
resolve(res.data)
},
fail(res) {
console.log(res)
reject(err)
}
})
})
}
//将post方法暴露出去
module.exports = {
checkData: checkData
}
在需要的页面调用即可拿到数据。
var kdn = require('../../utils/KdApiSearchDemo.js')
Page({
/**
* 页面的初始数据
*/
data: {
data: ""
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
//请求快递详情安能
getLogisticsAN(LogisticCode) {
var that=this;
var OrderCode = "";
var ShipperCode = "ANE";
//此处传入运单号,即可返回详情
var promise = kdn.checkData(OrderCode, ShipperCode, LogisticCode);
promise.then(function (value) {
console.log(value)
var listan = value.Traces
}, function (error) {
console.log(error)
});
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})