添加poolName
parent
d251987757
commit
87dab58d2f
@ -1,115 +1,116 @@
|
|||||||
import { login, logout, getInfo, refreshToken } from '@/api/login'
|
import { login, logout, getInfo, refreshToken } from '@/api/login'
|
||||||
import { getToken, setToken, setExpiresIn, removeToken } from '@/utils/auth'
|
import { getToken, setToken, setExpiresIn, removeToken } from '@/utils/auth'
|
||||||
|
|
||||||
const user = {
|
const user = {
|
||||||
state: {
|
state: {
|
||||||
token: getToken(),
|
token: getToken(),
|
||||||
name: '',
|
name: '',
|
||||||
avatar: '',
|
avatar: '',
|
||||||
roles: [],
|
roles: [],
|
||||||
permissions: []
|
permissions: []
|
||||||
},
|
},
|
||||||
|
|
||||||
mutations: {
|
mutations: {
|
||||||
SET_TOKEN: (state, token) => {
|
SET_TOKEN: (state, token) => {
|
||||||
state.token = token
|
state.token = token
|
||||||
},
|
},
|
||||||
SET_EXPIRES_IN: (state, time) => {
|
SET_EXPIRES_IN: (state, time) => {
|
||||||
state.expires_in = time
|
state.expires_in = time
|
||||||
},
|
},
|
||||||
SET_NAME: (state, name) => {
|
SET_NAME: (state, name) => {
|
||||||
state.name = name
|
state.name = name
|
||||||
},
|
},
|
||||||
SET_AVATAR: (state, avatar) => {
|
SET_AVATAR: (state, avatar) => {
|
||||||
state.avatar = avatar
|
state.avatar = avatar
|
||||||
},
|
},
|
||||||
SET_ROLES: (state, roles) => {
|
SET_ROLES: (state, roles) => {
|
||||||
state.roles = roles
|
state.roles = roles
|
||||||
},
|
},
|
||||||
SET_PERMISSIONS: (state, permissions) => {
|
SET_PERMISSIONS: (state, permissions) => {
|
||||||
state.permissions = permissions
|
state.permissions = permissions
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
// 登录
|
// 登录
|
||||||
Login({ commit }, userInfo) {
|
Login({ commit }, userInfo) {
|
||||||
const username = userInfo.username.trim()
|
const username = userInfo.username.trim()
|
||||||
const password = userInfo.password
|
const password = userInfo.password
|
||||||
const code = userInfo.code
|
const code = userInfo.code
|
||||||
const uuid = userInfo.uuid
|
const uuid = userInfo.uuid
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
login(username, password, code, uuid).then(res => {
|
login(username, password, code, uuid).then(res => {
|
||||||
let data = res.data
|
let data = res.data
|
||||||
setToken(data.access_token)
|
setToken(data.access_token)
|
||||||
commit('SET_TOKEN', data.access_token)
|
commit('SET_TOKEN', data.access_token)
|
||||||
setExpiresIn(data.expires_in)
|
setExpiresIn(data.expires_in)
|
||||||
commit('SET_EXPIRES_IN', data.expires_in)
|
commit('SET_EXPIRES_IN', data.expires_in)
|
||||||
resolve()
|
resolve()
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
reject(error)
|
reject(error)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
// 获取用户信息
|
// 获取用户信息
|
||||||
GetInfo({ commit, state }) {
|
GetInfo({ commit, state }) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
getInfo().then(res => {
|
getInfo().then(res => {
|
||||||
const user = res.user
|
const user = res.user
|
||||||
const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : user.avatar;
|
const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : user.avatar;
|
||||||
if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
|
if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
|
||||||
commit('SET_ROLES', res.roles)
|
commit('SET_ROLES', res.roles)
|
||||||
commit('SET_PERMISSIONS', res.permissions)
|
commit('SET_PERMISSIONS', res.permissions)
|
||||||
} else {
|
} else {
|
||||||
commit('SET_ROLES', ['ROLE_DEFAULT'])
|
commit('SET_ROLES', ['ROLE_DEFAULT'])
|
||||||
}
|
}
|
||||||
commit('SET_NAME', user.userName)
|
commit('SET_NAME', user.userName)
|
||||||
commit('SET_AVATAR', avatar)
|
commit('SET_AVATAR', avatar)
|
||||||
resolve(res)
|
localStorage.setItem('USER_POOL_NAME_CURRENT',user.poolName)
|
||||||
}).catch(error => {
|
resolve(res)
|
||||||
reject(error)
|
}).catch(error => {
|
||||||
})
|
reject(error)
|
||||||
})
|
})
|
||||||
},
|
})
|
||||||
|
},
|
||||||
// 刷新token
|
|
||||||
RefreshToken({commit, state}) {
|
// 刷新token
|
||||||
return new Promise((resolve, reject) => {
|
RefreshToken({commit, state}) {
|
||||||
refreshToken(state.token).then(res => {
|
return new Promise((resolve, reject) => {
|
||||||
setExpiresIn(res.data)
|
refreshToken(state.token).then(res => {
|
||||||
commit('SET_EXPIRES_IN', res.data)
|
setExpiresIn(res.data)
|
||||||
resolve()
|
commit('SET_EXPIRES_IN', res.data)
|
||||||
}).catch(error => {
|
resolve()
|
||||||
reject(error)
|
}).catch(error => {
|
||||||
})
|
reject(error)
|
||||||
})
|
})
|
||||||
},
|
})
|
||||||
|
},
|
||||||
// 退出系统
|
|
||||||
LogOut({ commit, state }) {
|
// 退出系统
|
||||||
return new Promise((resolve, reject) => {
|
LogOut({ commit, state }) {
|
||||||
logout(state.token).then(() => {
|
return new Promise((resolve, reject) => {
|
||||||
commit('SET_TOKEN', '')
|
logout(state.token).then(() => {
|
||||||
commit('SET_ROLES', [])
|
commit('SET_TOKEN', '')
|
||||||
commit('SET_PERMISSIONS', [])
|
commit('SET_ROLES', [])
|
||||||
removeToken()
|
commit('SET_PERMISSIONS', [])
|
||||||
resolve()
|
removeToken()
|
||||||
}).catch(error => {
|
resolve()
|
||||||
reject(error)
|
}).catch(error => {
|
||||||
})
|
reject(error)
|
||||||
})
|
})
|
||||||
},
|
})
|
||||||
|
},
|
||||||
// 前端 登出
|
|
||||||
FedLogOut({ commit }) {
|
// 前端 登出
|
||||||
return new Promise(resolve => {
|
FedLogOut({ commit }) {
|
||||||
commit('SET_TOKEN', '')
|
return new Promise(resolve => {
|
||||||
removeToken()
|
commit('SET_TOKEN', '')
|
||||||
resolve()
|
removeToken()
|
||||||
})
|
resolve()
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
export default user
|
|
||||||
|
export default user
|
||||||
|
Loading…
Reference in New Issue