@tmsfe/tms-core 0.0.1 → 0.0.5

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/src/md5.js ADDED
@@ -0,0 +1,190 @@
1
+ /**
2
+ * @copyright 2017-present, Tencent, Inc. All rights reserved.
3
+ * @author Davis.Lu <davislu@tencent.com>
4
+ *
5
+ * @file crypto tools.
6
+ *
7
+ **/
8
+
9
+ /**
10
+ * @public
11
+ * @description 基于md5算法对源字符串进行hash,生成hash字符串
12
+ * @param {String} str 源字符串
13
+ * @returns {String} 源字符串的md5 hash值
14
+ */
15
+ const md5 = function (str) {
16
+ /**
17
+ * 将unicode编码成utf-8
18
+ * @private
19
+ * @param {string} encoedStr unicode字符
20
+ * @returns {string} utf8格式的字符串
21
+ */
22
+ const encodeUtf8 = (encoedStr) => {
23
+ const string = encoedStr.replace(/\r\n/g, '\n');
24
+ /**
25
+ * @private
26
+ * @param {string} c unicode字符
27
+ * @returns {string} 字符串
28
+ */
29
+ const charCode = c => String.fromCharCode(c);
30
+ const utftextArr = [];
31
+ for (let n = 0; n < string.length; n += 1) {
32
+ let c = string.charCodeAt(n);
33
+ if (c < 128) {
34
+ utftextArr.push(charCode(c));
35
+ } else if (c < 2048) {
36
+ utftextArr.push(charCode((c >> 6) | 192), charCode((c & 63) | 128));
37
+ } else if (c < 55296 || c >= 57344) {
38
+ utftextArr.push(
39
+ charCode((c >> 12) | 224),
40
+ charCode(((c >> 6) & 63) | 128),
41
+ charCode((c & 63) | 128),
42
+ );
43
+ } else {
44
+ c = 65536 + (((c & 1023) << 10) | (string.charCodeAt(n += 1) & 1023));
45
+ utftextArr.push(
46
+ charCode((c >> 18) | 240),
47
+ charCode(((c >> 12) & 63) | 128),
48
+ charCode(((c >> 6) & 63) | 128),
49
+ charCode((c & 63) | 128),
50
+ );
51
+ }
52
+ }
53
+ return utftextArr.join('');
54
+ };
55
+
56
+ /**
57
+ * @private
58
+ * @param {string} string 字符串
59
+ * @returns {array} 字符串分组
60
+ */
61
+ const convertToWordArray = (string) => {
62
+ const msgLen = string.length;
63
+ const lNumberOfWords = ((((msgLen + 8) - ((msgLen + 8) % 64)) / 64) + 1) * 16;
64
+ const lWordArray = Array(lNumberOfWords - 1);
65
+ let lByteCount = 0;
66
+ while (lByteCount <= msgLen) {
67
+ const wordCount = (lByteCount - (lByteCount % 4)) / 4;
68
+ const lBytePosition = (lByteCount % 4) * 8;
69
+ const byteWord = lByteCount === msgLen ? 0x80 : string.charCodeAt(lByteCount);
70
+ lWordArray[wordCount] |= (byteWord << lBytePosition);
71
+ lByteCount += 1;
72
+ }
73
+ lWordArray[lNumberOfWords - 2] = msgLen << 3;
74
+ lWordArray[lNumberOfWords - 1] = msgLen >>> 29;
75
+ return lWordArray;
76
+ };
77
+
78
+ /**
79
+ * @private
80
+ * @param {string} lValue 字符串
81
+ * @param {number} iShiftBits 移动位数
82
+ * @returns {string} 字符串
83
+ */
84
+ const rotateLeft = (
85
+ lValue,
86
+ iShiftBits,
87
+ ) => (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
88
+
89
+ /**
90
+ * @private
91
+ * @param {string} lX 字符串
92
+ * @param {string} lY 字符串
93
+ * @returns {string} 字符串
94
+ */
95
+ const addUnsigned = (lX, lY) => {
96
+ const lX8 = (lX & 0x80000000);
97
+ const lY8 = (lY & 0x80000000);
98
+ const lX4 = (lX & 0x40000000);
99
+ const lY4 = (lY & 0x40000000);
100
+ const lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
101
+ if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
102
+ if (!(lX4 | lY4)) return (lResult ^ lX8 ^ lY8);
103
+ return (lResult & 0x40000000)
104
+ ? (lResult ^ 0xC0000000 ^ lX8 ^ lY8)
105
+ : (lResult ^ 0x40000000 ^ lX8 ^ lY8);
106
+ };
107
+
108
+ /**
109
+ * @private
110
+ * @param {object} recycleData 对象
111
+ * @returns {string} 字符串
112
+ */
113
+ const addRecycling = (recycleData) => {
114
+ const { FN, a, b, c, d, x, s, ac } = recycleData;
115
+ const aa = addUnsigned(a, addUnsigned(addUnsigned(FN(b, c, d), x), ac));
116
+ return addUnsigned(rotateLeft(aa, s), b);
117
+ };
118
+
119
+ /**
120
+ * @private
121
+ * @param {string} lValue 字符串
122
+ * @returns {string} 字符串
123
+ */
124
+ const wordToHex = (lValue) => {
125
+ let WordToHexValue = '';
126
+ for (let lCount = 0; lCount <= 3; lCount += 1) {
127
+ const lByte = (lValue >>> (lCount * 8)) & 255;
128
+ const WordToHexValueTemp = `0${lByte.toString(16)}`;
129
+ WordToHexValue += WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2);
130
+ }
131
+ return WordToHexValue;
132
+ };
133
+ let [a, b, c, d] = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476];
134
+ const sArr = [[7, 12, 17, 22], [5, 9, 14, 20], [4, 11, 16, 23], [6, 10, 15, 21]];
135
+ const kiArr = ('16b05af49e38d27c58be147ad0369cf207e5c3a18f6d4b29').split('').map(n => parseInt(n, 16));
136
+ const hxArr = [
137
+ 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
138
+ 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
139
+ 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x2441453, 0xD8A1E681, 0xE7D3FBC8,
140
+ 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
141
+ 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
142
+ 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x4881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
143
+ 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
144
+ 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391,
145
+ ];
146
+ // eslint-disable-next-line require-jsdoc
147
+ const cyc = (i, r = 0) => (i + r) % 4;
148
+ // 4组处理位操作函数
149
+ // eslint-disable-next-line require-jsdoc
150
+ const md5F = (x, y, z) => (x & y) | ((~x) & z);
151
+ // eslint-disable-next-line require-jsdoc
152
+ const md5G = (x, y, z) => (x & z) | (y & (~z));
153
+ // eslint-disable-next-line require-jsdoc
154
+ const md5H = (x, y, z) => (x ^ y ^ z);
155
+ // eslint-disable-next-line require-jsdoc
156
+ const md5I = (x, y, z) => (y ^ (x | (~z)));
157
+ const string = encodeUtf8(str);
158
+ const x = convertToWordArray(string);
159
+ for (let k = 0; k < x.length; k += 16) {
160
+ const AA = a;
161
+ const BB = b;
162
+ const CC = c;
163
+ const DD = d;
164
+ const arr = [a, d, c, b];
165
+ hxArr.forEach((hx, m) => {
166
+ const i = m % 16;
167
+ const g = m / 16 << 0;
168
+ const ki = m < 16 ? m : kiArr[m - 16];
169
+ const FN = [md5F, md5G, md5H, md5I][g];
170
+
171
+ arr[cyc(i)] = addRecycling({
172
+ FN,
173
+ a: arr[cyc(i)],
174
+ b: arr[cyc(i, 3)],
175
+ c: arr[cyc(i, 2)],
176
+ d: arr[cyc(i, 1)],
177
+ x: x[k + ki],
178
+ s: sArr[g][i % 4],
179
+ ac: hx,
180
+ });
181
+ });
182
+ a = addUnsigned(arr[0], AA);
183
+ b = addUnsigned(arr[3], BB);
184
+ c = addUnsigned(arr[2], CC);
185
+ d = addUnsigned(arr[1], DD);
186
+ }
187
+ return (wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d)).toLowerCase();
188
+ };
189
+
190
+ export default md5;
package/src/mpInfo.js ADDED
@@ -0,0 +1,38 @@
1
+ /**
2
+ * 支持服务接入相关接口
3
+ */
4
+ import Request from './request';
5
+
6
+ /**
7
+ * getMpOpenId 获取接入方用户唯一标识 [变更为 getOuterOpenId]
8
+ * @private
9
+ * @description 唯一标识 openId 用于与服务商建立账号关联关系
10
+ * @category 服务接入
11
+ * @param {String} mpId 接入服务商渠道标识
12
+ * @param {String} userId 出行用户标识
13
+ * @returns {Promise<String>} 返回 openId,失败时返回空
14
+ */
15
+ async function getMpOpenId(mpId, userId) {
16
+ const { resData } = await new Request().post('user/mpinfo', { userId, mpId });
17
+ const { openId = '' } = resData || {};
18
+ return openId;
19
+ }
20
+
21
+ /**
22
+ * getOuterOpenId 获取接入方用户唯一标识
23
+ * @public
24
+ * @description 唯一标识 openId 用于服务接入方作为唯一标识、向腾讯出行服务同步订单等
25
+ * @category 服务接入
26
+ * @param {String} apiKey 服务接入方渠道标识
27
+ * @returns {Promise<String>} 返回 openId,失败时返回空
28
+ */
29
+ async function getOuterOpenId(apiKey) {
30
+ const { resData } = await new Request().post('user/mpinfo', { mpId: apiKey });
31
+ const { openId = '' } = resData || {};
32
+ return openId;
33
+ }
34
+
35
+ export {
36
+ getMpOpenId,
37
+ getOuterOpenId,
38
+ };
@@ -0,0 +1,52 @@
1
+
2
+ /**
3
+ * @copyright 2021-present, Tencent, Inc. All rights reserved.
4
+ * @author Davislu <davislu@tencent.com>
5
+ * @brief navigator provides some function to navigate pages in miniprogram.
6
+ *
7
+ */
8
+
9
+ /**
10
+ * DEFN 空方法
11
+ * @private
12
+ * @returns {undefined} 无返回值
13
+ */
14
+ const DEFN = () => {};
15
+
16
+ /**
17
+ * navigateToWebview 方法 跳转到小程序的 web-view 容器打开 H5
18
+ * @param {object} setting 配置信息
19
+ * @param {string} setting.url 需要跳转的 H5 连接
20
+ * @param {function} setting.complete 跳转成功后的回调函数
21
+ * @param {function} setting.message 用于获取 H5 中的 postMessage 的数据
22
+ * @param {object} setting.share 页面分享信息
23
+ * @param {string} setting.share.title 页面分享标题
24
+ * @param {string} setting.share.image 页面分享图片
25
+ * @param {string} setting.share.disable 是否禁用页面分享
26
+ * @param {object} setting.navbar 页面导航栏设置
27
+ * @param {string} setting.navbar.frontColor 导航栏字体颜色
28
+ * @param {string} setting.navbar.backgroundColor 导航栏背景颜色
29
+ * @returns {undefined} 无返回值
30
+ */
31
+ const navigateToWebview = ({ url: webUrl, complete = DEFN, message = DEFN, share = {}, navbar = {} }) => {
32
+ const page = '/modules/x/webcontainer/webcontainer';
33
+ let query = `url=${encodeURIComponent(webUrl)}`;
34
+ if (share.disable) {
35
+ query += '&disableShare=true';
36
+ } else if (share.title) {
37
+ const image = share.image ? `&image=${encodeURIComponent(share.image)}` : '';
38
+ query += `&title=${encodeURIComponent(share.title)}${image}`;
39
+ }
40
+ if (navbar.frontColor) query += `&navbarFront=${navbar.frontColor}`;
41
+ if (navbar.backgroundColor) query += `&navbarBg=${navbar.backgroundColor}`;
42
+
43
+ const url = `${page}${/\?/.test(page) ? '&' : '?'}${query}`;
44
+ const navSetting = { url, complete };
45
+ navSetting.events = { onMessage: message };
46
+ wx.navigateTo(navSetting);
47
+ };
48
+
49
+ const obj = {
50
+ navigateToWebview,
51
+ };
52
+ export default obj;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Tencent Inc. All Rights Reserved.
3
+ * Description: Some Functions for Obejct.
4
+ */
5
+
6
+ /**
7
+ * @function
8
+ * @description 把对象拼接成 a=b&c=d 形式的字符串
9
+ * @param {Object} queryObj 需要进行序列化的对象
10
+ * @returns {String} 拼接后的字符串
11
+ */
12
+ const serialize = (queryObj = {}) => {
13
+ if (!queryObj) {
14
+ return '';
15
+ }
16
+
17
+ const queryArray = [];
18
+
19
+ Object.keys(queryObj).forEach((key) => {
20
+ queryArray.push(`${key}=${queryObj[key]}`);
21
+ });
22
+
23
+ return queryArray.join('&');
24
+ };
25
+
26
+ export { serialize };