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 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";
|
||||||
|
|
||||||
|
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() {
|
export function getToken() {
|
||||||
return getStudentDemoSession()?.token || Cookies.get(TokenKey);
|
return getStudentDemoSession()?.token || Cookies.get(TokenKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setToken(token) {
|
export function setToken(token) {
|
||||||
return Cookies.set(TokenKey, token);
|
return setAuthCookie(TokenKey, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function removeToken() {
|
export function removeToken() {
|
||||||
if (isStudentDemo()) return clearStudentDemoSession();
|
if (isStudentDemo()) return clearStudentDemoSession();
|
||||||
return Cookies.remove(TokenKey);
|
return removeAuthCookie(TokenKey);
|
||||||
}
|
}
|
||||||
export function setUserInfo(userinfo) {
|
export function setUserInfo(userinfo) {
|
||||||
return Cookies.set(user, userinfo);
|
return setAuthCookie(user, userinfo);
|
||||||
}
|
}
|
||||||
export function getUserInfo() {
|
export function getUserInfo() {
|
||||||
const demo = getStudentDemoSession();
|
const demo = getStudentDemoSession();
|
||||||
return demo?.userInfo ? JSON.stringify(demo.userInfo) : Cookies.get(user);
|
return demo?.userInfo ? JSON.stringify(demo.userInfo) : Cookies.get(user);
|
||||||
}
|
}
|
||||||
export function removeUserInfo() {
|
export function removeUserInfo() {
|
||||||
if (isStudentDemo()) return clearStudentDemoSession();
|
if (isStudentDemo()) return clearStudentDemoSession();
|
||||||
return Cookies.remove(user);
|
return removeAuthCookie(user);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue