add iframe
This commit is contained in:
		@@ -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;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user