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.

44 lines
1.3 KiB

  1. // src/store/index.js
  2. import { defineStore } from 'pinia'
  3. export const useAdminStore = defineStore('admin', {
  4. state: () => ({
  5. adminData: null, // 用户信息
  6. menuTree: null, // 菜单权限树
  7. }),
  8. actions: {
  9. // 设置用户信息并同步到localStorage
  10. setAdminData(info) {
  11. this.adminData = info
  12. localStorage.setItem('adminData', JSON.stringify(info))
  13. },
  14. // 设置菜单树并同步到localStorage
  15. setMenuTree(tree) {
  16. this.menuTree = tree
  17. localStorage.setItem('menuTree', JSON.stringify(tree))
  18. },
  19. // 从localStorage初始化数据
  20. initFromLocalStorage() {
  21. const adminData = localStorage.getItem('adminData')
  22. const menuTree = localStorage.getItem('menuTree')
  23. if (adminData) {
  24. this.adminData = JSON.parse(adminData)
  25. }
  26. if (menuTree) {
  27. this.menuTree = JSON.parse(menuTree)
  28. }
  29. },
  30. // 清空状态并移除localStorage数据
  31. clearState() {
  32. this.adminData = null
  33. this.menuTree = null
  34. localStorage.removeItem('adminData')
  35. localStorage.removeItem('menuTree')
  36. // localStorage.removeItem('token')
  37. }
  38. }
  39. })