import config from '@/config' import storage from '@/common/utils/storage' import { login, logout, getPermissionInfo, getTenantInfo } from '@/api/request2' const user = { state: { id: 0, // 用户编号 name: storage.getStorage(storage.constant.name), avatar: storage.getStorage(storage.constant.avatar), roles: storage.getStorage(storage.constant.roles), permissions: storage.getStorage(storage.constant.permissions) }, mutations: { SET_ID: (state, id) => { state.id = id }, SET_NAME: (state, name) => { state.name = name storage.setStorage(storage.constant.name, name) }, SET_AVATAR: (state, avatar) => { state.avatar = avatar storage.setStorage(storage.constant.avatar, avatar) }, SET_ROLES: (state, roles) => { state.roles = roles storage.setStorage(storage.constant.roles, roles) }, SET_PERMISSIONS: (state, permissions) => { state.permissions = permissions storage.setStorage(storage.constant.permissions, permissions) }, }, actions: { // 登录 Login({ commit }, userInfo) { const username = userInfo.username const password = userInfo.password const code = userInfo.code const uuid = userInfo.uuid return new Promise((resolve, reject) => { login(username, password, code, uuid).then(res => { if(res.data){ res = res.data; // 设置 token storage.setStorage(storage.constant.token,res.accessToken) resolve(res) }else { uni.showToast({ title:res.msg }) } }).catch(error => { reject(error) uni.showToast({ title:error }) }) }) }, // 获取用户信息 getPermissionInfo({ commit, state }) { return new Promise((resolve, reject) => { getPermissionInfo().then(res => { res = res.data; // 读取 data 数据 const user = res.user const avatar = (user == null || user.avatar === "" || user.avatar == null) ? require("@/static/images/profile.jpg") : user.avatar const nickname = (user == null || user.nickname === "" || user.nickname == null) ? "" : user.nickname 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', nickname) commit('SET_AVATAR', avatar) commit('SET_ID',user.id) resolve(res) }).catch(error => { reject(error) // uni.showToast({ // title:"用户信息获取失败" // }) }) }) }, // 退出系统 LogOut({ commit, state }) { return new Promise((resolve, reject) => { logout(state.token).then(() => { commit('SET_ROLES', []) commit('SET_PERMISSIONS', []) commit('SET_ID',[]) storage.removeStorage(storage.constant.token) storage.clearStorage() resolve() }).catch(error => { reject(error) }) }) } } } export default user