From b42d843099d7f3d83eefec32e9cfe0121fbd8745 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Tue, 3 Jun 2025 23:26:11 +0800 Subject: [PATCH] add iframe --- src/user/store/index.ts | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/user/store/index.ts b/src/user/store/index.ts index e8b21ec..3725b1f 100644 --- a/src/user/store/index.ts +++ b/src/user/store/index.ts @@ -25,9 +25,50 @@ export type Config = { }; beian: string; }; +export const inIframeToDo = async (config?: Config) => { + const isInIframe = window !== window.parent && !window.opener; + + if (isInIframe && config) { + try { + // 检查是否同源 + const isSameOrigin = (() => { + try { + // 尝试访问父窗口的 location.origin,如果能访问则是同源 + return window.parent.location.origin === window.location.origin; + } catch (e) { + // 如果出现跨域错误,则不是同源 + return false; + } + })(); + const isLocalhost = window.location.hostname === 'localhost'; + const isKevisual = window.location.hostname.includes('kevisual'); + if (isSameOrigin || isLocalhost || isKevisual) { + // 同源情况下,可以直接向父窗口传递配置 + window.parent.postMessage( + { + type: 'kevisual-login', + data: config, + }, + window.location.origin, + ); + + console.log('已向父窗口传递登录配置信息'); + } + } catch (error) { + console.error('向父窗口传递配置信息失败:', error); + } + } + + return isInIframe; +}; export const redirectToSuccess = async (config: Config) => { const href = location.href; const url = new URL(href); + const check = await inIframeToDo(config); + if (check) { + return; + } + const redirect = url.searchParams.get('redirect'); if (redirect) { const href = decodeURIComponent(redirect); @@ -44,6 +85,7 @@ export const redirectToSuccess = async (config: Config) => { location.href = '/'; } }; + type UserStore = { isAuthenticated: boolean; qrCodeUrl: string;