3
3
* @since 2022/2/11
4
4
*/
5
5
import * as forge from 'node-forge'
6
- import axios from 'axios'
7
- import { API_BASE_URL , BASE_URL } from '../config'
8
- import { IAuthResult , IIframeOptions } from '../interface/provider'
6
+ import { BASE_URL } from '../config'
7
+ import { IAuthResult , IIframeData , IIframeOptions } from '../interface/provider'
9
8
import { Provider } from '../provider'
10
9
11
10
export const getFrameWidth = ( ) => {
@@ -68,10 +67,38 @@ const setBodyScrollable = () => {
68
67
} )
69
68
}
70
69
70
+ export const isObject = ( obj : unknown ) => {
71
+ return Object . prototype . toString . call ( obj ) === '[object Object]'
72
+ }
73
+
71
74
const closeIframe = ( root : HTMLDivElement ) => {
72
75
setBodyScrollable ( )
73
76
root . style . display = 'none'
74
77
}
78
+ export const sendMessageToApp = ( {
79
+ data,
80
+ type,
81
+ success = true ,
82
+ } : IIframeData ) => {
83
+ const iframe : HTMLIFrameElement | null = document . getElementById (
84
+ 'anyweb-iframe'
85
+ ) as HTMLIFrameElement | null
86
+ if ( ! iframe ) {
87
+ return
88
+ }
89
+ iframe . contentWindow &&
90
+ iframe . contentWindow . postMessage (
91
+ {
92
+ data : {
93
+ data,
94
+ type,
95
+ success,
96
+ } ,
97
+ type : 'anyweb' ,
98
+ } ,
99
+ '*'
100
+ )
101
+ }
75
102
76
103
export const getIframe = async (
77
104
url : string ,
@@ -82,8 +109,13 @@ export const getIframe = async (
82
109
document . getElementById ( 'anyweb-iframe' )
83
110
) {
84
111
const mask = document . getElementById ( 'anyweb-iframe-mask' ) as HTMLDivElement
85
- const iframe = document . getElementById ( 'anyweb-iframe' ) as HTMLIFrameElement
86
- iframe . setAttribute ( 'src' , url )
112
+ sendMessageToApp ( {
113
+ type : 'router' ,
114
+ data : {
115
+ path : `/${ url } ` ,
116
+ mode : 'redirectTo' ,
117
+ } ,
118
+ } )
87
119
mask . style . display = 'block'
88
120
setBodyNonScrollable ( )
89
121
return ( ) => {
@@ -168,7 +200,7 @@ export const getIframe = async (
168
200
iframe . id = 'anyweb-iframe'
169
201
mask . id = 'anyweb-iframe-mask'
170
202
171
- iframe . setAttribute ( 'src' , url )
203
+ iframe . setAttribute ( 'src' , ` ${ BASE_URL } ${ url } ` )
172
204
iframe . setAttribute ( 'frameborder' , '0' )
173
205
iframe . setAttribute ( 'scrolling' , 'no' )
174
206
@@ -200,25 +232,11 @@ export const callIframe = async (
200
232
waitResult = true ,
201
233
} : IIframeOptions
202
234
) => {
203
- let serialNumber = ''
204
- const hash = sha512 ( JSON . stringify ( { appId, params } ) )
205
-
206
- try {
207
- serialNumber = (
208
- await axios . post ( `${ API_BASE_URL } /open/serial/create` , {
209
- hash : hash ,
210
- } )
211
- ) . data . data . serialNumber
212
- } catch ( e ) {
213
- console . error ( 'Get serialNumber error' , e )
214
- throw new Error ( 'Get serialNumber error' )
215
- }
216
-
217
235
if ( waitResult ) {
218
236
return new Promise < unknown > ( async ( resolve , reject ) => {
219
- let timer : NodeJS . Timeout | undefined = undefined
237
+ let callback : IIframeData | undefined = undefined
220
238
const close = await getIframe (
221
- `${ BASE_URL } ${ path } ?appId=${ appId } &authType=${ authType } &serialNumber= ${ serialNumber } &hash= ${ hash } &random=${ Math . floor (
239
+ `${ path } ?appId=${ appId } &authType=${ authType } &random=${ Math . floor (
222
240
Math . random ( ) * 1000
223
241
) } &chainId=${ chainId } ¶ms=${ params } &scope=${ JSON . stringify ( scope ) } `,
224
242
( ) => {
@@ -227,48 +245,54 @@ export const callIframe = async (
227
245
}
228
246
}
229
247
)
230
- const delay = 1000
231
- const next = ( i : number ) => {
232
- timer = setTimeout ( async ( ) => {
233
- let data
234
- try {
235
- data = (
236
- await axios . post ( `${ API_BASE_URL } /open/serial/read` , {
237
- serialNumber : serialNumber ,
238
- hash : hash ,
239
- } )
240
- ) . data . data
241
- } catch ( e ) {
242
- console . error ( "Can't get result from iframe" , e )
243
- next ( i ++ )
244
- return
245
- // reject(new Error("Can't get result from iframe"))
246
- }
247
- if ( data && data !== 'false' && data !== false ) {
248
- timer && clearTimeout ( timer )
249
- close ( )
250
- resolve ( JSON . parse ( data ) )
251
- } else {
252
- if ( i * delay > 10 * 60 * 1000 ) {
253
- close ( )
254
- reject ( new Error ( 'Timeout' ) )
248
+ const timer = setTimeout ( ( ) => {
249
+ close ( )
250
+ reject ( new Error ( 'Timeout' ) )
251
+ } , 10 * 60 * 1000 )
252
+
253
+ // Set Listeners
254
+ window . addEventListener (
255
+ 'message' ,
256
+ function receiveMessageFromIframePage ( event ) {
257
+ if (
258
+ event . data &&
259
+ isObject ( event . data ) &&
260
+ 'type' in event . data &&
261
+ event . data . type === 'anyweb'
262
+ ) {
263
+ console . log ( 'SDK收到子页面信息: ' , event . data )
264
+ callback = event . data . data as IIframeData
265
+
266
+ if ( callback . type === 'callback' ) {
267
+ window . removeEventListener (
268
+ 'message' ,
269
+ receiveMessageFromIframePage
270
+ )
271
+ clearTimeout ( timer )
272
+ if ( callback . success ) {
273
+ close ( )
274
+ resolve ( callback . data )
275
+ } else {
276
+ close ( )
277
+ reject ( new Error ( callback . data as string ) )
278
+ }
255
279
}
256
- next ( i ++ )
257
280
}
258
- } , delay )
259
- }
260
- next ( 0 )
281
+ } ,
282
+ false
283
+ )
261
284
} )
285
+ } else {
286
+ await getIframe (
287
+ `${ path } ?appId=${ appId } &authType=${ authType } &random=${ Math . floor (
288
+ Math . random ( ) * 1000
289
+ ) } &chainId=${ chainId } ¶ms=${ params } &scope=${ JSON . stringify ( scope ) } `,
290
+ ( ) => {
291
+ return
292
+ }
293
+ )
294
+ return 'ok'
262
295
}
263
- await getIframe (
264
- `${ BASE_URL } ${ path } ?appId=${ appId } &authType=${ authType } &serialNumber=${ serialNumber } &hash=${ hash } &random=${ Math . floor (
265
- Math . random ( ) * 1000
266
- ) } &chainId=${ chainId } ¶ms=${ params } &scope=${ JSON . stringify ( scope ) } `,
267
- ( ) => {
268
- return
269
- }
270
- )
271
- return 'ok'
272
296
}
273
297
274
298
export const readCache = ( provider : Provider ) => {
0 commit comments