@tmsfe/tms-core 0.0.119 → 0.0.121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.119",
3
+ "version": "0.0.121",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
package/src/config.js CHANGED
@@ -4,7 +4,6 @@
4
4
  import Request from './request';
5
5
  import { getEnvInfo } from './env';
6
6
 
7
- const api = new Request();
8
7
  const parseAllCfgs = (configPaths, resData, defaultCfgs) => configPaths.map((path, index) => {
9
8
  const found = resData.find(cfg => cfg.configPath === path);
10
9
  if (found) {
@@ -61,7 +60,7 @@ function getConfig(configPath, extendAttr = {}, defaultCfg) {
61
60
  const configPaths = formatConfigPaths(configPath);
62
61
  const defaultCfgs = (defaultCfg && (Array.isArray(defaultCfg) ? defaultCfg : [defaultCfg])) || null;
63
62
  const extendAttrs = typeof extendAttr === 'string' ? extendAttr : JSON.stringify(extendAttr);
64
- return api.post('marketing/config', {
63
+ return Request.getInstance().post('marketing/config', {
65
64
  extendAttrs,
66
65
  configPaths,
67
66
  })
@@ -104,8 +103,7 @@ function getApolloConfig(configPath, extendAttr = {}, defaultCfg) {
104
103
  const configPaths = formatConfigPaths(configPath);
105
104
  const defaultCfgs = (defaultCfg && (Array.isArray(defaultCfg) ? defaultCfg : [defaultCfg])) || null;
106
105
  const { businessKey, moduleKey, ...extendAttrs } = extendAttr;
107
- const api = new Request();
108
- return api.post('marketing/apollo/config', {
106
+ return Request.getInstance().post('marketing/apollo/config', {
109
107
  businessKey,
110
108
  moduleKey,
111
109
  configPaths,
@@ -37,7 +37,6 @@ interface IpLocationType {
37
37
  },
38
38
  }
39
39
 
40
- const request = new Request();
41
40
  const event = new EventDispatcher();
42
41
 
43
42
  const userLocationCache: UserLocationType = {}; // 获取位置缓存
@@ -104,35 +103,36 @@ class Location extends LocationBase {
104
103
  return userLocationCache[cacheName];
105
104
  }
106
105
 
107
- userLocationCache[cacheName] = request.post('basic/lbs/decode', { lat, lng, getPoi }).then((res) => {
108
- const { errCode, resData = {} } = res;
106
+ userLocationCache[cacheName] = Request.getInstance().post('basic/lbs/decode', { lat, lng, getPoi })
107
+ .then((res) => {
108
+ const { errCode, resData = {} } = res;
109
109
 
110
- if (errCode === 0) {
111
- const { result = {} } = resData;
112
- const { ad_info: adInfo = {} } = result;
110
+ if (errCode === 0) {
111
+ const { result = {} } = resData;
112
+ const { ad_info: adInfo = {} } = result;
113
113
 
114
- const loc = {
115
- nationCode: adInfo.nation_code,
116
- province: adInfo.province,
117
- cityName: adInfo.city,
118
- district: adInfo.district,
119
- adCode: adInfo.adcode,
120
- cityCode: formatCityCode(adInfo.city_code, adInfo.nation_code),
121
- latitude: lat,
122
- longitude: lng,
123
- };
114
+ const loc = {
115
+ nationCode: adInfo.nation_code,
116
+ province: adInfo.province,
117
+ cityName: adInfo.city,
118
+ district: adInfo.district,
119
+ adCode: adInfo.adcode,
120
+ cityCode: formatCityCode(adInfo.city_code, adInfo.nation_code),
121
+ latitude: lat,
122
+ longitude: lng,
123
+ };
124
124
 
125
- if (getPoi === 0) {
125
+ if (getPoi === 0) {
126
+ return loc;
127
+ }
128
+
129
+ loc.adCode = adInfo.adcode;
130
+ (loc as typeof loc & { poi: object }).poi = formatPoi(result);
126
131
  return loc;
127
132
  }
128
133
 
129
- loc.adCode = adInfo.adcode;
130
- (loc as typeof loc & { poi: object }).poi = formatPoi(result);
131
- return loc;
132
- }
133
-
134
- return Promise.reject(res);
135
- })
134
+ return Promise.reject(res);
135
+ })
136
136
  .catch(err => Promise.reject(err));
137
137
 
138
138
  return userLocationCache[cacheName];
@@ -324,7 +324,7 @@ class Location extends LocationBase {
324
324
  const { latitude, longitude } = (await this.getMergedLocation() as PostionType);
325
325
  from = `${latitude},${longitude}`;
326
326
  }
327
- return request.post('basic/lbs/direction', { from, to, mode });
327
+ return Request.getInstance().post('basic/lbs/direction', { from, to, mode });
328
328
  }
329
329
 
330
330
  /**
@@ -336,7 +336,7 @@ class Location extends LocationBase {
336
336
  getIpLocation(force = false): Promise<IpLocationType> {
337
337
  if (ipLocationPromise === null || force) {
338
338
  ipLocationPromise = new Promise((resolve, reject) => {
339
- request.post('basic/lbs/decodeip')
339
+ Request.getInstance().post('basic/lbs/decodeip')
340
340
  .then((res) => {
341
341
  if (res.errCode === 0) {
342
342
  resolve(res.resData as IpLocationType);
package/src/md5.js CHANGED
@@ -1,187 +1,375 @@
1
- /**
2
- * @author Davis.Lu <davislu@tencent.com>
3
- * @desc: 基于md5算法对源字符串进行hash,生成hash字符串
4
- **/
5
-
1
+ /* eslint-disable no-param-reassign */
6
2
  /**
7
3
  * @public
8
4
  * @description 基于md5算法对源字符串进行hash,生成hash字符串
9
5
  * @param {String} str 源字符串
10
6
  * @returns {String} 源字符串的md5 hash值
7
+ * MIT https://github.com/blueimp/JavaScript-MD5
11
8
  */
12
- const md5 = function (str) {
13
- /**
14
- * 将unicode编码成utf-8
15
- * @private
16
- * @param {string} encoedStr unicode字符
17
- * @returns {string} utf8格式的字符串
18
- */
19
- const encodeUtf8 = (encoedStr) => {
20
- const string = encoedStr.replace(/\r\n/g, '\n');
21
- /**
22
- * @private
23
- * @param {string} c unicode字符
24
- * @returns {string} 字符串
25
- */
26
- const charCode = c => String.fromCharCode(c);
27
- const utftextArr = [];
28
- for (let n = 0; n < string.length; n += 1) {
29
- let c = string.charCodeAt(n);
30
- if (c < 128) {
31
- utftextArr.push(charCode(c));
32
- } else if (c < 2048) {
33
- utftextArr.push(charCode((c >> 6) | 192), charCode((c & 63) | 128));
34
- } else if (c < 55296 || c >= 57344) {
35
- utftextArr.push(
36
- charCode((c >> 12) | 224),
37
- charCode(((c >> 6) & 63) | 128),
38
- charCode((c & 63) | 128),
39
- );
40
- } else {
41
- c = 65536 + (((c & 1023) << 10) | (string.charCodeAt(n += 1) & 1023));
42
- utftextArr.push(
43
- charCode((c >> 18) | 240),
44
- charCode(((c >> 12) & 63) | 128),
45
- charCode(((c >> 6) & 63) | 128),
46
- charCode((c & 63) | 128),
47
- );
48
- }
49
- }
50
- return utftextArr.join('');
51
- };
52
-
53
- /**
54
- * @private
55
- * @param {string} string 字符串
56
- * @returns {array} 字符串分组
57
- */
58
- const convertToWordArray = (string) => {
59
- const msgLen = string.length;
60
- const lNumberOfWords = ((((msgLen + 8) - ((msgLen + 8) % 64)) / 64) + 1) * 16;
61
- const lWordArray = Array(lNumberOfWords - 1);
62
- let lByteCount = 0;
63
- while (lByteCount <= msgLen) {
64
- const wordCount = (lByteCount - (lByteCount % 4)) / 4;
65
- const lBytePosition = (lByteCount % 4) * 8;
66
- const byteWord = lByteCount === msgLen ? 0x80 : string.charCodeAt(lByteCount);
67
- lWordArray[wordCount] |= (byteWord << lBytePosition);
68
- lByteCount += 1;
69
- }
70
- lWordArray[lNumberOfWords - 2] = msgLen << 3;
71
- lWordArray[lNumberOfWords - 1] = msgLen >>> 29;
72
- return lWordArray;
73
- };
74
-
75
- /**
76
- * @private
77
- * @param {string} lValue 字符串
78
- * @param {number} iShiftBits 移动位数
79
- * @returns {string} 字符串
80
- */
81
- const rotateLeft = (
82
- lValue,
83
- iShiftBits,
84
- ) => (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
85
-
86
- /**
87
- * @private
88
- * @param {string} lX 字符串
89
- * @param {string} lY 字符串
90
- * @returns {string} 字符串
91
- */
92
- const addUnsigned = (lX, lY) => {
93
- const lX8 = (lX & 0x80000000);
94
- const lY8 = (lY & 0x80000000);
95
- const lX4 = (lX & 0x40000000);
96
- const lY4 = (lY & 0x40000000);
97
- const lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
98
- if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
99
- if (!(lX4 | lY4)) return (lResult ^ lX8 ^ lY8);
100
- return (lResult & 0x40000000)
101
- ? (lResult ^ 0xC0000000 ^ lX8 ^ lY8)
102
- : (lResult ^ 0x40000000 ^ lX8 ^ lY8);
103
- };
104
-
105
- /**
106
- * @private
107
- * @param {object} recycleData 对象
108
- * @returns {string} 字符串
109
- */
110
- const addRecycling = (recycleData) => {
111
- const { FN, a, b, c, d, x, s, ac } = recycleData;
112
- const aa = addUnsigned(a, addUnsigned(addUnsigned(FN(b, c, d), x), ac));
113
- return addUnsigned(rotateLeft(aa, s), b);
114
- };
115
-
116
- /**
117
- * @private
118
- * @param {string} lValue 字符串
119
- * @returns {string} 字符串
120
- */
121
- const wordToHex = (lValue) => {
122
- let WordToHexValue = '';
123
- for (let lCount = 0; lCount <= 3; lCount += 1) {
124
- const lByte = (lValue >>> (lCount * 8)) & 255;
125
- const WordToHexValueTemp = `0${lByte.toString(16)}`;
126
- WordToHexValue += WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2);
9
+ /**
10
+ * Add integers, wrapping at 2^32.
11
+ * This uses 16-bit operations internally to work around bugs in interpreters.
12
+ *
13
+ * @param {number} x First integer
14
+ * @param {number} y Second integer
15
+ * @returns {number} Sum
16
+ */
17
+ function safeAdd(x, y) {
18
+ const lsw = (x & 0xffff) + (y & 0xffff);
19
+ const msw = (x >> 16) + (y >> 16) + (lsw >> 16);
20
+ return (msw << 16) | (lsw & 0xffff);
21
+ }
22
+
23
+ /**
24
+ * Bitwise rotate a 32-bit number to the left.
25
+ *
26
+ * @param {number} num 32-bit number
27
+ * @param {number} cnt Rotation count
28
+ * @returns {number} Rotated number
29
+ */
30
+ function bitRotateLeft(num, cnt) {
31
+ return (num << cnt) | (num >>> (32 - cnt));
32
+ }
33
+
34
+ /**
35
+ * Basic operation the algorithm uses.
36
+ *
37
+ * @param {number} q q
38
+ * @param {number} a a
39
+ * @param {number} b b
40
+ * @param {number} x x
41
+ * @param {number} s s
42
+ * @param {number} t t
43
+ * @returns {number} Result
44
+ */
45
+ function md5cmn(q, a, b, x, s, t) {
46
+ return safeAdd(bitRotateLeft(safeAdd(safeAdd(a, q), safeAdd(x, t)), s), b);
47
+ }
48
+ /**
49
+ * Basic operation the algorithm uses.
50
+ *
51
+ * @param {number} a a
52
+ * @param {number} b b
53
+ * @param {number} c c
54
+ * @param {number} d d
55
+ * @param {number} x x
56
+ * @param {number} s s
57
+ * @param {number} t t
58
+ * @returns {number} Result
59
+ */
60
+ function md5ff(a, b, c, d, x, s, t) {
61
+ return md5cmn((b & c) | (~b & d), a, b, x, s, t);
62
+ }
63
+ /**
64
+ * Basic operation the algorithm uses.
65
+ *
66
+ * @param {number} a a
67
+ * @param {number} b b
68
+ * @param {number} c c
69
+ * @param {number} d d
70
+ * @param {number} x x
71
+ * @param {number} s s
72
+ * @param {number} t t
73
+ * @returns {number} Result
74
+ */
75
+ function md5gg(a, b, c, d, x, s, t) {
76
+ return md5cmn((b & d) | (c & ~d), a, b, x, s, t);
77
+ }
78
+ /**
79
+ * Basic operation the algorithm uses.
80
+ *
81
+ * @param {number} a a
82
+ * @param {number} b b
83
+ * @param {number} c c
84
+ * @param {number} d d
85
+ * @param {number} x x
86
+ * @param {number} s s
87
+ * @param {number} t t
88
+ * @returns {number} Result
89
+ */
90
+ function md5hh(a, b, c, d, x, s, t) {
91
+ return md5cmn(b ^ c ^ d, a, b, x, s, t);
92
+ }
93
+ /**
94
+ * Basic operation the algorithm uses.
95
+ *
96
+ * @param {number} a a
97
+ * @param {number} b b
98
+ * @param {number} c c
99
+ * @param {number} d d
100
+ * @param {number} x x
101
+ * @param {number} s s
102
+ * @param {number} t t
103
+ * @returns {number} Result
104
+ */
105
+ function md5ii(a, b, c, d, x, s, t) {
106
+ return md5cmn(c ^ (b | ~d), a, b, x, s, t);
107
+ }
108
+
109
+ /**
110
+ * Calculate the MD5 of an array of little-endian words, and a bit length.
111
+ *
112
+ * @param {Array} x Array of little-endian words
113
+ * @param {number} len Bit length
114
+ * @returns {Array<number>} MD5 Array
115
+ */
116
+ function binlMD5(x, len) {
117
+ /* append padding */
118
+ x[len >> 5] |= 0x80 << len % 32;
119
+ x[(((len + 64) >>> 9) << 4) + 14] = len;
120
+
121
+ let i;
122
+ let olda;
123
+ let oldb;
124
+ let oldc;
125
+ let oldd;
126
+ let a = 1732584193;
127
+ let b = -271733879;
128
+ let c = -1732584194;
129
+ let d = 271733878;
130
+
131
+ for (i = 0; i < x.length; i += 16) {
132
+ olda = a;
133
+ oldb = b;
134
+ oldc = c;
135
+ oldd = d;
136
+
137
+ a = md5ff(a, b, c, d, x[i], 7, -680876936);
138
+ d = md5ff(d, a, b, c, x[i + 1], 12, -389564586);
139
+ c = md5ff(c, d, a, b, x[i + 2], 17, 606105819);
140
+ b = md5ff(b, c, d, a, x[i + 3], 22, -1044525330);
141
+ a = md5ff(a, b, c, d, x[i + 4], 7, -176418897);
142
+ d = md5ff(d, a, b, c, x[i + 5], 12, 1200080426);
143
+ c = md5ff(c, d, a, b, x[i + 6], 17, -1473231341);
144
+ b = md5ff(b, c, d, a, x[i + 7], 22, -45705983);
145
+ a = md5ff(a, b, c, d, x[i + 8], 7, 1770035416);
146
+ d = md5ff(d, a, b, c, x[i + 9], 12, -1958414417);
147
+ c = md5ff(c, d, a, b, x[i + 10], 17, -42063);
148
+ b = md5ff(b, c, d, a, x[i + 11], 22, -1990404162);
149
+ a = md5ff(a, b, c, d, x[i + 12], 7, 1804603682);
150
+ d = md5ff(d, a, b, c, x[i + 13], 12, -40341101);
151
+ c = md5ff(c, d, a, b, x[i + 14], 17, -1502002290);
152
+ b = md5ff(b, c, d, a, x[i + 15], 22, 1236535329);
153
+
154
+ a = md5gg(a, b, c, d, x[i + 1], 5, -165796510);
155
+ d = md5gg(d, a, b, c, x[i + 6], 9, -1069501632);
156
+ c = md5gg(c, d, a, b, x[i + 11], 14, 643717713);
157
+ b = md5gg(b, c, d, a, x[i], 20, -373897302);
158
+ a = md5gg(a, b, c, d, x[i + 5], 5, -701558691);
159
+ d = md5gg(d, a, b, c, x[i + 10], 9, 38016083);
160
+ c = md5gg(c, d, a, b, x[i + 15], 14, -660478335);
161
+ b = md5gg(b, c, d, a, x[i + 4], 20, -405537848);
162
+ a = md5gg(a, b, c, d, x[i + 9], 5, 568446438);
163
+ d = md5gg(d, a, b, c, x[i + 14], 9, -1019803690);
164
+ c = md5gg(c, d, a, b, x[i + 3], 14, -187363961);
165
+ b = md5gg(b, c, d, a, x[i + 8], 20, 1163531501);
166
+ a = md5gg(a, b, c, d, x[i + 13], 5, -1444681467);
167
+ d = md5gg(d, a, b, c, x[i + 2], 9, -51403784);
168
+ c = md5gg(c, d, a, b, x[i + 7], 14, 1735328473);
169
+ b = md5gg(b, c, d, a, x[i + 12], 20, -1926607734);
170
+
171
+ a = md5hh(a, b, c, d, x[i + 5], 4, -378558);
172
+ d = md5hh(d, a, b, c, x[i + 8], 11, -2022574463);
173
+ c = md5hh(c, d, a, b, x[i + 11], 16, 1839030562);
174
+ b = md5hh(b, c, d, a, x[i + 14], 23, -35309556);
175
+ a = md5hh(a, b, c, d, x[i + 1], 4, -1530992060);
176
+ d = md5hh(d, a, b, c, x[i + 4], 11, 1272893353);
177
+ c = md5hh(c, d, a, b, x[i + 7], 16, -155497632);
178
+ b = md5hh(b, c, d, a, x[i + 10], 23, -1094730640);
179
+ a = md5hh(a, b, c, d, x[i + 13], 4, 681279174);
180
+ d = md5hh(d, a, b, c, x[i], 11, -358537222);
181
+ c = md5hh(c, d, a, b, x[i + 3], 16, -722521979);
182
+ b = md5hh(b, c, d, a, x[i + 6], 23, 76029189);
183
+ a = md5hh(a, b, c, d, x[i + 9], 4, -640364487);
184
+ d = md5hh(d, a, b, c, x[i + 12], 11, -421815835);
185
+ c = md5hh(c, d, a, b, x[i + 15], 16, 530742520);
186
+ b = md5hh(b, c, d, a, x[i + 2], 23, -995338651);
187
+
188
+ a = md5ii(a, b, c, d, x[i], 6, -198630844);
189
+ d = md5ii(d, a, b, c, x[i + 7], 10, 1126891415);
190
+ c = md5ii(c, d, a, b, x[i + 14], 15, -1416354905);
191
+ b = md5ii(b, c, d, a, x[i + 5], 21, -57434055);
192
+ a = md5ii(a, b, c, d, x[i + 12], 6, 1700485571);
193
+ d = md5ii(d, a, b, c, x[i + 3], 10, -1894986606);
194
+ c = md5ii(c, d, a, b, x[i + 10], 15, -1051523);
195
+ b = md5ii(b, c, d, a, x[i + 1], 21, -2054922799);
196
+ a = md5ii(a, b, c, d, x[i + 8], 6, 1873313359);
197
+ d = md5ii(d, a, b, c, x[i + 15], 10, -30611744);
198
+ c = md5ii(c, d, a, b, x[i + 6], 15, -1560198380);
199
+ b = md5ii(b, c, d, a, x[i + 13], 21, 1309151649);
200
+ a = md5ii(a, b, c, d, x[i + 4], 6, -145523070);
201
+ d = md5ii(d, a, b, c, x[i + 11], 10, -1120210379);
202
+ c = md5ii(c, d, a, b, x[i + 2], 15, 718787259);
203
+ b = md5ii(b, c, d, a, x[i + 9], 21, -343485551);
204
+
205
+ a = safeAdd(a, olda);
206
+ b = safeAdd(b, oldb);
207
+ c = safeAdd(c, oldc);
208
+ d = safeAdd(d, oldd);
209
+ }
210
+ return [a, b, c, d];
211
+ }
212
+
213
+ /**
214
+ * Convert an array of little-endian words to a string
215
+ *
216
+ * @param {Array<number>} input MD5 Array
217
+ * @returns {string} MD5 string
218
+ */
219
+ function binl2rstr(input) {
220
+ let i;
221
+ let output = '';
222
+ const length32 = input.length * 32;
223
+ for (i = 0; i < length32; i += 8) {
224
+ output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff);
225
+ }
226
+ return output;
227
+ }
228
+
229
+ /**
230
+ * Convert a raw string to an array of little-endian words
231
+ * Characters >255 have their high-byte silently ignored.
232
+ *
233
+ * @param {string} input Raw input string
234
+ * @returns {Array<number>} Array of little-endian words
235
+ */
236
+ function rstr2binl(input) {
237
+ let i;
238
+ const output = [];
239
+ output[(input.length >> 2) - 1] = undefined;
240
+ for (i = 0; i < output.length; i += 1) {
241
+ output[i] = 0;
242
+ }
243
+ const length8 = input.length * 8;
244
+ for (i = 0; i < length8; i += 8) {
245
+ output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32;
246
+ }
247
+ return output;
248
+ }
249
+
250
+ /**
251
+ * Calculate the MD5 of a raw string
252
+ *
253
+ * @param {string} s Input string
254
+ * @returns {string} Raw MD5 string
255
+ */
256
+ function rstrMD5(s) {
257
+ return binl2rstr(binlMD5(rstr2binl(s), s.length * 8));
258
+ }
259
+
260
+ /**
261
+ * Calculates the HMAC-MD5 of a key and some data (raw strings)
262
+ *
263
+ * @param {string} key HMAC key
264
+ * @param {string} data Raw input string
265
+ * @returns {string} Raw MD5 string
266
+ */
267
+ function rstrHMACMD5(key, data) {
268
+ let i;
269
+ let bkey = rstr2binl(key);
270
+ const ipad = [];
271
+ const opad = [];
272
+ ipad[15] = undefined;
273
+ opad[15] = undefined;
274
+ if (bkey.length > 16) {
275
+ bkey = binlMD5(bkey, key.length * 8);
276
+ }
277
+ for (i = 0; i < 16; i += 1) {
278
+ ipad[i] = bkey[i] ^ 0x36363636;
279
+ opad[i] = bkey[i] ^ 0x5c5c5c5c;
280
+ }
281
+ const hash = binlMD5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
282
+ return binl2rstr(binlMD5(opad.concat(hash), 512 + 128));
283
+ }
284
+
285
+ /**
286
+ * Convert a raw string to a hex string
287
+ *
288
+ * @param {string} input Raw input string
289
+ * @returns {string} Hex encoded string
290
+ */
291
+ function rstr2hex(input) {
292
+ const hexTab = '0123456789abcdef';
293
+ let output = '';
294
+ let x;
295
+ let i;
296
+ for (i = 0; i < input.length; i += 1) {
297
+ x = input.charCodeAt(i);
298
+ output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
299
+ }
300
+ return output;
301
+ }
302
+
303
+ /**
304
+ * Encode a string as UTF-8
305
+ *
306
+ * @param {string} input Input string
307
+ * @returns {string} UTF8 string
308
+ */
309
+ function str2rstrUTF8(input) {
310
+ return unescape(encodeURIComponent(input));
311
+ }
312
+
313
+ /**
314
+ * Encodes input string as raw MD5 string
315
+ *
316
+ * @param {string} s Input string
317
+ * @returns {string} Raw MD5 string
318
+ */
319
+ function rawMD5(s) {
320
+ return rstrMD5(str2rstrUTF8(s));
321
+ }
322
+ /**
323
+ * Encodes input string as Hex encoded string
324
+ *
325
+ * @param {string} s Input string
326
+ * @returns {string} Hex encoded string
327
+ */
328
+ function hexMD5(s) {
329
+ return rstr2hex(rawMD5(s));
330
+ }
331
+ /**
332
+ * Calculates the raw HMAC-MD5 for the given key and data
333
+ *
334
+ * @param {string} k HMAC key
335
+ * @param {string} d Input string
336
+ * @returns {string} Raw MD5 string
337
+ */
338
+ function rawHMACMD5(k, d) {
339
+ return rstrHMACMD5(str2rstrUTF8(k), str2rstrUTF8(d));
340
+ }
341
+ /**
342
+ * Calculates the Hex encoded HMAC-MD5 for the given key and data
343
+ *
344
+ * @param {string} k HMAC key
345
+ * @param {string} d Input string
346
+ * @returns {string} Raw MD5 string
347
+ */
348
+ function hexHMACMD5(k, d) {
349
+ return rstr2hex(rawHMACMD5(k, d));
350
+ }
351
+
352
+ /**
353
+ * Calculates MD5 value for a given string.
354
+ * If a key is provided, calculates the HMAC-MD5 value.
355
+ * Returns a Hex encoded string unless the raw argument is given.
356
+ *
357
+ * @param {string} string Input string
358
+ * @param {string} [key] HMAC key
359
+ * @param {boolean} [raw] Raw output switch
360
+ * @returns {string} MD5 output
361
+ */
362
+ function md5(string, key, raw) {
363
+ if (!key) {
364
+ if (!raw) {
365
+ return hexMD5(string);
127
366
  }
128
- return WordToHexValue;
129
- };
130
- let [a, b, c, d] = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476];
131
- const sArr = [[7, 12, 17, 22], [5, 9, 14, 20], [4, 11, 16, 23], [6, 10, 15, 21]];
132
- const kiArr = ('16b05af49e38d27c58be147ad0369cf207e5c3a18f6d4b29').split('').map(n => parseInt(n, 16));
133
- const hxArr = [
134
- 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
135
- 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
136
- 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x2441453, 0xD8A1E681, 0xE7D3FBC8,
137
- 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
138
- 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
139
- 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x4881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
140
- 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
141
- 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391,
142
- ];
143
- // eslint-disable-next-line require-jsdoc
144
- const cyc = (i, r = 0) => (i + r) % 4;
145
- // 4组处理位操作函数
146
- // eslint-disable-next-line require-jsdoc
147
- const md5F = (x, y, z) => (x & y) | ((~x) & z);
148
- // eslint-disable-next-line require-jsdoc
149
- const md5G = (x, y, z) => (x & z) | (y & (~z));
150
- // eslint-disable-next-line require-jsdoc
151
- const md5H = (x, y, z) => (x ^ y ^ z);
152
- // eslint-disable-next-line require-jsdoc
153
- const md5I = (x, y, z) => (y ^ (x | (~z)));
154
- const string = encodeUtf8(str);
155
- const x = convertToWordArray(string);
156
- for (let k = 0; k < x.length; k += 16) {
157
- const AA = a;
158
- const BB = b;
159
- const CC = c;
160
- const DD = d;
161
- const arr = [a, d, c, b];
162
- hxArr.forEach((hx, m) => {
163
- const i = m % 16;
164
- const g = m / 16 << 0;
165
- const ki = m < 16 ? m : kiArr[m - 16];
166
- const FN = [md5F, md5G, md5H, md5I][g];
167
-
168
- arr[cyc(i)] = addRecycling({
169
- FN,
170
- a: arr[cyc(i)],
171
- b: arr[cyc(i, 3)],
172
- c: arr[cyc(i, 2)],
173
- d: arr[cyc(i, 1)],
174
- x: x[k + ki],
175
- s: sArr[g][i % 4],
176
- ac: hx,
177
- });
178
- });
179
- a = addUnsigned(arr[0], AA);
180
- b = addUnsigned(arr[3], BB);
181
- c = addUnsigned(arr[2], CC);
182
- d = addUnsigned(arr[1], DD);
367
+ return rawMD5(string);
368
+ }
369
+ if (!raw) {
370
+ return hexHMACMD5(key, string);
183
371
  }
184
- return (wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d)).toLowerCase();
185
- };
372
+ return rawHMACMD5(key, string);
373
+ }
186
374
 
187
375
  export default md5;
package/src/mpInfo.js CHANGED
@@ -16,7 +16,7 @@ import Request from './request';
16
16
  * tms.getMpOpenId('xxx', '111')
17
17
  */
18
18
  async function getMpOpenId(mpId, userId) {
19
- const { resData } = await new Request().post('user/mpinfo', { userId, mpId });
19
+ const { resData } = await Request.getInstance().post('user/mpinfo', { userId, mpId });
20
20
  const { openId = '' } = resData || {};
21
21
  return openId;
22
22
  }
@@ -33,7 +33,7 @@ async function getMpOpenId(mpId, userId) {
33
33
  * tms.getMpOpenId('xxx', '111')
34
34
  */
35
35
  async function getOuterOpenId(apiKey) {
36
- const { resData } = await new Request().post('user/mpinfo', { mpId: apiKey });
36
+ const { resData } = await Request.getInstance().post('user/mpinfo', { mpId: apiKey });
37
37
  const { openId = '' } = resData || {};
38
38
  return openId;
39
39
  }
package/src/request.js CHANGED
@@ -110,6 +110,8 @@ const modifyAuthParam = async (param, withAuth) => {
110
110
  return requestParam;
111
111
  };
112
112
 
113
+ let reqInstance = null;
114
+
113
115
  /**
114
116
  * @public
115
117
  * @class Request
@@ -129,6 +131,13 @@ export default class Request {
129
131
  */
130
132
  static defaultSecretKey = '';
131
133
 
134
+ static getInstance() {
135
+ if (reqInstance === null) {
136
+ reqInstance = new Request();
137
+ }
138
+ return reqInstance;
139
+ }
140
+
132
141
  host = '';
133
142
  secretKey = '';
134
143
  withAuth = true;