rexma-design 0.0.1

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/changelog.md ADDED
File without changes
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default as XinhuaClient } from './utils/XinhuaClient/index';
package/lib/md5.js ADDED
@@ -0,0 +1,370 @@
1
+ /*
2
+ * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
3
+ * Digest Algorithm, as defined in RFC 1321.
4
+ * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
5
+ * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
6
+ * Distributed under the BSD License
7
+ * See http://pajhome.org.uk/crypt/md5 for more info.
8
+ */
9
+
10
+ /*
11
+ * Configurable variables. You may need to tweak these to be compatible with
12
+ * the server-side, but the defaults work in most cases.
13
+ */
14
+ var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
15
+ var b64pad = ''; /* base-64 pad character. "=" for strict RFC compliance */
16
+
17
+ /*
18
+ * These are the functions you'll usually want to call
19
+ * They take string arguments and return either hex or base-64 encoded strings
20
+ */
21
+ function hex_md5(s) {
22
+ return rstr2hex(rstr_md5(str2rstr_utf8(s)));
23
+ }
24
+ function b64_md5(s) {
25
+ return rstr2b64(rstr_md5(str2rstr_utf8(s)));
26
+ }
27
+ function any_md5(s, e) {
28
+ return rstr2any(rstr_md5(str2rstr_utf8(s)), e);
29
+ }
30
+ function hex_hmac_md5(k, d) {
31
+ return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
32
+ }
33
+ function b64_hmac_md5(k, d) {
34
+ return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)));
35
+ }
36
+ function any_hmac_md5(k, d, e) {
37
+ return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e);
38
+ }
39
+
40
+ /*
41
+ * Perform a simple self-test to see if the VM is working
42
+ */
43
+ function md5_vm_test() {
44
+ return hex_md5('abc').toLowerCase() == '900150983cd24fb0d6963f7d28e17f72';
45
+ }
46
+
47
+ /*
48
+ * Calculate the MD5 of a raw string
49
+ */
50
+ function rstr_md5(s) {
51
+ return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
52
+ }
53
+
54
+ /*
55
+ * Calculate the HMAC-MD5, of a key and some data (raw strings)
56
+ */
57
+ function rstr_hmac_md5(key, data) {
58
+ var bkey = rstr2binl(key);
59
+ if (bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);
60
+
61
+ var ipad = Array(16),
62
+ opad = Array(16);
63
+ for (var i = 0; i < 16; i++) {
64
+ ipad[i] = bkey[i] ^ 0x36363636;
65
+ opad[i] = bkey[i] ^ 0x5c5c5c5c;
66
+ }
67
+
68
+ var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
69
+ return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
70
+ }
71
+
72
+ /*
73
+ * Convert a raw string to a hex string
74
+ */
75
+ function rstr2hex(input) {
76
+ try {
77
+ hexcase;
78
+ } catch (e) {
79
+ hexcase = 0;
80
+ }
81
+ var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef';
82
+ var output = '';
83
+ var x;
84
+ for (var i = 0; i < input.length; i++) {
85
+ x = input.charCodeAt(i);
86
+ output += hex_tab.charAt((x >>> 4) & 0x0f) + hex_tab.charAt(x & 0x0f);
87
+ }
88
+ return output;
89
+ }
90
+
91
+ /*
92
+ * Convert a raw string to a base-64 string
93
+ */
94
+ function rstr2b64(input) {
95
+ try {
96
+ b64pad;
97
+ } catch (e) {
98
+ b64pad = '';
99
+ }
100
+ var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
101
+ var output = '';
102
+ var len = input.length;
103
+ for (var i = 0; i < len; i += 3) {
104
+ var triplet =
105
+ (input.charCodeAt(i) << 16) |
106
+ (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) |
107
+ (i + 2 < len ? input.charCodeAt(i + 2) : 0);
108
+ for (var j = 0; j < 4; j++) {
109
+ if (i * 8 + j * 6 > input.length * 8) output += b64pad;
110
+ else output += tab.charAt((triplet >>> (6 * (3 - j))) & 0x3f);
111
+ }
112
+ }
113
+ return output;
114
+ }
115
+
116
+ /*
117
+ * Convert a raw string to an arbitrary string encoding
118
+ */
119
+ function rstr2any(input, encoding) {
120
+ var divisor = encoding.length;
121
+ var i, j, q, x, quotient;
122
+
123
+ /* Convert to an array of 16-bit big-endian values, forming the dividend */
124
+ var dividend = Array(Math.ceil(input.length / 2));
125
+ for (i = 0; i < dividend.length; i++) {
126
+ dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
127
+ }
128
+
129
+ /*
130
+ * Repeatedly perform a long division. The binary array forms the dividend,
131
+ * the length of the encoding is the divisor. Once computed, the quotient
132
+ * forms the dividend for the next step. All remainders are stored for later
133
+ * use.
134
+ */
135
+ var full_length = Math.ceil((input.length * 8) / (Math.log(encoding.length) / Math.log(2)));
136
+ var remainders = Array(full_length);
137
+ for (j = 0; j < full_length; j++) {
138
+ quotient = Array();
139
+ x = 0;
140
+ for (i = 0; i < dividend.length; i++) {
141
+ x = (x << 16) + dividend[i];
142
+ q = Math.floor(x / divisor);
143
+ x -= q * divisor;
144
+ if (quotient.length > 0 || q > 0) quotient[quotient.length] = q;
145
+ }
146
+ remainders[j] = x;
147
+ dividend = quotient;
148
+ }
149
+
150
+ /* Convert the remainders to the output string */
151
+ var output = '';
152
+ for (i = remainders.length - 1; i >= 0; i--) output += encoding.charAt(remainders[i]);
153
+
154
+ return output;
155
+ }
156
+
157
+ /*
158
+ * Encode a string as utf-8.
159
+ * For efficiency, this assumes the input is valid utf-16.
160
+ */
161
+ function str2rstr_utf8(input) {
162
+ var output = '';
163
+ var i = -1;
164
+ var x, y;
165
+
166
+ while (++i < input.length) {
167
+ /* Decode utf-16 surrogate pairs */
168
+ x = input.charCodeAt(i);
169
+ y = i + 1 < input.length ? input.charCodeAt(i + 1) : 0;
170
+ if (0xd800 <= x && x <= 0xdbff && 0xdc00 <= y && y <= 0xdfff) {
171
+ x = 0x10000 + ((x & 0x03ff) << 10) + (y & 0x03ff);
172
+ i++;
173
+ }
174
+
175
+ /* Encode output as utf-8 */
176
+ if (x <= 0x7f) output += String.fromCharCode(x);
177
+ else if (x <= 0x7ff)
178
+ output += String.fromCharCode(0xc0 | ((x >>> 6) & 0x1f), 0x80 | (x & 0x3f));
179
+ else if (x <= 0xffff)
180
+ output += String.fromCharCode(
181
+ 0xe0 | ((x >>> 12) & 0x0f),
182
+ 0x80 | ((x >>> 6) & 0x3f),
183
+ 0x80 | (x & 0x3f),
184
+ );
185
+ else if (x <= 0x1fffff)
186
+ output += String.fromCharCode(
187
+ 0xf0 | ((x >>> 18) & 0x07),
188
+ 0x80 | ((x >>> 12) & 0x3f),
189
+ 0x80 | ((x >>> 6) & 0x3f),
190
+ 0x80 | (x & 0x3f),
191
+ );
192
+ }
193
+ return output;
194
+ }
195
+
196
+ /*
197
+ * Encode a string as utf-16
198
+ */
199
+ function str2rstr_utf16le(input) {
200
+ var output = '';
201
+ for (var i = 0; i < input.length; i++)
202
+ output += String.fromCharCode(input.charCodeAt(i) & 0xff, (input.charCodeAt(i) >>> 8) & 0xff);
203
+ return output;
204
+ }
205
+
206
+ function str2rstr_utf16be(input) {
207
+ var output = '';
208
+ for (var i = 0; i < input.length; i++)
209
+ output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xff, input.charCodeAt(i) & 0xff);
210
+ return output;
211
+ }
212
+
213
+ /*
214
+ * Convert a raw string to an array of little-endian words
215
+ * Characters >255 have their high-byte silently ignored.
216
+ */
217
+ function rstr2binl(input) {
218
+ var output = Array(input.length >> 2);
219
+ for (var i = 0; i < output.length; i++) output[i] = 0;
220
+ for (var i = 0; i < input.length * 8; i += 8)
221
+ output[i >> 5] |= (input.charCodeAt(i / 8) & 0xff) << i % 32;
222
+ return output;
223
+ }
224
+
225
+ /*
226
+ * Convert an array of little-endian words to a string
227
+ */
228
+ function binl2rstr(input) {
229
+ var output = '';
230
+ for (var i = 0; i < input.length * 32; i += 8)
231
+ output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff);
232
+ return output;
233
+ }
234
+
235
+ /*
236
+ * Calculate the MD5 of an array of little-endian words, and a bit length.
237
+ */
238
+ function binl_md5(x, len) {
239
+ /* append padding */
240
+ x[len >> 5] |= 0x80 << len % 32;
241
+ x[(((len + 64) >>> 9) << 4) + 14] = len;
242
+
243
+ var a = 1732584193;
244
+ var b = -271733879;
245
+ var c = -1732584194;
246
+ var d = 271733878;
247
+
248
+ for (var i = 0; i < x.length; i += 16) {
249
+ var olda = a;
250
+ var oldb = b;
251
+ var oldc = c;
252
+ var oldd = d;
253
+
254
+ a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
255
+ d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
256
+ c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
257
+ b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
258
+ a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
259
+ d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
260
+ c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
261
+ b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
262
+ a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
263
+ d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
264
+ c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
265
+ b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
266
+ a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
267
+ d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
268
+ c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
269
+ b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
270
+
271
+ a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
272
+ d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
273
+ c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
274
+ b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
275
+ a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
276
+ d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
277
+ c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
278
+ b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
279
+ a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
280
+ d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
281
+ c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
282
+ b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
283
+ a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
284
+ d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
285
+ c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
286
+ b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
287
+
288
+ a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
289
+ d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
290
+ c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
291
+ b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
292
+ a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
293
+ d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
294
+ c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
295
+ b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
296
+ a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
297
+ d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
298
+ c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
299
+ b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
300
+ a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
301
+ d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
302
+ c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
303
+ b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
304
+
305
+ a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
306
+ d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
307
+ c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
308
+ b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
309
+ a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
310
+ d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
311
+ c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
312
+ b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
313
+ a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
314
+ d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
315
+ c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
316
+ b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
317
+ a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
318
+ d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
319
+ c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
320
+ b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
321
+
322
+ a = safe_add(a, olda);
323
+ b = safe_add(b, oldb);
324
+ c = safe_add(c, oldc);
325
+ d = safe_add(d, oldd);
326
+ }
327
+ return Array(a, b, c, d);
328
+ }
329
+
330
+ /*
331
+ * These functions implement the four basic operations the algorithm uses.
332
+ */
333
+ function md5_cmn(q, a, b, x, s, t) {
334
+ return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
335
+ }
336
+ function md5_ff(a, b, c, d, x, s, t) {
337
+ return md5_cmn((b & c) | (~b & d), a, b, x, s, t);
338
+ }
339
+ function md5_gg(a, b, c, d, x, s, t) {
340
+ return md5_cmn((b & d) | (c & ~d), a, b, x, s, t);
341
+ }
342
+ function md5_hh(a, b, c, d, x, s, t) {
343
+ return md5_cmn(b ^ c ^ d, a, b, x, s, t);
344
+ }
345
+ function md5_ii(a, b, c, d, x, s, t) {
346
+ return md5_cmn(c ^ (b | ~d), a, b, x, s, t);
347
+ }
348
+
349
+ /*
350
+ * Add integers, wrapping at 2^32. This uses 16-bit operations internally
351
+ * to work around bugs in some JS interpreters.
352
+ */
353
+ function safe_add(x, y) {
354
+ var lsw = (x & 0xffff) + (y & 0xffff);
355
+ var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
356
+ return (msw << 16) | (lsw & 0xffff);
357
+ }
358
+
359
+ /*
360
+ * Bitwise rotate a 32-bit number to the left.
361
+ */
362
+ function bit_rol(num, cnt) {
363
+ return (num << cnt) | (num >>> (32 - cnt));
364
+ }
365
+
366
+ export default {
367
+ md5: function (str) {
368
+ return hex_md5(str);
369
+ },
370
+ };
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "id": "rexma-design",
3
+ "name": "rexma-design",
4
+ "displayName": "rexma-design",
5
+ "version": "0.0.1",
6
+ "description": "rexma-design",
7
+ "keywords": [
8
+ "rexma-design"
9
+ ],
10
+ "repository": "",
11
+ "engines": {
12
+ "HBuilderX": "^3.1.0"
13
+ },
14
+ "dcloudext": {
15
+ "type": "component-vue",
16
+ "sale": {
17
+ "regular": {
18
+ "price": "0.00"
19
+ },
20
+ "sourcecode": {
21
+ "price": "0.00"
22
+ }
23
+ },
24
+ "contact": {
25
+ "qq": ""
26
+ },
27
+ "declaration": {
28
+ "ads": "",
29
+ "data": "",
30
+ "permissions": ""
31
+ },
32
+ "npmurl": ""
33
+ },
34
+ "uni_modules": {
35
+ "dependencies": [],
36
+ "encrypt": [],
37
+ "platforms": {
38
+ "cloud": {
39
+ "tcb": "u",
40
+ "aliyun": "u",
41
+ "alipay": "u"
42
+ },
43
+ "client": {
44
+ "Vue": {
45
+ "vue2": "u",
46
+ "vue3": "u"
47
+ },
48
+ "App": {
49
+ "app-vue": "u",
50
+ "app-nvue": "u",
51
+ "app-uvue": "u"
52
+ },
53
+ "H5-mobile": {
54
+ "Safari": "u",
55
+ "Android Browser": "u",
56
+ "微信浏览器(Android)": "u",
57
+ "QQ浏览器(Android)": "u"
58
+ },
59
+ "H5-pc": {
60
+ "Chrome": "u",
61
+ "IE": "u",
62
+ "Edge": "u",
63
+ "Firefox": "u",
64
+ "Safari": "u"
65
+ },
66
+ "小程序": {
67
+ "微信": "u",
68
+ "阿里": "u",
69
+ "百度": "u",
70
+ "字节跳动": "u",
71
+ "QQ": "u",
72
+ "钉钉": "u",
73
+ "快手": "u",
74
+ "飞书": "u",
75
+ "京东": "u"
76
+ },
77
+ "快应用": {
78
+ "华为": "u",
79
+ "联盟": "u"
80
+ }
81
+ }
82
+ }
83
+ },
84
+ "peerDependencies": {
85
+ "rexma-cli": "*"
86
+ },
87
+ "dependencies": {
88
+ "crypto-js": "^4.2.0"
89
+ }
90
+ }
package/readme.md ADDED
@@ -0,0 +1 @@
1
+ # rexma-design
@@ -0,0 +1,69 @@
1
+ import type { IXinhuaClientState } from './XinhuaClient/declaration';
2
+
3
+ import { enc, mode, AES, pad } from 'crypto-js';
4
+ import xh from './xh';
5
+
6
+ type IGetClientSignParams = {
7
+ key: string;
8
+ offset: string;
9
+ moduleCode?: string;
10
+ developmentClientSign?: IXinhuaClientState['clientSign'];
11
+ };
12
+
13
+ class ClientSign {
14
+ /**
15
+ * ASE 解密
16
+ * @description 使用加密秘钥,对 需要解密的参数 进行解密
17
+ * @param {string} content - 需要解密的参数
18
+ * @param {string} params.key - 加密密钥(长度必须是 16 的整数倍)
19
+ * @param {string} params.offset - 偏移量
20
+ */
21
+ static aesDecryptParams(content, { key, offset }) {
22
+ if (!key || !offset) {
23
+ throw new Error('缺少加密相关配置');
24
+ }
25
+
26
+ // 密钥 - 从 UTF-8 编码 解析出原始字符串
27
+ const keyUTF8 = enc.Utf8.parse(key);
28
+ // 偏移量(在此公司内是固定的) - 从 UTF-8编码 解析出原始字符串
29
+ const offsetUTF8 = enc.Utf8.parse(offset);
30
+
31
+ const bytes = AES.decrypt(content, keyUTF8, {
32
+ iv: offsetUTF8,
33
+ mode: mode.CBC,
34
+ padding: pad.Pkcs7,
35
+ });
36
+
37
+ return bytes.toString(enc.Utf8);
38
+ }
39
+
40
+ /**
41
+ * D@description 获取客户端签名
42
+ */
43
+ static async getClientSign(options: IGetClientSignParams) {
44
+ const { developmentClientSign, key, offset, moduleCode } = options;
45
+ // @ts-ignore
46
+ if (process?.env?.NODE_ENV === 'development' && developmentClientSign) {
47
+ return developmentClientSign;
48
+ }
49
+ if (!xh.isMediaConvergenceXinhuaApp()) {
50
+ return {
51
+ sign: '',
52
+ subDomain: '',
53
+ };
54
+ }
55
+ const clientSignInfo: any = await new Promise((resolve) => {
56
+ xh.getClientSign({
57
+ moduleCode,
58
+ success: (res) => resolve(res),
59
+ });
60
+ });
61
+ return {
62
+ sign: this.aesDecryptParams(clientSignInfo.cs, { key, offset }),
63
+ subDomain: clientSignInfo.subDomain,
64
+ siteId: clientSignInfo.siteId,
65
+ };
66
+ }
67
+ }
68
+
69
+ export default ClientSign;
@@ -0,0 +1,25 @@
1
+ export type IXinhuaClientState = {
2
+ /** 站点 id */
3
+ siteId: string;
4
+ /** 用户 id */
5
+ userId: string;
6
+ /** 状态栏高度 */
7
+ statusBarHeight: number;
8
+ /** 签名信息 */
9
+ clientSign: {
10
+ sign: string;
11
+ subDomain: string;
12
+ };
13
+ };
14
+
15
+ /**
16
+ * @description 初始化方法参数
17
+ */
18
+ export type IXinhuaClientInitOptions = {
19
+ clientSign: {
20
+ key: string;
21
+ offset: string;
22
+ moduleCode?: string;
23
+ };
24
+ developmentState?: IXinhuaClientState;
25
+ };
@@ -0,0 +1,90 @@
1
+ import type { IXinhuaClientInitOptions, IXinhuaClientState } from './declaration';
2
+
3
+ import getValueFromUA from '../getValueFromUA';
4
+ import md5Libs from '../../lib/md5';
5
+ import ClientSign from '../ClientSign';
6
+ import xh from '../xh';
7
+
8
+ class XinhuaClient {
9
+ /** 数据初始化是否完成 */
10
+ private static initStatus: boolean = false;
11
+ /** 站点 id */
12
+ private static state: IXinhuaClientState = {
13
+ siteId: '',
14
+ userId: '',
15
+ statusBarHeight: 0,
16
+ clientSign: {
17
+ sign: '',
18
+ subDomain: '',
19
+ },
20
+ };
21
+ /** 初始化信息 */
22
+ private static initOptions: IXinhuaClientInitOptions;
23
+
24
+ static init(options: IXinhuaClientInitOptions) {
25
+ this.initOptions = options;
26
+ }
27
+
28
+ private static async stateInit() {
29
+ const initOptions = this.initOptions;
30
+ // @ts-ignore
31
+ if (process?.env?.NODE_ENV === 'development' && initOptions.developmentState) {
32
+ this.state = {
33
+ ...this.state,
34
+ ...initOptions.developmentState,
35
+ };
36
+ }
37
+ if (xh.isMediaConvergenceXinhuaApp()) {
38
+ // 客户端环境
39
+ this.state.siteId = getValueFromUA('currentSiteId');
40
+ this.state.userId = getValueFromUA('userId');
41
+ const numberStatusBarHeight = Number(getValueFromUA('statusBarHeight'));
42
+ this.state.statusBarHeight = isNaN(numberStatusBarHeight) ? 0 : numberStatusBarHeight;
43
+ this.state.clientSign = await ClientSign.getClientSign({
44
+ key: initOptions.clientSign.key,
45
+ offset: initOptions.clientSign.offset,
46
+ moduleCode: initOptions.clientSign.moduleCode,
47
+ });
48
+ }
49
+ this.initStatus = true;
50
+ }
51
+
52
+ /**
53
+ * @description 使用数据状态
54
+ */
55
+ static async useState() {
56
+ if (!this.initStatus) {
57
+ await this.stateInit();
58
+ }
59
+ return this.state;
60
+ }
61
+
62
+ /**
63
+ * @@description 生成签名参数
64
+ */
65
+ static async getSignRequestParams(params: Record<string, any>) {
66
+ if (!this.initStatus) {
67
+ await this.stateInit();
68
+ }
69
+ const requestParams = { ...params },
70
+ initialState = this.state;
71
+ if (initialState.userId) {
72
+ requestParams.userId = initialState.userId;
73
+ }
74
+ if (initialState.siteId) {
75
+ requestParams.siteId = initialState.siteId;
76
+ }
77
+ requestParams.currentTimeMillis = new Date().getTime();
78
+ if (initialState.clientSign && initialState.clientSign.sign) {
79
+ const paramsArr = Object.values(requestParams);
80
+ paramsArr.push(initialState.clientSign.sign);
81
+ paramsArr.sort();
82
+ requestParams.signature = md5Libs.md5(paramsArr.join(''));
83
+ }
84
+ return requestParams;
85
+ }
86
+
87
+ private constructor() {}
88
+ }
89
+
90
+ export default XinhuaClient;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * @description 从 ua 中根据 key 获取信息
3
+ */
4
+ function getValueFromUA(name: string) {
5
+ // #ifdef H5
6
+ const userAgent = window.navigator.userAgent;
7
+ const userAgentArr = userAgent.split(' ');
8
+ let uaValue = '';
9
+ if (userAgentArr) {
10
+ for (let index in userAgentArr) {
11
+ const item = userAgentArr[index];
12
+ if (item.startsWith(name + '/')) {
13
+ uaValue = item.split('/')[1];
14
+ break;
15
+ }
16
+ }
17
+ }
18
+ return uaValue;
19
+ // #endif
20
+
21
+ // #ifndef H5
22
+ return '';
23
+ // #endif
24
+ }
25
+
26
+ export default getValueFromUA;
package/utils/xh.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { XHDeclaration } from 'rexma-cli/lib/declaration/xinhua-sdk';
2
+
3
+ import xhImport from 'rexma-cli/dist/xinhua-sdk';
4
+
5
+ const xh = xhImport as XHDeclaration;
6
+
7
+ export default xh;