You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.2 KiB

  1. // src/store/index.js
  2. import {defineStore} from 'pinia'
  3. export const useAdminStore = defineStore('admin', {
  4. state: () => ({
  5. adminData: null, // 用户信息
  6. menuTree: [], // 菜单权限树
  7. marketList: {}, // 市场列表
  8. flag: 0, //员工数据开关状态,0=不包含员工数据,1=包含员工数据
  9. }),
  10. actions: {
  11. // 设置用户信息并同步到localStorage
  12. setAdminData(info) {
  13. this.adminData = info
  14. localStorage.setItem('adminData', JSON.stringify(info))
  15. },
  16. // 设置菜单树并同步到localStorage
  17. setMenuTree(tree) {
  18. this.menuTree = tree
  19. localStorage.setItem('menuTree', JSON.stringify(tree))
  20. },
  21. setMarketList(list) {
  22. this.marketList = list
  23. localStorage.setItem('marketList', JSON.stringify(list))
  24. },
  25. // 设置员工数据开关状态(使用数字0和1)
  26. setFlag(flag) {
  27. this.flag = flag
  28. localStorage.setItem('flag', JSON.stringify(flag))
  29. },
  30. // 从localStorage初始化数据
  31. initFromLocalStorage() {
  32. const adminData = localStorage.getItem('adminData')
  33. const menuTree = localStorage.getItem('menuTree')
  34. const marketList = localStorage.getItem('marketList')
  35. const flag = localStorage.getItem('flag')
  36. if (adminData) {
  37. this.adminData = JSON.parse(adminData)
  38. }
  39. if (menuTree) {
  40. this.menuTree = JSON.parse(menuTree)
  41. }
  42. if (marketList) {
  43. this.marketList = JSON.parse(marketList)
  44. }
  45. if (flag) {
  46. this.flag = JSON.parse(flag)
  47. }
  48. },
  49. // 清空状态并移除localStorage数据
  50. clearState() {
  51. this.adminData = null
  52. this.menuTree = []
  53. this.marketList = {}
  54. this.flag = 0
  55. localStorage.removeItem('adminData')
  56. localStorage.removeItem('menuTree')
  57. localStorage.removeItem('marketList')
  58. localStorage.removeItem('flag')
  59. // localStorage.removeItem('token')
  60. }
  61. }
  62. })