@@ -64,7 +64,26 @@ const setBodyScrollable = () => {
6464export const getIframe = async (
6565 url : string ,
6666 onClose : ( ) => void
67- ) : Promise < HTMLDivElement > => {
67+ ) : Promise < {
68+ root : HTMLDivElement
69+ close : ( ) => void
70+ } > => {
71+ if (
72+ document . getElementById ( 'anyweb-iframe-mask' ) &&
73+ document . getElementById ( 'anyweb-iframe' )
74+ ) {
75+ const mask = document . getElementById ( 'anyweb-iframe-mask' ) as HTMLDivElement
76+ const iframe = document . getElementById ( 'anyweb-iframe' ) as HTMLIFrameElement
77+ iframe . setAttribute ( 'src' , url )
78+ mask . style . display = 'block'
79+ return {
80+ root : document . getElementById ( 'anyweb-iframe-mask' ) as HTMLDivElement ,
81+ close : ( ) => {
82+ onClose ( )
83+ mask . style . display = 'none'
84+ } ,
85+ }
86+ }
6887 const mask = document . createElement ( 'div' )
6988 const div = document . createElement ( 'div' )
7089 const iframe = document . createElement ( 'iframe' )
@@ -139,6 +158,7 @@ export const getIframe = async (
139158 button . className = 'iframe-contain-button'
140159 iframe . className = 'iframe'
141160
161+ iframe . id = 'anyweb-iframe'
142162 mask . id = 'anyweb-iframe-mask'
143163
144164 iframe . setAttribute ( 'src' , url )
@@ -151,13 +171,21 @@ export const getIframe = async (
151171 button . onclick = ( ) => {
152172 setBodyScrollable ( )
153173 onClose ( )
154- mask . remove ( )
174+ mask . style . display = 'none'
155175 }
156176 document . body . appendChild ( style )
157177 setBodyNonScrollable ( )
158178 document . body . insertBefore ( mask , document . body . firstElementChild )
159- return mask
179+ return {
180+ root : mask ,
181+ close : ( ) => {
182+ setBodyScrollable ( )
183+ onClose ( )
184+ mask . style . display = 'none'
185+ } ,
186+ }
160187}
188+
161189export const callIframe = async (
162190 path : string ,
163191 {
@@ -182,51 +210,61 @@ export const callIframe = async (
182210 console . error ( 'Get serialNumber error' , e )
183211 throw new Error ( 'Get serialNumber error' )
184212 }
213+
185214 if ( waitResult ) {
186215 return new Promise < unknown > ( async ( resolve , reject ) => {
187216 let timer : NodeJS . Timeout | undefined = undefined
188- const iframeContain = await getIframe (
217+ const { close } = await getIframe (
189218 `${ BASE_URL } ${ path } ?appId=${ appId } &authType=${ authType } &serialNumber=${ serialNumber } &hash=${ hash } &random=${ Math . floor (
190219 Math . random ( ) * 1000
191220 ) } &chainId=${ chainId } ¶ms=${ params } &scope=${ JSON . stringify ( scope ) } `,
192221 ( ) => {
193222 if ( timer ) {
194223 clearTimeout ( timer )
195- reject ( new Error ( 'User cancel' ) )
196224 }
197225 }
198226 )
199- const delay = 800
227+ const delay = 1000
200228 const next = ( i : number ) => {
201229 timer = setTimeout ( async ( ) => {
230+ let data
202231 try {
203- const data = (
232+ data = (
204233 await axios . post ( `${ API_BASE_URL } /open/serial/read` , {
205234 serialNumber : serialNumber ,
206235 hash : hash ,
207236 } )
208237 ) . data . data
209- if ( data && data !== 'false' && data !== false ) {
210- timer && clearTimeout ( timer )
211- iframeContain . remove ( )
212- setBodyScrollable ( )
213- resolve ( JSON . parse ( data ) )
214- } else {
215- if ( i * delay > 10 * 60 * 1000 ) {
216- iframeContain . remove ( )
217- setBodyScrollable ( )
218- reject ( new Error ( 'Timeout' ) )
219- }
220- next ( i ++ )
221- }
222238 } catch ( e ) {
223239 console . error ( "Can't get result from iframe" , e )
240+ next ( i ++ )
241+ return
242+ // reject(new Error("Can't get result from iframe"))
243+ }
244+ if ( data && data !== 'false' && data !== false ) {
245+ timer && clearTimeout ( timer )
246+ close ( )
247+ resolve ( JSON . parse ( data ) )
248+ } else {
249+ if ( i * delay > 10 * 60 * 1000 ) {
250+ close ( )
251+ reject ( new Error ( 'Timeout' ) )
252+ }
253+ next ( i ++ )
224254 }
225255 } , delay )
226256 }
227257 next ( 0 )
228258 } )
229259 }
260+ await getIframe (
261+ `${ BASE_URL } ${ path } ?appId=${ appId } &authType=${ authType } &serialNumber=${ serialNumber } &hash=${ hash } &random=${ Math . floor (
262+ Math . random ( ) * 1000
263+ ) } &chainId=${ chainId } ¶ms=${ params } &scope=${ JSON . stringify ( scope ) } `,
264+ ( ) => {
265+ return
266+ }
267+ )
230268 return 'ok'
231269}
232270
0 commit comments