zhang_12370
10 天以前 4c9e1ba3c7f5c95a1789e88823ff262c26a53bce
src/store/modules/user.js
@@ -2,6 +2,7 @@
import { getToken, setToken, removeToken } from '@/utils/auth'
import { isHttp, isEmpty } from "@/utils/validate"
import defAva from '@/assets/images/profile.jpg'
import {userListAll} from '@/api/publicApi/index.js'
const useUserStore = defineStore(
  'user',
@@ -13,7 +14,8 @@
      nickName: '',
      avatar: '',
      roles: [],
      permissions: []
      permissions: [],
      userList: []
    }),
    actions: {
      // 登录
@@ -70,6 +72,20 @@
            reject(error)
          })
        })
      },
      // 获取用户列表(如果已缓存则直接返回,否则重新获取)
      async getUserList () {
        if (Array.isArray(this.userList) && this.userList.length > 0) {
          return this.userList;
        }
        // 为空时重新获取
        return userListAll().then(res => {
          this.userList = res.data;
          return this.userList;
        }).catch(error => {
          throw error;
        });
      }
    }
  })