若依框架 前台自己获取登录用户信息

这个方法其实只能获取到用户名称,获取不到用户id,不知道官方文档写了但是没有是个什么意思

const userid = this.$store.state.user.userId;

const username= this.$store.state.user.name 

1.获取用户id

在src包下找到store,modules下的user.js文件

login_id就是需要用到的用户id

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    LOGIN_ID: (state, userId) => {
      state.userId = userId
    }
  },

在获取用户信息GetInfo方法中获取一下id

    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('LOGIN_ID', user.userId)
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
 
 

你可能感兴趣的:(前端,javascript,开发语言)