前言:
因为若依为了保护用户的隐私所以没有把user_id存入token中所以需要添加一些配置拿取用户user_id,但是若依为大家存储用户名,头像,角色等信息
默认的存储的信息:
拿取这些是不用配置什么的只需要在js中编写拿取代码:
因为拿取的数据是个集合所以在data中定义tokenList集合对象存储所有信息
# 拿取所有的用户信息存储到tokenList集合对象中
this.tokenList = uni.getStorageSync("storage_data") || {};
# 通过点出他的名字拿取vuex_xxx 比如id就是vuex_id
this.username = this.tokenList.vuex_name
1. 开始配置拿取用户user_id操作
1.1 修改user.js页面
state: {
token: getToken(),
name: storage.get(constant.name),
id: storage.get(constant.id), # 添加id
avatar: storage.get(constant.avatar),
roles: storage.get(constant.roles),
permissions: storage.get(constant.permissions)
},
mutations: {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
storage.set(constant.name, name)
},
SET_ID: (state, id) => { # 设置id数据
state.id = id
storage.set(constant.id, id)
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
storage.set(constant.avatar, avatar)
},
SET_ROLES: (state, roles) => {
state.roles = roles
storage.set(constant.roles, roles)
},
SET_PERMISSIONS: (state, permissions) => {
state.permissions = permissions
storage.set(constant.permissions, permissions)
}
},
// 获取用户信息
GetInfo({
commit,
state
}) {
return new Promise((resolve, reject) => {
getInfo().then(res => {
const user = res.user
const avatar = (user == null || user.avatar == "" || user.avatar == null) ? require("@/static/images/profile.jpg") : user.avatar
const username = (user == null || user.userName == "" || user.userName == null) ? "" : user.userName
# 添加userId判读是否为空
const userid = (user == null || user.userId == "" || user.userId == null) ? "" : user.userId
if (res.roles && res.roles.length > 0) {
commit('SET_ROLES', res.roles)
commit('SET_PERMISSIONS', res.permissions)
} else {
commit('SET_ROLES', ['ROLE_DEFAULT'])
}
commit('SET_NAME', username)
# 添加提交userId
commit('SET_ID', userid)
commit('SET_AVATAR', avatar)
resolve(res)
}).catch(error => {
reject(error)
})
})
},
2.2 修改constant.js和storage.js页面
1. 修改 constant.js
添加id:'vuex_id'
2. 修改 storage.js
添加constant.id
然后就可以通过这种方法拿取user_id
# 拿取所有的用户信息存储到tokenList集合对象中
this.tokenList = uni.getStorageSync("storage_data") || {};
# 通过点出他的名字拿取vuex_xxx 比如id就是vuex_id
this.id= this.tokenList.vuex_id