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

fix: 登录状态维持

parent 1ebcb3f2
...@@ -28,7 +28,7 @@ const LoginWrap: React.FC = () => { ...@@ -28,7 +28,7 @@ const LoginWrap: React.FC = () => {
message.success("登录成功") message.success("登录成功")
setRouters(data.urls) setRouters(data.urls)
// 设置同域名cookie缓存 // 设置同域名cookie缓存
setAuth(omit(data, ['urls'])) setAuth(data)
// 此处需使用href跳转, 否则无法触发app.ts中的路由初始化校验 // 此处需使用href跳转, 否则无法触发app.ts中的路由初始化校验
if (redirect) { if (redirect) {
window.location.replace(decodeURIComponent(atob(redirect))) window.location.replace(decodeURIComponent(atob(redirect)))
......
import { isDev, TOP_DOMAIN } from '@/constants' import { isDev, TOP_DOMAIN } from '@/constants'
import { GetMemberLoginRegetResponse } from '@/services/memberApi' import { GetMemberLoginRegetResponse } from '@/services/memberApi'
import { getCookie, setCookie } from './cookie' import { getTopDomainByHost } from '.'
import { getCookie, removeCookie, setCookie } from './cookie'
import { getUserCookie, setUserCookie } from './siteCookie' import { getUserCookie, setUserCookie } from './siteCookie'
export interface AuthInfo { export interface AuthInfo {
...@@ -11,6 +12,7 @@ export interface AuthInfo { ...@@ -11,6 +12,7 @@ export interface AuthInfo {
logo: string, logo: string,
levelTag: string, levelTag: string,
creditPoint: number, creditPoint: number,
urls: string[]
} }
const AUTH_KEY = 'AUTH' const AUTH_KEY = 'AUTH'
...@@ -25,11 +27,20 @@ export const setAuth = (info: AuthInfo) => { ...@@ -25,11 +27,20 @@ export const setAuth = (info: AuthInfo) => {
creditPoint: info.creditPoint creditPoint: info.creditPoint
} }
setCookie(AUTH_KEY, JSON.stringify(auth), { domain: TOP_DOMAIN }) 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 => { export const getAuth = (): AuthInfo => {
try { try {
const localAuth = getCookie(AUTH_KEY) const localAuth = JSON.parse(window.localStorage.getItem(AUTH_KEY))
return (localAuth || {} )as AuthInfo return (localAuth || {} )as AuthInfo
} catch (error) { } catch (error) {
return {} as AuthInfo return {} as AuthInfo
...@@ -42,7 +53,7 @@ export const setRouters = (routers: any[]) => { ...@@ -42,7 +53,7 @@ export const setRouters = (routers: any[]) => {
export const getRouters = (): string[] => { export const getRouters = (): string[] => {
try { try {
const localAuth = window.localStorage.getItem('auth') const localAuth = window.sessionStorage.getItem('rt')
if (localAuth) { if (localAuth) {
const userInfo = JSON.parse(localAuth) const userInfo = JSON.parse(localAuth)
return userInfo.urls ? userInfo.urls : [] return userInfo.urls ? userInfo.urls : []
...@@ -58,7 +69,10 @@ export const removeRouters = () => { ...@@ -58,7 +69,10 @@ export const removeRouters = () => {
} }
export const removeAuth = () => { 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[]) => { 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