add iframe
This commit is contained in:
parent
9e6649afa0
commit
b42d843099
@ -25,9 +25,50 @@ export type Config = {
|
|||||||
};
|
};
|
||||||
beian: string;
|
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) => {
|
export const redirectToSuccess = async (config: Config) => {
|
||||||
const href = location.href;
|
const href = location.href;
|
||||||
const url = new URL(href);
|
const url = new URL(href);
|
||||||
|
const check = await inIframeToDo(config);
|
||||||
|
if (check) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const redirect = url.searchParams.get('redirect');
|
const redirect = url.searchParams.get('redirect');
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
const href = decodeURIComponent(redirect);
|
const href = decodeURIComponent(redirect);
|
||||||
@ -44,6 +85,7 @@ export const redirectToSuccess = async (config: Config) => {
|
|||||||
location.href = '/';
|
location.href = '/';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
type UserStore = {
|
type UserStore = {
|
||||||
isAuthenticated: boolean;
|
isAuthenticated: boolean;
|
||||||
qrCodeUrl: string;
|
qrCodeUrl: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user