Merge branch 'master' of http://118.31.7.2:3000/chenyuan1/dianshang-qianduan
commit
6bba6e9359
@ -1,28 +1,56 @@
|
||||
import Cookies from "js-cookie";
|
||||
import { clearStudentDemoSession, getStudentDemoSession, isStudentDemo } from "@/utils/studentDemo";
|
||||
|
||||
const TokenKey = "Admin-Token";
|
||||
const user = "UserInfo";
|
||||
const TokenKey = "Admin-Token";
|
||||
const user = "UserInfo";
|
||||
const SharedCookieDomain = ".sztzjy.com";
|
||||
|
||||
function isSharedCookieHost() {
|
||||
if (typeof window === "undefined") return false;
|
||||
const hostname = window.location.hostname.toLowerCase();
|
||||
return hostname === "sztzjy.com" || hostname.endsWith(SharedCookieDomain);
|
||||
}
|
||||
|
||||
function getAuthCookieOptions() {
|
||||
if (!isSharedCookieHost()) return { path: "/" };
|
||||
return {
|
||||
domain: SharedCookieDomain,
|
||||
path: "/",
|
||||
secure: true,
|
||||
sameSite: "Lax",
|
||||
};
|
||||
}
|
||||
|
||||
function setAuthCookie(name, value) {
|
||||
if (isSharedCookieHost()) Cookies.remove(name, { path: "/" });
|
||||
return Cookies.set(name, value, getAuthCookieOptions());
|
||||
}
|
||||
|
||||
function removeAuthCookie(name) {
|
||||
Cookies.remove(name, { path: "/" });
|
||||
if (isSharedCookieHost()) Cookies.remove(name, getAuthCookieOptions());
|
||||
}
|
||||
|
||||
export function getToken() {
|
||||
return getStudentDemoSession()?.token || Cookies.get(TokenKey);
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
return Cookies.set(TokenKey, token);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
return setAuthCookie(TokenKey, token);
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
if (isStudentDemo()) return clearStudentDemoSession();
|
||||
return Cookies.remove(TokenKey);
|
||||
}
|
||||
export function setUserInfo(userinfo) {
|
||||
return Cookies.set(user, userinfo);
|
||||
}
|
||||
return removeAuthCookie(TokenKey);
|
||||
}
|
||||
export function setUserInfo(userinfo) {
|
||||
return setAuthCookie(user, userinfo);
|
||||
}
|
||||
export function getUserInfo() {
|
||||
const demo = getStudentDemoSession();
|
||||
return demo?.userInfo ? JSON.stringify(demo.userInfo) : Cookies.get(user);
|
||||
}
|
||||
export function removeUserInfo() {
|
||||
if (isStudentDemo()) return clearStudentDemoSession();
|
||||
return Cookies.remove(user);
|
||||
return removeAuthCookie(user);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue