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.

56 lines
1.7 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. }),
  9. actions: {
  10. // 设置用户信息并同步到localStorage
  11. setAdminData(info) {
  12. this.adminData = info
  13. localStorage.setItem('adminData', JSON.stringify(info))
  14. },
  15. // 设置菜单树并同步到localStorage
  16. setMenuTree(tree) {
  17. this.menuTree = tree
  18. localStorage.setItem('menuTree', JSON.stringify(tree))
  19. },
  20. setMarketList(list) {
  21. this.marketList = list
  22. localStorage.setItem('marketList', JSON.stringify(list))
  23. },
  24. // 从localStorage初始化数据
  25. initFromLocalStorage() {
  26. const adminData = localStorage.getItem('adminData')
  27. const menuTree = localStorage.getItem('menuTree')
  28. const marketList = localStorage.getItem('marketList')
  29. if (adminData) {
  30. this.adminData = JSON.parse(adminData)
  31. }
  32. if (menuTree) {
  33. this.menuTree = JSON.parse(menuTree)
  34. }
  35. if (marketList) {
  36. this.marketList = JSON.parse(marketList)
  37. }
  38. },
  39. // 清空状态并移除localStorage数据
  40. clearState() {
  41. this.adminData = null
  42. this.menuTree = []
  43. this.marketList = {}
  44. localStorage.removeItem('adminData')
  45. localStorage.removeItem('menuTree')
  46. localStorage.removeItem('marketList')
  47. // localStorage.removeItem('token')
  48. }
  49. }
  50. })