import { ActionContext , ActionTree , CommitOptions , DispatchOptions , GetterTree , Module , MutationTree , Store as VuexStore } from 'vuex' ; import { ResultData } from '@/common/types/apiResult.type' ; import { RootState } from '../index' ; import { login , UserApiResult , UserLogin , getUserInfo as getUserInfoApi } from '@/api/user' ; import { PermApiResult , getCurrUserMenuPerms , getAllApiPerms } from '@/api/perm' ; import { MenuTypeContants , MenuApiResult } from '@/api/menu' ; export enum UserMutationContants { SET_USER = 'SET_USER' , SET_PERMSMENU = 'SET_PERMSMENUS' , SET_ALLAPIPERMS = 'SET_APIPERMS' } export enum UserActionContants { GET_USER_INFO = 'user/getUserInfo' , GET_USER_MENU_PERM = 'user/getCurrUserMenuPerm' , GET_ALL_API_PERMS = 'user/getAllApiPerms' } export interface UserState { user : UserApiResult ; permMenus : MenuApiResult [ ] ; permButton : MenuApiResult [ ] ; permTabs : MenuApiResult [ ] ; allApiPerms : PermApiResult [ ] ; } const state = { user : { id : '' , account : '' , avatar : '' } , permMenus : [ ] , permButton : [ ] , permTabs : [ ] , allApiPerms : [ ] } ; interface UserMutation { [ UserMutationContants . SET_USER ] ( state , user ) : void ; [ UserMutationContants . SET_PERMSMENU ] ( state , permSource ) : void ; [ UserMutationContants . SET_ALLAPIPERMS ] ( state , apiPerms ) : void ; } const mutations = { [ UserMutationContants . SET_USER ] : ( state , user ) => { state . user = user ; } , [ UserMutationContants . SET_PERMSMENU ] : ( state , permSource ) => { permSource . forEach ( v => { switch ( v . type ) { case MenuTypeContants . MENU : state . permMenus . push ( v ) ; break ; case MenuTypeContants . TAB : state . permTabs . push ( v ) ; break ; case MenuTypeContants . BUTTON : state . permButton . push ( v ) ; break ; default : break ; } } ) ; } , [ UserMutationContants . SET_ALLAPIPERMS ] : ( state , apiPerms ) => { state . allApiPerms = apiPerms ; } } ; type AugmentedActionContext = { state : UserState ; commit < K extends keyof UserMutation > ( key , payload ? ) : ReturnType < UserMutation [ K ] > ; } & Omit < ActionContext < UserState , RootState > , 'commit' | 'state' > ; interface UserAction { getUserInfo ( { commit , state } , reload ? ) : Promise < UserApiResult > ; getAllApiPerms ( { commit , state } ) : Promise < Array < PermApiResult > > ; getCurrUserMenuPerm ( { commit } ) : Promise < MenuApiResult [ ] > ; } const actions = { getUserInfo : async ( { commit , state } , reload ? ) : Promise < UserApiResult > => { if ( state . user && ! reload ) return state . user ; const res = await getUserInfoApi ( ) ; if ( res ?. code === 200 ) { const user = res . data as UserApiResult ; commit ( UserMutationContants . SET_USER , user ) ; return user ; } return { id : '' , account : '' , avatar : '' } as UserApiResult ; } , getCurrUserMenuPerm : async ( { commit } ) : Promise < MenuApiResult [ ] > => { const res = await getCurrUserMenuPerms ( ) ; if ( res ?. code === 200 ) { const data = res . data as MenuApiResult [ ] ; commit ( UserMutationContants . SET_PERMSMENU , data ) ; return data ; } return [ ] ; } , getAllApiPerms : async ( { commit , state } ) : Promise < Array < PermApiResult > > => { if ( state . allApiPerms . length > 0 ) return state . allApiPerms ; const res = await getAllApiPerms ( ) ; if ( res ?. code === 200 ) { const apiPerms = res . data as Array < PermApiResult > ; commit ( UserMutationContants . SET_ALLAPIPERMS , apiPerms ) ; return apiPerms ; } return [ ] ; } } ; export type UserStore < S = UserState > = Omit < VuexStore < S > , 'commit' | 'dispatch' > & { commit < K extends keyof UserMutation , P extends Parameters < UserMutation [ K ] > [ 1 ] > ( key , payload ? , options ? ) : ReturnType < UserMutation [ K ] > ; } & { dispatch < K extends keyof UserAction , P extends Parameters < UserAction [ K ] > [ 1 ] > ( key , payload ? , options ? ) : ReturnType < UserAction [ K ] > ; } ; export const UserModule = { namespaced : true , state , mutations , actions } ;