You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.8 KiB
JavaScript

8 years ago
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
8 years ago
const user = {
state: {
token: getToken(),
8 years ago
name: '',
avatar: '',
8 years ago
roles: []
8 years ago
},
mutations: {
SET_TOKEN: (state, token) => {
8 years ago
state.token = token
8 years ago
},
SET_NAME: (state, name) => {
8 years ago
state.name = name
8 years ago
},
SET_AVATAR: (state, avatar) => {
8 years ago
state.avatar = avatar
8 years ago
},
SET_ROLES: (state, roles) => {
8 years ago
state.roles = roles
8 years ago
}
},
actions: {
8 years ago
// 登录
Login({ commit }, userInfo) {
const username = userInfo.username.trim()
8 years ago
return new Promise((resolve, reject) => {
login(username, userInfo.password).then(response => {
8 years ago
const data = response.data
setToken(data.token)
commit('SET_TOKEN', data.token)
resolve()
8 years ago
}).catch(error => {
8 years ago
reject(error)
})
})
8 years ago
},
// 获取用户信息
GetInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
8 years ago
const data = response.data
commit('SET_ROLES', data.roles)
8 years ago
commit('SET_NAME', data.name)
commit('SET_AVATAR', data.avatar)
resolve(response)
8 years ago
}).catch(error => {
8 years ago
reject(error)
})
})
8 years ago
},
// 登出
LogOut({ commit, state }) {
return new Promise((resolve, reject) => {
logout(state.token).then(() => {
8 years ago
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
removeToken()
resolve()
8 years ago
}).catch(error => {
8 years ago
reject(error)
})
})
8 years ago
},
// 前端 登出
FedLogOut({ commit }) {
return new Promise(resolve => {
8 years ago
commit('SET_TOKEN', '')
removeToken()
resolve()
})
8 years ago
}
}
8 years ago
}
8 years ago
8 years ago
export default user