@ -1,34 +1,33 @@
import Cookies from "js-cookie" ;
import Cookies from "js-cookie" ;
import { clearStudentDemoSession , getStudentDemoSession , isStudentDemo } from "@/utils/studentDemo" ;
import { clearStudentDemoSession , getStudentDemoSession , isStudentDemo } from "@/utils/studentDemo" ;
const TokenKey = "Admin-Token" ;
const TokenKey = "Admin-Token" ;
const user = "UserInfo" ;
const user = "UserInfo" ;
const SharedCookieDomain = ".sztzjy.com" ;
// 历史遗留:旧版本曾把 cookie 写到共享主域 .sztzjy.com, 会污染其他子域系统的登录态。
// 现已改为 host-only( 只写当前子域) , 此常量仅用于清理浏览器中遗留的旧主域 cookie。
const LegacySharedCookieDomain = ".sztzjy.com" ;
function isSharedCookieHost ( ) {
function isSharedCookieHost ( ) {
if ( typeof window === "undefined" ) return false ;
if ( typeof window === "undefined" ) return false ;
const hostname = window . location . hostname . toLowerCase ( ) ;
const hostname = window . location . hostname . toLowerCase ( ) ;
return hostname === "sztzjy.com" || hostname . endsWith ( SharedCookieDomain) ;
return hostname === "sztzjy.com" || hostname . endsWith ( Legacy SharedCookieDomain) ;
}
}
function getAuthCookieOptions ( ) {
function removeLegacySharedCookie ( name ) {
if ( ! isSharedCookieHost ( ) ) return { path : "/" } ;
if ( isSharedCookieHost ( ) ) {
return {
Cookies . remove ( name , { domain : LegacySharedCookieDomain , path : "/" } ) ;
domain : SharedCookieDomain ,
}
path : "/" ,
secure : true ,
sameSite : "Lax" ,
} ;
}
}
function setAuthCookie ( name , value ) {
function setAuthCookie ( name , value ) {
if ( isSharedCookieHost ( ) ) Cookies . remove ( name , { path : "/" } ) ;
// 先清掉旧主域 cookie, 避免同名双 cookie 导致读取到脏值
return Cookies . set ( name , value , getAuthCookieOptions ( ) ) ;
removeLegacySharedCookie ( name ) ;
return Cookies . set ( name , value , { path : "/" } ) ;
}
}
function removeAuthCookie ( name ) {
function removeAuthCookie ( name ) {
Cookies . remove ( name , { path : "/" } ) ;
Cookies . remove ( name , { path : "/" } ) ;
if ( isSharedCookieHost ( ) ) Cookies . remove ( name , getAuthCookieOptions ( ) ) ;
removeLegacySharedCookie ( name ) ;
}
}
export function getToken ( ) {
export function getToken ( ) {