jsy-app/store/modules/user.js
2024-09-18 10:30:10 +08:00

177 lines
3.7 KiB
JavaScript

import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
login,
getCodePhone,
phoneLogin,
getInfo,
userRoles,
logout
} from '@/api/login'
import {
getToken,
setToken,
removeToken
} from '@/utils/auth'
import defaultAvatar from "../../static/images/login/userAvatar.png"
const user = {
state: {
token: getToken(),
name: storage.get(constant.name),
avatar: storage.get(constant.avatar),
roles: storage.get(constant.roles),
permissions: storage.get(constant.permissions),
company: storage.get(constant.company),
user: storage.get(constant.user)
},
mutations: {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
storage.set(constant.name, name)
},
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_COMPANY: (state, company) => {
state.company = company
storage.set(constant.company, company)
},
SET_USER: (state, user) => {
state.user = user
storage.set(constant.user, user)
}
},
actions: {
// 登录
Login({
commit
}, userInfo) {
const loginName = userInfo.loginName
const password = userInfo.password
const code = userInfo.code
const uuid = userInfo.uuid
return new Promise((resolve, reject) => {
login(loginName, password, code, uuid)
.then(res => {
setToken(res.token)
commit("SET_TOKEN", res.token)
resolve(res)
})
.catch(error => {
reject(error)
})
})
},
//获取验证码
getCodePhone({}, data) {
return new Promise((resolve, reject) => {
getCodePhone(data)
.then(res => {
resolve(res)
})
.catch(error => {
reject(error)
})
})
},
//手机验证码登录
phoneLogin({
commit
}, data) {
return new Promise((resolve, reject) => {
phoneLogin(data)
.then(res => {
setToken(res.token)
commit("SET_TOKEN", res.token)
resolve(res)
})
.catch(error => {
reject(error)
})
})
},
// 获取用户信息
GetInfo({
commit,
state
}) {
return new Promise((resolve, reject) => {
getInfo()
.then(res => {
const user = res.user
const company = res.user.company
const avatar = user == null || user.avatar == "" || user.avatar == null ?
defaultAvatar : user.avatar
const username = user == null || user.userName == "" || user.userName == null ?
"" : user.userName
commit("SET_USER", user)
commit("SET_PERMISSIONS", res.permissions)
commit("SET_NAME", username)
commit("SET_AVATAR", avatar)
commit("SET_COMPANY", company)
resolve(res)
})
.catch(error => {
reject(error)
})
})
},
// 退出系统
LogOut({
commit,
state
}) {
return new Promise((resolve, reject) => {
logout(state.token)
.then(() => {
commit("SET_TOKEN", "")
commit("SET_ROLES", [])
commit("SET_PERMISSIONS", [])
commit("SET_COMPANY", [])
commit("SET_USER", [])
removeToken()
storage.clean()
resolve()
})
.catch(error => {
reject(error)
})
})
},
//用户菜单权限
getUserRoles({
commit,
state
}) {
return new Promise((resolve, reject) => {
userRoles()
.then(res => {
commit("SET_ROLES", res.data)
resolve()
})
.catch(error => {
reject(error)
})
})
}
}
}
export default user