import { MutationTree , ActionContext , ActionTree , CommitOptions , DispatchOptions , Store as VuexStore , Module } from 'vuex' ; import { AppRouteRecordRaw } from '@/common/types/appRoute.type' ; import { asyncRoutes , constantRoutes } from '@/router/index' ; import { RootState } from '../index' ; import { MenuApiResult } from '@/api/menu' ; export enum PermissionMutationContants { SET_ROUTES = 'PERMISSION/SET_ROUTES' } export enum PermissionActionContants { GENRATERROUTES = 'permission/generaterRoutes' } export type PermissionState = { routes : AppRouteRecordRaw [ ] ; addRoutes : AppRouteRecordRaw [ ] ; isReqPerm : boolean ; } ; const state = { routes : [ ] , addRoutes : [ ] , isReqPerm : false } ; interface PermissionMutation { [ PermissionMutationContants . SET_ROUTES ] ( state , routes ) : void ; } const mutations = { [ PermissionMutationContants . SET_ROUTES ] : ( state , routes ) => { state . addRoutes = routes ; state . routes = constantRoutes . concat ( routes ) ; state . isReqPerm = true ; } } ; type AugmentedActionContext = { commit < K extends keyof PermissionMutation > ( key , payload ? ) : ReturnType < PermissionMutation [ K ] > ; } & Omit < ActionContext < PermissionState , RootState > , 'commit' > ; interface PermissionAction { generaterRoutes ( { commit } , menus ) : Promise < AppRouteRecordRaw [ ] > ; } const hasPermission = ( route , menus ) : boolean => { if ( route . name ) { return menus . some ( menu => menu . code === route . name ) ; } if ( route . hidden ) return true ; if ( route . children && route . children . length > 0 ) return hasPermission ( route . children [ 0 ] , menus ) ; return false ; } ; const filterAsyncRoutes = ( routes , menus ) : AppRouteRecordRaw [ ] => { const res = [ ] ; routes . forEach ( route => { const tmp = { ... route } ; if ( hasPermission ( tmp , menus ) ) { if ( tmp . children && tmp . children . length > 0 ) tmp . children = filterAsyncRoutes ( tmp . children , menus ) ; res . push ( tmp ) ; } } ) ; return res ; } ; const actions = { generaterRoutes ( { commit } , menus ) { return new Promise ( resolve => { const accessRoutes = filterAsyncRoutes ( asyncRoutes , menus ) ; commit ( PermissionMutationContants . SET_ROUTES , accessRoutes ) ; resolve ( accessRoutes ) ; } ) ; } } ; export type PermissionStore < S = PermissionState > = Omit < VuexStore < S > , 'commit' | 'dispatch' > & { commit < K extends keyof PermissionMutation , P extends Parameters < PermissionMutation [ K ] > [ 1 ] > ( key , payload ? , options ? ) : ReturnType < PermissionMutation [ K ] > ; } & { dispatch < K extends keyof PermissionAction , P extends Parameters < PermissionAction [ K ] > [ 1 ] > ( key , payload ? , options ? ) : ReturnType < PermissionAction [ K ] > ; } ; export const PermissionModule = { namespaced : true , state , mutations , actions } ;