|
@@ -4,11 +4,32 @@ const TokenKey = 'Admin-Token'
|
|
|
|
|
|
|
|
const isSecure = window.location.protocol === 'https:'
|
|
const isSecure = window.location.protocol === 'https:'
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 是否运行在 iframe 内(被 SCRM 等宿主系统内嵌)
|
|
|
|
|
+ * 跨域访问 window.top 会抛异常,同样说明处于 iframe 中
|
|
|
|
|
+ */
|
|
|
|
|
+export function isEmbedded(): boolean {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return window.self !== window.top
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return true
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export function getToken(): string | undefined {
|
|
export function getToken(): string | undefined {
|
|
|
|
|
+ // iframe 内 token 走 sessionStorage:跨站 iframe 不会携带 SameSite=Strict 的 Cookie,
|
|
|
|
|
+ // 独立访问时行为保持原样(Cookie)
|
|
|
|
|
+ if (isEmbedded()) {
|
|
|
|
|
+ return sessionStorage.getItem(TokenKey) || undefined
|
|
|
|
|
+ }
|
|
|
return Cookies.get(TokenKey)
|
|
return Cookies.get(TokenKey)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function setToken(token: string): void {
|
|
export function setToken(token: string): void {
|
|
|
|
|
+ if (isEmbedded()) {
|
|
|
|
|
+ sessionStorage.setItem(TokenKey, token)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
Cookies.set(TokenKey, token, {
|
|
Cookies.set(TokenKey, token, {
|
|
|
secure: isSecure,
|
|
secure: isSecure,
|
|
|
sameSite: 'Strict',
|
|
sameSite: 'Strict',
|
|
@@ -17,5 +38,9 @@ export function setToken(token: string): void {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function removeToken(): void {
|
|
export function removeToken(): void {
|
|
|
|
|
+ if (isEmbedded()) {
|
|
|
|
|
+ sessionStorage.removeItem(TokenKey)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
Cookies.remove(TokenKey)
|
|
Cookies.remove(TokenKey)
|
|
|
}
|
|
}
|