123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { defineStore, createPinia } from 'pinia'
- import { GlobalState } from './interface/index'
- import { Menu_User_List } from '@/api/role/index'
- import { User_Info } from '@/api/user/index'
- import { isEmptyObject } from '@/utils/common'
- import { User_Dept_List } from '@/api/user/index'
- import { flatMenuListGet, AddRouterMeta, getAllBreadcrumbList } from '@/utils/common'
- export const GlobalStore = defineStore({
- id: 'GlobalState',
- state: (): GlobalState => ({
- isloading: false,
- isCollapse: false,
- breadcrumb: [],
- userInfo: JSON.parse(sessionStorage.getItem('User_info') as any) || {},
- UserDeptList: [],
- UserPostList: [],
- User_tokey: sessionStorage.getItem('User_tokey') || '',
- path: '',
- MenuList: [], // 保存原始菜单
- allBreadcrumbList: [], // 用作面包屑
- expandMenuList: [], // 扩展菜单属性
- flatMenu: [] // 扁平化的数组
- }),
- getters: {
- GET_isloading: state => state.isloading,
- GET_User_tokey: state => state.User_tokey,
- GET_Dept_List: state => state.UserDeptList,
- GET_isCollapse: state => state.isCollapse,
- GET_Breadcrumb: state => state.breadcrumb,
- GET_User_Info: state => state.userInfo,
- GET_Menu_List: state => state.MenuList,
- GET_Flat_Menu: state => state.flatMenu
- },
- actions: {
- SET_isloading(payload: boolean) {
- if (typeof payload === 'boolean') {
- this.isloading = payload
- }
- },
- SET_User_Tokey(payload: string) {
- if (typeof payload === 'string') {
- this.User_tokey = payload
- }
- },
- SET_isCollapse() {
- this.isCollapse = !this.isCollapse
- },
- SET_Breadcrumb(payload: string[]) {
- let map = payload.map(item => {
- return item.replace('/', '')
- })
- this.breadcrumb = map
- },
- SET_User_Info(payload: any) {
- this.userInfo = payload
- },
- SET_Path(payload: string) {
- this.path = payload
- },
- SET_Flat_Menu(payload: any) {
- this.flatMenu = payload
- },
- async SET_User_Dept_List() {
- const res: any = await User_Dept_List()
- this.UserDeptList = res.Data
- },
- async SET_UserInfo() {
- // 如果以及有用户数据,则不请求
- if (isEmptyObject(this.GET_User_Info)) return
- const res: any = await User_Info({ User_tokey: this.GET_User_tokey })
- if (res.Code === 200) {
- this.SET_User_Info(res.Data)
- sessionStorage.setItem('User_info', JSON.stringify(res.Data))
- }
- },
- async SET_Menu_User_List() {
- const res: any = await Menu_User_List({ User_tokey: this.GET_User_tokey })
- if (res.Code === 200) {
- this.MenuList = res.Data.Data // 保存所有菜单
- this.expandMenuList = AddRouterMeta(res.Data.Data) // 扩展菜单属性
- this.SET_Flat_Menu(flatMenuListGet(this.expandMenuList)) // 接收扁平化菜单
- this.allBreadcrumbList = getAllBreadcrumbList(this.expandMenuList)
- }
- }
- }
- })
- const pinia = createPinia()
- export default pinia
|