|
|
|
import addRoutes from '@/libs/route/addRoutes'
|
|
|
|
import Setting from '@/setting'
|
|
|
|
import util from '@/libs/util'
|
|
|
|
import { Message } from 'element-ui'
|
|
|
|
import {post,get} from '@/plugins/requests/index.js'
|
|
|
|
import api from '@/api'
|
|
|
|
import bus from '@/libs/bus'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 用户信息
|
|
|
|
* */
|
|
|
|
export default {
|
|
|
|
namespaced: true,
|
|
|
|
state: {
|
|
|
|
avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
|
|
|
|
token: '',
|
|
|
|
userId: '',
|
|
|
|
roleId: '',
|
|
|
|
userName: '',
|
|
|
|
account: '',
|
|
|
|
phone: '',
|
|
|
|
clientId: '',
|
|
|
|
clientName: '',
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
SET_INFO: (state, info) => {
|
|
|
|
info.avatar && (state.avatar = info.avatar)
|
|
|
|
state.userId = info.userId
|
|
|
|
state.roleId = info.roleId
|
|
|
|
state.userName = info.userName
|
|
|
|
state.account = info.account
|
|
|
|
state.phone = info.phone
|
|
|
|
state.clientId = info.clientId
|
|
|
|
state.clientName = info.clientName
|
|
|
|
},
|
|
|
|
SET_TOKEN: (state, token) => {
|
|
|
|
state.token = token
|
|
|
|
},
|
|
|
|
SET_USERNAME: (state, userName) => {
|
|
|
|
state.userName = userName
|
|
|
|
},
|
|
|
|
SET_AVATAR: (state, avatar) => {
|
|
|
|
state.avatar = avatar
|
|
|
|
},
|
|
|
|
SET_ROLEID: (state, roleId) => {
|
|
|
|
state.roleId = roleId
|
|
|
|
},
|
|
|
|
SET_CLIENTID: (state, clientId) => {
|
|
|
|
state.clientId = clientId
|
|
|
|
},
|
|
|
|
SET_CLIENTNAME: (state, clientName) => {
|
|
|
|
state.clientName = clientName
|
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
login({ state,commit }, userInfo) {
|
|
|
|
const { username, password } = userInfo
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
get(api.logins,{ account: username.trim(), password: password }).then(res => {
|
|
|
|
if(res.success){
|
|
|
|
let user = res.data.userInfo
|
|
|
|
if((user.isPort == 0 || user.isPort == 2) && user.roleId != 4){
|
|
|
|
let routes = res.data.permissions[0].children
|
|
|
|
commit('SET_INFO',{
|
|
|
|
avatar: user.userAvatars,
|
|
|
|
userId: user.userId,
|
|
|
|
roleId: user.roleId,
|
|
|
|
userName: user.userName,
|
|
|
|
account: user.account,
|
|
|
|
phone: user.phone,
|
|
|
|
clientId: user.clientId,
|
|
|
|
clientName: user.clientName,
|
|
|
|
})
|
|
|
|
Setting.dynamicRoute && addRoutes(routes)
|
|
|
|
util.session.set(Setting.usernameKey, user.userName)
|
|
|
|
util.session.set(Setting.storeKey, JSON.stringify(state))
|
|
|
|
util.successMsg('登录成功')
|
|
|
|
resolve()
|
|
|
|
}else{
|
|
|
|
util.errorMsg('该用户没有权限')
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
util.errorMsg(res.message)
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
reject(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
logout({ commit, state, dispatch },router) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
util.session.remove(Setting.usernameKey);
|
|
|
|
util.session.remove(Setting.storeKey);
|
|
|
|
// router.push('/login')
|
|
|
|
location.reload()
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
},
|
|
|
|
setAvatar({ state,commit },avatar) {
|
|
|
|
commit('SET_AVATAR',avatar)
|
|
|
|
let data = {
|
|
|
|
userId: state.userId,
|
|
|
|
userAvatars: avatar
|
|
|
|
}
|
|
|
|
post(api.userinfoUpdate,data).then(res => {}).catch(res => {})
|
|
|
|
},
|
|
|
|
setUserName({ state,commit },userName) {
|
|
|
|
commit('SET_USERNAME',userName)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|