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.
159 lines
4.9 KiB
159 lines
4.9 KiB
import config from '@/config'
|
|
import storage from '@/utils/storage'
|
|
import constant from '@/utils/constant'
|
|
import { login, logout, getInfo } from '@/api/login'
|
|
import { getToken, setToken, removeToken } from '@/utils/auth'
|
|
|
|
const baseUrl = config.baseUrl
|
|
|
|
const user = {
|
|
state: {
|
|
token: getToken(),
|
|
name: storage.get(constant.name),
|
|
nickName: storage.get(constant.nickName),
|
|
phonenumber: storage.get(constant.phonenumber),
|
|
userId: storage.get(constant.userId),
|
|
avatar: storage.get(constant.avatar),
|
|
roles: storage.get(constant.roles),
|
|
permissions: storage.get(constant.permissions),
|
|
amount: storage.get(constant.amount),
|
|
userCate: storage.get(constant.userCate),
|
|
status: storage.get(constant.status)
|
|
},
|
|
|
|
mutations: {
|
|
SET_TOKEN: (state, token) => {
|
|
state.token = token
|
|
},
|
|
SET_Id: (state, id) => {
|
|
state.id = id
|
|
storage.set(constant.id, id)
|
|
},
|
|
SET_NAME: (state, name) => {
|
|
state.name = name
|
|
storage.set(constant.name, name)
|
|
},
|
|
SET_NICKNAME: (state, nickName) => {
|
|
state.nickName = nickName
|
|
storage.set(constant.nickName, nickName)
|
|
},
|
|
SET_PHONENUMBER: (state, phonenumber) => {
|
|
state.phonenumber = phonenumber
|
|
storage.set(constant.phonenumber, phonenumber)
|
|
},
|
|
SET_USERID: (state, userId) => {
|
|
state.userId = userId
|
|
storage.set(constant.userId, userId)
|
|
},
|
|
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)
|
|
},
|
|
SET_AMOUNT: (state, amount) =>{
|
|
state.amount = amount
|
|
storage.set(constant.amount, amount)
|
|
},
|
|
SET_USERCATE:(state, userCate) => {
|
|
state.userCate = userCate
|
|
storage.set(constant.userCate, userCate)
|
|
},
|
|
SET_STATUS:(state, status)=>{
|
|
state.status = status
|
|
storage.set(constant.status, status)
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
// 登录
|
|
Login({ commit }, userInfo) {
|
|
const username = userInfo.username.trim()
|
|
const password = userInfo.password
|
|
const code = userInfo.code
|
|
const uuid = userInfo.uuid
|
|
return new Promise((resolve, reject) => {
|
|
login(username, password, code, uuid).then(res => {
|
|
setToken(res.data.token)
|
|
commit('SET_TOKEN', res.data.token)
|
|
resolve()
|
|
}).catch(error => {
|
|
reject(error)
|
|
})
|
|
})
|
|
},
|
|
|
|
// 获取用户信息
|
|
GetInfo({ commit, state }) {
|
|
return new Promise((resolve, reject) => {
|
|
getInfo().then(res => {
|
|
const user = res.data.user
|
|
const avatar = (user == null || user.avatar == "" || user.avatar == null) ? (user.gender == 1 ? require("@/static/images/profile_M.jpg") : require("@/static/images/profile.jpg")) : baseUrl + user.avatar
|
|
const username = (user == null || user.name == "" || user.name == null) ? "" : user.name
|
|
const userId = (user == null || user.userId == "" || user.userId == null) ? "" : user.userId
|
|
const nickName = (user == null || user.name == "" || user.name == null) ? "" : user.name
|
|
const phonenumber = (user == null || user.phone == "" || user.phone == null) ? "" : user.phone
|
|
const userCate = (user == null || user.userType == "" || user.userType == null) ? 0 : user.userType
|
|
const status = (user == null || user.status == "" || user.status == null) ? 0 : user.status
|
|
const id = (user == null || user.id == "" || user.id == null) ? "" : user.id
|
|
if (res.data.roles && res.data.roles.length > 0) {
|
|
commit('SET_ROLES', res.roles)
|
|
commit('SET_PERMISSIONS', res.permissions)
|
|
} else {
|
|
commit('SET_ROLES', ['ROLE_DEFAULT'])
|
|
}
|
|
commit('SET_Id', id)
|
|
commit('SET_NAME', username)
|
|
commit('SET_USERID', userId)
|
|
commit('SET_AVATAR', avatar)
|
|
commit('SET_NICKNAME', nickName)
|
|
commit('SET_PHONENUMBER', phonenumber)
|
|
commit('SET_USERCATE', userCate)
|
|
commit('SET_STATUS', status)
|
|
resolve(res)
|
|
}).catch(error => {
|
|
reject(error)
|
|
})
|
|
})
|
|
},
|
|
|
|
// 退出系统
|
|
LogOut({ commit, state }) {
|
|
return new Promise((resolve, reject) => {
|
|
logout(state.token).then(() => {
|
|
uni.showToast({
|
|
title: '已退出登录',
|
|
icon: 'success'
|
|
});
|
|
setTimeout(()=>{
|
|
commit('SET_Id', '')
|
|
commit('SET_TOKEN', '')
|
|
commit('SET_ROLES', [])
|
|
commit('SET_PERMISSIONS', [])
|
|
commit('SET_NAME', '')
|
|
commit('SET_USERID', '')
|
|
commit('SET_AVATAR', '')
|
|
commit('SET_NICKNAME', '')
|
|
commit('SET_PHONENUMBER', '')
|
|
commit('SET_USERCATE', '')
|
|
commit('SET_STATUS', '')
|
|
removeToken()
|
|
storage.clean()
|
|
resolve()
|
|
},1000)
|
|
}).catch(error => {
|
|
reject(error)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
export default user
|
|
|