11import { configureStore } from '@reduxjs/toolkit'
2+ import config from '@/config'
3+ import router from '@/router'
24import themeReducer from './themeSlice'
35import routeLoadingReducer from './routeLoadingSlice'
46import layoutStateReducer from './layoutStateSlice'
57import activitysReducer from './activitysSlice'
68import layoutTabsReducer from './layoutTabsSlice'
7- import userReducer from './userSlice'
9+ import userReducer , { changeUserInfo , onlyChangeUserInfo , type UserState } from './userSlice'
810import watermarkReducer from './watermarkSlice'
911import systemSetReducer from './systemSetSlice'
1012
@@ -25,3 +27,31 @@ export default store
2527
2628export type RootState = ReturnType < typeof store . getState >
2729export type AppDispatch = typeof store . dispatch
30+
31+ const BASE_URL = import . meta. env . BASE_URL
32+ export const loginFn = ( userInfo : UserState [ 'value' ] ) => {
33+ localStorage . setItem ( config . loginLocalStorageKey , JSON . stringify ( userInfo ) )
34+ store . dispatch ( changeUserInfo ( userInfo ) )
35+ router . navigate ( '/' )
36+ }
37+ export const onlyLoginFn = ( userInfo : UserState [ 'value' ] ) => {
38+ store . dispatch ( onlyChangeUserInfo ( userInfo ) )
39+ const pathname = window . location . pathname
40+ if ( pathname === BASE_URL + 'login' ) {
41+ router . navigate ( '/' )
42+ }
43+ }
44+ export const logoutFn = ( ) => {
45+ localStorage . removeItem ( config . loginLocalStorageKey )
46+ store . dispatch ( changeUserInfo ( { id : '' , name : '' , img : undefined , badge : 0 , permissionList : [ ] } ) )
47+ router . navigate ( '/login' )
48+ }
49+ export const onlyLogoutFn = ( ) => {
50+ store . dispatch ( onlyChangeUserInfo ( { id : '' , name : '' , img : undefined , badge : 0 , permissionList : [ ] } ) )
51+ const pathname = window . location . pathname
52+ const whiteList = config . whiteList . map ( x => BASE_URL + x . replace ( / ^ \/ / , '' ) )
53+ if ( ! whiteList . includes ( pathname ) ) {
54+ router . navigate ( '/login' )
55+ }
56+ }
57+
0 commit comments