-
Notifications
You must be signed in to change notification settings - Fork 6
/
zfjs.js
501 lines (451 loc) · 16.5 KB
/
zfjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
(function () {
//--- util
var __DL = 0, __IL = 1, __EL = 2, __LogLvl = parseInt("${_debugLevel}") || __DL
var __LogArr = ["start"]
function __Log(lvl) {
__LogArr.push(arguments)
if (lvl >= __LogLvl) {
window.console && window.console.log.apply(console, Array.prototype.slice.call(arguments, 1))
}
}
var CryptoJS = CryptoJS || function (e, m) {
var p = {},
j = p.lib = {},
l = function () { },
f = j.Base = {
extend: function (a) {
l.prototype = this
var c = new l
a && c.mixIn(a)
c.hasOwnProperty("init") || (c.init = function () {
c.$super.init.apply(this, arguments)
})
c.init.prototype = c
c.$super = this
return c
},
create: function () {
var a = this.extend()
a.init.apply(a, arguments)
return a
},
init: function () { },
mixIn: function (a) {
for (var c in a) a.hasOwnProperty(c) && (this[c] = a[c])
a.hasOwnProperty("toString") && (this.toString = a.toString)
},
clone: function () {
return this.init.prototype.extend(this)
}
},
n = j.WordArray = f.extend({
init: function (a, c) {
a = this.words = a || []
this.sigBytes = c != m ? c : 4 * a.length
},
toString: function (a) {
return (a || h).stringify(this)
},
concat: function (a) {
var c = this.words,
q = a.words,
d = this.sigBytes
a = a.sigBytes
this.clamp()
if (d % 4)
for (var b = 0; b < a; b++) c[d + b >>> 2] |= (q[b >>> 2] >>> 24 - 8 * (b % 4) & 255) << 24 - 8 * ((d + b) % 4)
else if (65535 < q.length)
for (b = 0; b < a; b += 4) c[d + b >>> 2] = q[b >>> 2]
else c.push.apply(c, q)
this.sigBytes += a
return this
},
clamp: function () {
var a = this.words,
c = this.sigBytes
a[c >>> 2] &= 4294967295 << 32 - 8 * (c % 4)
a.length = e.ceil(c / 4)
},
clone: function () {
var a = f.clone.call(this)
a.words = this.words.slice(0)
return a
},
random: function (a) {
for (var c = [], b = 0; b < a; b += 4) c.push(4294967296 * e.random() | 0)
return new n.init(c, a)
}
}),
b = p.enc = {},
h = b.Hex = {
stringify: function (a) {
var c = a.words
a = a.sigBytes
for (var b = [], d = 0; d < a; d++) {
var f = c[d >>> 2] >>> 24 - 8 * (d % 4) & 255
b.push((f >>> 4).toString(16))
b.push((f & 15).toString(16))
}
return b.join("")
},
parse: function (a) {
for (var c = a.length,
b = [], d = 0; d < c; d += 2) b[d >>> 3] |= parseInt(a.substr(d, 2), 16) << 24 - 4 * (d % 8)
return new n.init(b, c / 2)
}
},
g = b.Latin1 = {
stringify: function (a) {
var c = a.words
a = a.sigBytes
for (var b = [], d = 0; d < a; d++) b.push(String.fromCharCode(c[d >>> 2] >>> 24 - 8 * (d % 4) & 255))
return b.join("")
},
parse: function (a) {
for (var c = a.length,
b = [], d = 0; d < c; d++) b[d >>> 2] |= (a.charCodeAt(d) & 255) << 24 - 8 * (d % 4)
return new n.init(b, c)
}
},
r = b.Utf8 = {
stringify: function (a) {
try {
return decodeURIComponent(escape(g.stringify(a)))
} catch (c) {
throw Error("Malformed UTF-8 data")
}
},
parse: function (a) {
return g.parse(unescape(encodeURIComponent(a)))
}
},
k = j.BufferedBlockAlgorithm = f.extend({
reset: function () {
this._data = new n.init
this._nDataBytes = 0
},
_append: function (a) {
"string" == typeof a && (a = r.parse(a))
this._data.concat(a)
this._nDataBytes += a.sigBytes
},
_process: function (a) {
var c = this._data,
b = c.words,
d = c.sigBytes,
f = this.blockSize,
ht = d / (4 * f),
h = a ? e.ceil(ht) : e.max((ht | 0) - this._minBufferSize, 0)
a = h * f
d = e.min(4 * a, d)
if (a) {
for (var g = 0; g < a; g += f) this._doProcessBlock(b, g)
g = b.splice(0, a)
c.sigBytes -= d
}
return new n.init(g, d)
},
clone: function () {
var a = f.clone.call(this)
a._data = this._data.clone()
return a
},
_minBufferSize: 0
})
j.Hasher = k.extend({
cfg: f.extend(),
init: function (a) {
this.cfg = this.cfg.extend(a)
this.reset()
},
reset: function () {
k.reset.call(this)
this._doReset()
},
update: function (a) {
this._append(a)
this._process()
return this
},
finalize: function (a) {
a && this._append(a)
return this._doFinalize()
},
blockSize: 16,
_createHelper: function (a) {
return function (c, b) {
return (new a.init(b)).finalize(c)
}
},
_createHmacHelper: function (a) {
return function (b, f) {
return (new s.HMAC.init(a, f)).finalize(b)
}
}
})
var s = p.algo = {}
return p
}(Math);
(function () {
var e = CryptoJS,
mt = e.lib,
p = mt.WordArray,
j = mt.Hasher,
l = [],
m = e.algo.SHA1 = j.extend({
_doReset: function () {
this._hash = new p.init([1732584193, 4023233417, 2562383102, 271733878, 3285377520])
},
_doProcessBlock: function (f, n) {
for (var b = this._hash.words,
h = b[0], g = b[1], e = b[2], k = b[3], j = b[4], a = 0; 80 > a; a++) {
if (16 > a) l[a] = f[n + a] | 0
else {
var c = l[a - 3] ^ l[a - 8] ^ l[a - 14] ^ l[a - 16]
l[a] = c << 1 | c >>> 31
}
c = (h << 5 | h >>> 27) + j + l[a]
c = 20 > a ? c + ((g & e | ~g & k) + 1518500249) : 40 > a ? c + ((g ^ e ^ k) + 1859775393) : 60 > a ? c + ((g & e | g & k | e & k) - 1894007588) : c + ((g ^ e ^ k) - 899497514)
j = k
k = e
e = g << 30 | g >>> 2
g = h
h = c
}
b[0] = b[0] + h | 0
b[1] = b[1] + g | 0
b[2] = b[2] + e | 0
b[3] = b[3] + k | 0
b[4] = b[4] + j | 0
},
_doFinalize: function () {
var f = this._data,
e = f.words,
b = 8 * this._nDataBytes,
h = 8 * f.sigBytes
e[h >>> 5] |= 128 << 24 - h % 32
e[(h + 64 >>> 9 << 4) + 14] = Math.floor(b / 4294967296)
e[(h + 64 >>> 9 << 4) + 15] = b
f.sigBytes = 4 * e.length
this._process()
return this._hash
},
clone: function () {
var e = j.clone.call(this)
e._hash = this._hash.clone()
return e
}
})
e.SHA1 = j._createHelper(m)
e.HmacSHA1 = j._createHmacHelper(m)
})()
var zfHelper = {
isIOS: function () {
let reg = /iphone|ipad|ipod/i
if (reg.test(window.navigator.userAgent)) { return true } else { return false }
},
b64DecodeUnicode: function (str) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(atob(str).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
}
//--- core
var __zf
if (zfHelper.isIOS()) {
if (window.webkit && window.webkit.messageHandlers) {
__zf = window.webkit.messageHandlers
}
if (!__zf) return "inject err: __zf not found"
delete window.webkit.messageHandlers
} else {
__zf = window.__zf
if (!__zf) return "inject err: __zf not found"
delete window.__zf
}
if (__DL >= __LogLvl)
__Log(__DL, "inject start")
var injectStartTime = Date.now()
var _callback_count = 1000,
_callback_map = {},
_MESSAGE_TYPE = 'msgType',
_CALLBACK_ID = 'callbackId',
_EVENT_NAME = 'eventName',
_event_map = {}
//客户端注入时替换
var _dgtVerifyRandomStr = "${_dgtVerifyRandomStr}"
var _JSON_MESSAGE = 'jsonMessage',
_SHA_KEY = 'shaKey',
_xxyy = _dgtVerifyRandomStr
if (__DL >= __LogLvl)
__Log(__DL, "__zf inject VerifyRandomStr:" + _xxyy)
//防止方法被替换
var _handleMessageIdentifier = _handleMessageFromZF;
var _callIdentifier = _call;
/**
* H5调用Native方法
*/
function _sendMessage(msg) {
var msgStr = JSON.stringify(msg)
var arr = new Array
arr[0] = msgStr
arr[1] = _xxyy
var str = arr.join("")
var msgSha = ''
var shaObj = CryptoJS.SHA1(str)
msgSha = shaObj.toString()
var retMap = {}
retMap[_JSON_MESSAGE] = msgStr
retMap[_SHA_KEY] = msgSha
msgStr = JSON.stringify(retMap)
_doSendMessage(msgStr)
}
function _doSendMessage(msgStr) {
if (__DL >= __LogLvl)
__Log(__DL, "_doSendMessage , msgStr : " + msgStr)
if (zfHelper.isIOS()) {
__zf._sendMessage.postMessage(msgStr)
} else {
__zf._sendMessage(msgStr)
}
}
/**
* Native调用
*/
function _handleMessageFromZF(msgStr) {
var curFuncIdentifier = ___zfJSBridge._handleMessageFromZF
if (curFuncIdentifier !== _handleMessageIdentifier) {
return '{}'
}
var msg = JSON.parse(msgStr)
var msgWrapStr = zfHelper.b64DecodeUnicode(msg[_JSON_MESSAGE])
var msgWrap = JSON.parse(msgWrapStr)
var shaStr = msg[_SHA_KEY]
var arr = new Array
arr[0] = msg[_JSON_MESSAGE]
arr[1] = _xxyy
var str = arr.join("")
var msgSha = ''
var shaObj = CryptoJS.SHA1(str)
msgSha = shaObj.toString()
if (__DL >= __LogLvl)
__Log(__DL, '_handleMessageFromZF , msgWrapStr : ' + msgWrapStr)
if (msgSha !== shaStr) {
if (__DL >= __LogLvl)
__Log(__DL, '_handleMessageFromZF shaStr not match, shaStr : ' + shaStr + ' , str : ' + str + ' , msgSha : ' + msgSha)
return '{}'
}
switch (msgWrap[_MESSAGE_TYPE]) {
case 'callback': {
if (typeof msgWrap[_CALLBACK_ID] === 'string' && typeof _callback_map[msgWrap[_CALLBACK_ID]] === 'function') {
var callBackRet = _callback_map[msgWrap[_CALLBACK_ID]](msgWrap['params'])
delete _callback_map[msgWrap[_CALLBACK_ID]];
var hasRet = callBackRet != undefined
callBackRet = callBackRet || {}
callBackRet['errCode'] = callBackRet['errCode'] || 0
var callBackRetStr = JSON.stringify(callBackRet)
if (__DL >= __LogLvl && hasRet)
__Log(__DL, "_handleMessageFromZF , callBackRetStr : " + callBackRetStr)
return callBackRetStr
}
return JSON.stringify({
'errCode': '404'
})
}
case 'event': {
if (typeof _event_map[msgWrap[_EVENT_NAME]] === 'function') {
var eventRet = _event_map[msgWrap[_EVENT_NAME]](msgWrap['params'])
var hasRet = callBackRet != undefined
eventRet = eventRet || {}
eventRet['errCode'] = eventRet['errCode'] || 0
var eventRetStr = JSON.stringify(eventRet)
if (__DL >= __LogLvl && hasRet)
__Log(__DL, "_handleMessageFromZF , eventRetStr : " + eventRetStr)
return eventRetStr
}
return JSON.stringify({
'errCode': '404'
})
}
}
return JSON.stringify({
'errCode': '404'
})
}
/**
* js-sdk调用bridge方法
* @param {String} apiName
* @param {Any} params
* @param {(result)->Unit?} callBack
*/
function _call(apiName, params, callback) {
var curFuncIdentifier = ___zfJSBridge.call
if (curFuncIdentifier !== _callIdentifier) {
return
}
if (!apiName || typeof apiName !== 'string') {
return
}
if (typeof params !== 'object') {
params = {}
}
var callbackID = (_callback_count++).toString()
if (typeof callback === 'function') {
_callback_map[callbackID] = callback
}
var msgObj = {
'apiName': apiName,
'params': params
}
msgObj[_MESSAGE_TYPE] = 'call'
msgObj[_CALLBACK_ID] = callbackID
try {
_sendMessage(msgObj)
} catch (e) {
__Log(__EL, "_call error", e)
}
}
function _on(eventName, callback) {
if (__DL >= __LogLvl)
__Log(__DL, "_on event ,eventName : " + eventName + " callback : " + callback)
if (!eventName || typeof eventName !== 'string') {
return
}
if (typeof callback !== 'function') {
return
}
_event_map[eventName] = callback
}
var ___zfJSBridge = {
// public
call: _call,
on: _on,
// log: _log,
// private
_hasInit: false
}
try {
Object.defineProperty(___zfJSBridge, '_handleMessageFromZF', {
value: _handleMessageFromZF,
writable: false,
configurable: false
});
} catch (e) {
__Log(__EL, "define _handleMessageFromZF", e)
return "zfJSBridge init err:" + e.message
}
try {
Object.defineProperty(window, 'zfJSBridge', {
value: ___zfJSBridge,
writable: false,
configurable: false
})
} catch (e) {
__Log(__EL, "define zfJSBridge", e)
return "inject err:" + e.message
}
if (__DL >= __LogLvl)
__Log(__IL, 'inject exec time', (Date.now() - injectStartTime))
return "inject success"
}())