index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { defineStore, createPinia } from 'pinia'
  2. import { GlobalState } from './interface/index'
  3. import { Menu_User_List } from '@/api/role/index'
  4. import { User_Info } from '@/api/user/index'
  5. import { isEmptyObject } from '@/utils/common'
  6. import { User_Dept_List } from '@/api/user/index'
  7. import { flatMenuListGet, AddRouterMeta, getAllBreadcrumbList } from '@/utils/common'
  8. export const GlobalStore = defineStore({
  9. id: 'GlobalState',
  10. state: (): GlobalState => ({
  11. isloading: false,
  12. isCollapse: false,
  13. breadcrumb: [],
  14. userInfo: JSON.parse(sessionStorage.getItem('User_info') as any) || {},
  15. UserDeptList: [],
  16. UserPostList: [],
  17. User_tokey: sessionStorage.getItem('User_tokey') || '',
  18. path: '',
  19. MenuList: [], // 保存原始菜单
  20. allBreadcrumbList: [], // 用作面包屑
  21. expandMenuList: [], // 扩展菜单属性
  22. flatMenu: [] // 扁平化的数组
  23. }),
  24. getters: {
  25. GET_isloading: state => state.isloading,
  26. GET_User_tokey: state => state.User_tokey,
  27. GET_Dept_List: state => state.UserDeptList,
  28. GET_isCollapse: state => state.isCollapse,
  29. GET_Breadcrumb: state => state.breadcrumb,
  30. GET_User_Info: state => state.userInfo,
  31. GET_Menu_List: state => state.MenuList,
  32. GET_Flat_Menu: state => state.flatMenu
  33. },
  34. actions: {
  35. SET_isloading(payload: boolean) {
  36. if (typeof payload === 'boolean') {
  37. this.isloading = payload
  38. }
  39. },
  40. SET_User_Tokey(payload: string) {
  41. if (typeof payload === 'string') {
  42. this.User_tokey = payload
  43. }
  44. },
  45. SET_isCollapse() {
  46. this.isCollapse = !this.isCollapse
  47. },
  48. SET_Breadcrumb(payload: string[]) {
  49. let map = payload.map(item => {
  50. return item.replace('/', '')
  51. })
  52. this.breadcrumb = map
  53. },
  54. SET_User_Info(payload: any) {
  55. this.userInfo = payload
  56. },
  57. SET_Path(payload: string) {
  58. this.path = payload
  59. },
  60. SET_Flat_Menu(payload: any) {
  61. this.flatMenu = payload
  62. },
  63. async SET_User_Dept_List() {
  64. const res: any = await User_Dept_List()
  65. this.UserDeptList = res.Data
  66. },
  67. async SET_UserInfo() {
  68. // 如果以及有用户数据,则不请求
  69. if (isEmptyObject(this.GET_User_Info)) return
  70. const res: any = await User_Info({ User_tokey: this.GET_User_tokey })
  71. if (res.Code === 200) {
  72. this.SET_User_Info(res.Data)
  73. sessionStorage.setItem('User_info', JSON.stringify(res.Data))
  74. }
  75. },
  76. async SET_Menu_User_List() {
  77. const res: any = await Menu_User_List({ User_tokey: this.GET_User_tokey })
  78. if (res.Code === 200) {
  79. this.MenuList = res.Data.Data // 保存所有菜单
  80. this.expandMenuList = AddRouterMeta(res.Data.Data) // 扩展菜单属性
  81. this.SET_Flat_Menu(flatMenuListGet(this.expandMenuList)) // 接收扁平化菜单
  82. this.allBreadcrumbList = getAllBreadcrumbList(this.expandMenuList)
  83. }
  84. }
  85. }
  86. })
  87. const pinia = createPinia()
  88. export default pinia