feat: release v1.0.0
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
import { toast } from 'sonner'
|
||||
import { create } from 'zustand'
|
||||
|
||||
import { getSysUserInfo, logout } from '@/api'
|
||||
import { getExpandedMenus, sleep } from '@/lib'
|
||||
import type { SysUserInfo } from '@/schemas'
|
||||
|
||||
const IS_LOGIN_KEY = 'is_login'
|
||||
|
||||
interface Store {
|
||||
token: string | null
|
||||
tokenExp: string | null
|
||||
userInfo: SysUserInfo | null
|
||||
expandedMenus: number[]
|
||||
isLogin: boolean
|
||||
setToken: (token: string | null, tokenExp?: string | null) => void
|
||||
logout: () => void
|
||||
init: () => Promise<void>
|
||||
@@ -19,19 +23,42 @@ const isLoginPage = () => {
|
||||
return typeof window !== 'undefined' && window.location.pathname === '/login'
|
||||
}
|
||||
|
||||
const getIsLogin = () => {
|
||||
return typeof window !== 'undefined' && localStorage.getItem(IS_LOGIN_KEY) === 'true'
|
||||
}
|
||||
|
||||
const setLoginFlag = (isLogin: boolean) => {
|
||||
if (isLogin) {
|
||||
localStorage.setItem(IS_LOGIN_KEY, 'true')
|
||||
} else {
|
||||
localStorage.removeItem(IS_LOGIN_KEY)
|
||||
}
|
||||
}
|
||||
|
||||
export const useStore = create<Store>((set) => {
|
||||
return {
|
||||
token: null,
|
||||
tokenExp: null,
|
||||
userInfo: null,
|
||||
expandedMenus: [],
|
||||
isLogin: getIsLogin(),
|
||||
setToken: (token, tokenExp = null) => {
|
||||
setLoginFlag(Boolean(token))
|
||||
|
||||
set({ token, tokenExp })
|
||||
},
|
||||
logout: async () => {
|
||||
await logout()
|
||||
set({ token: null, tokenExp: null, userInfo: null })
|
||||
location.href = '/login'
|
||||
try {
|
||||
await logout()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error('退出登录失败,请稍后重试', { richColors: true })
|
||||
return
|
||||
}
|
||||
|
||||
set({ token: null, tokenExp: null, userInfo: null, isLogin: false })
|
||||
setLoginFlag(false)
|
||||
location.replace('/login')
|
||||
},
|
||||
init: async () => {
|
||||
if (isLoginPage()) {
|
||||
|
||||
Reference in New Issue
Block a user