Commit a7722af5 authored by 前端-许佳敏's avatar 前端-许佳敏

fix: 登录状态维持

parent 1ebcb3f2
......@@ -28,7 +28,7 @@ const LoginWrap: React.FC = () => {
message.success("登录成功")
setRouters(data.urls)
// 设置同域名cookie缓存
setAuth(omit(data, ['urls']))
setAuth(data)
// 此处需使用href跳转, 否则无法触发app.ts中的路由初始化校验
if (redirect) {
window.location.replace(decodeURIComponent(atob(redirect)))
......
import { isDev, TOP_DOMAIN } from '@/constants'
import { GetMemberLoginRegetResponse } from '@/services/memberApi'
import { getCookie, setCookie } from './cookie'
import { getTopDomainByHost } from '.'
import { getCookie, removeCookie, setCookie } from './cookie'
import { getUserCookie, setUserCookie } from './siteCookie'
export interface AuthInfo {
......@@ -11,6 +12,7 @@ export interface AuthInfo {
logo: string,
levelTag: string,
creditPoint: number,
urls: string[]
}
const AUTH_KEY = 'AUTH'
......@@ -25,11 +27,20 @@ export const setAuth = (info: AuthInfo) => {
creditPoint: info.creditPoint
}
setCookie(AUTH_KEY, JSON.stringify(auth), { domain: TOP_DOMAIN })
setLocalAuth(info)
setRouters(info.urls)
}
/**
* 由于member本地需要获取urls字段, 但cookie不需要, 所以分开保留以前的储存方式
*/
export const setLocalAuth = (info: AuthInfo) => {
window.localStorage.setItem(AUTH_KEY, JSON.stringify(info))
}
export const getAuth = (): AuthInfo => {
try {
const localAuth = getCookie(AUTH_KEY)
const localAuth = JSON.parse(window.localStorage.getItem(AUTH_KEY))
return (localAuth || {} )as AuthInfo
} catch (error) {
return {} as AuthInfo
......@@ -42,7 +53,7 @@ export const setRouters = (routers: any[]) => {
export const getRouters = (): string[] => {
try {
const localAuth = window.localStorage.getItem('auth')
const localAuth = window.sessionStorage.getItem('rt')
if (localAuth) {
const userInfo = JSON.parse(localAuth)
return userInfo.urls ? userInfo.urls : []
......@@ -58,7 +69,10 @@ export const removeRouters = () => {
}
export const removeAuth = () => {
window.localStorage.removeItem('auth')
console.log(TOP_DOMAIN)
removeCookie(AUTH_KEY, { path: '/', domain: TOP_DOMAIN })
window.localStorage.removeItem(AUTH_KEY)
removeRouters()
}
export const asyncRouter = async (routeLists: string[], routes: any[]) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment