admin-fe/src/store/modules/user.js

134 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-08-23 03:39:16 +00:00
import { loginByUsername, logout, getUserInfo } from 'api/login'
2017-08-22 07:43:34 +00:00
import { getToken, setToken, removeToken } from 'utils/auth'
2017-04-18 07:09:13 +00:00
const user = {
state: {
user: '',
status: '',
code: '',
2017-07-20 05:26:09 +00:00
token: getToken(),
2017-04-18 07:09:13 +00:00
name: '',
avatar: '',
introduction: '',
roles: [],
setting: {
articlePlatform: []
}
},
mutations: {
SET_CODE: (state, code) => {
2017-08-22 07:43:34 +00:00
state.code = code
2017-04-18 07:09:13 +00:00
},
SET_TOKEN: (state, token) => {
2017-08-22 07:43:34 +00:00
state.token = token
2017-04-18 07:09:13 +00:00
},
SET_INTRODUCTION: (state, introduction) => {
2017-08-22 07:43:34 +00:00
state.introduction = introduction
2017-04-18 07:09:13 +00:00
},
SET_SETTING: (state, setting) => {
2017-08-22 07:43:34 +00:00
state.setting = setting
2017-04-18 07:09:13 +00:00
},
SET_STATUS: (state, status) => {
2017-08-22 07:43:34 +00:00
state.status = status
2017-04-18 07:09:13 +00:00
},
SET_NAME: (state, name) => {
2017-08-22 07:43:34 +00:00
state.name = name
2017-04-18 07:09:13 +00:00
},
SET_AVATAR: (state, avatar) => {
2017-08-22 07:43:34 +00:00
state.avatar = avatar
2017-04-18 07:09:13 +00:00
},
SET_ROLES: (state, roles) => {
2017-08-22 07:43:34 +00:00
state.roles = roles
2017-04-18 07:09:13 +00:00
}
},
actions: {
2017-08-22 06:26:19 +00:00
// 用户名登录
LoginByUsername({ commit }, userInfo) {
2017-08-22 07:43:34 +00:00
const username = userInfo.username.trim()
2017-04-18 07:09:13 +00:00
return new Promise((resolve, reject) => {
2017-08-22 06:26:19 +00:00
loginByUsername(username, userInfo.password).then(response => {
2017-08-22 07:43:34 +00:00
const data = response.data
setToken(response.data.token)
commit('SET_TOKEN', data.token)
resolve()
2017-04-24 06:15:42 +00:00
}).catch(error => {
2017-08-22 07:43:34 +00:00
reject(error)
})
})
2017-04-18 07:09:13 +00:00
},
2017-04-24 06:15:42 +00:00
2017-05-16 03:08:17 +00:00
// 获取用户信息
2017-08-23 03:39:16 +00:00
GetUserInfo({ commit, state }) {
2017-04-24 06:15:42 +00:00
return new Promise((resolve, reject) => {
2017-08-23 03:39:16 +00:00
getUserInfo(state.token).then(response => {
2017-08-22 07:43:34 +00:00
const data = response.data
commit('SET_ROLES', data.role)
commit('SET_NAME', data.name)
commit('SET_AVATAR', data.avatar)
commit('SET_INTRODUCTION', data.introduction)
resolve(response)
2017-04-24 06:15:42 +00:00
}).catch(error => {
2017-08-22 07:43:34 +00:00
reject(error)
})
})
2017-04-24 06:15:42 +00:00
},
// 第三方验证登录
2017-08-22 07:43:34 +00:00
// LoginByThirdparty({ commit, state }, code) {
// return new Promise((resolve, reject) => {
// commit('SET_CODE', code)
// loginByThirdparty(state.status, state.email, state.code).then(response => {
// commit('SET_TOKEN', response.data.token)
// setToken(response.data.token)
// resolve()
// }).catch(error => {
// reject(error)
// })
// })
// },
2017-04-24 06:15:42 +00:00
// 登出
2017-04-18 07:09:13 +00:00
LogOut({ commit, state }) {
return new Promise((resolve, reject) => {
2017-04-24 06:15:42 +00:00
logout(state.token).then(() => {
2017-08-22 07:43:34 +00:00
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
removeToken()
resolve()
2017-04-18 07:09:13 +00:00
}).catch(error => {
2017-08-22 07:43:34 +00:00
reject(error)
})
})
2017-04-18 07:09:13 +00:00
},
2017-05-16 03:08:17 +00:00
// 前端 登出
2017-04-18 07:09:13 +00:00
FedLogOut({ commit }) {
return new Promise(resolve => {
2017-08-22 07:43:34 +00:00
commit('SET_TOKEN', '')
removeToken()
resolve()
})
2017-06-06 02:46:20 +00:00
},
// 动态修改权限
ChangeRole({ commit }, role) {
return new Promise(resolve => {
2017-08-22 07:43:34 +00:00
commit('SET_TOKEN', role)
setToken(role)
2017-08-23 03:39:16 +00:00
getUserInfo(role).then(response => {
2017-08-23 03:16:54 +00:00
const data = response.data
commit('SET_ROLES', data.role)
commit('SET_NAME', data.name)
commit('SET_AVATAR', data.avatar)
commit('SET_INTRODUCTION', data.introduction)
resolve()
})
2017-06-06 02:46:20 +00:00
})
2017-04-18 07:09:13 +00:00
}
}
2017-08-22 07:43:34 +00:00
}
2017-04-18 07:09:13 +00:00
2017-08-22 07:43:34 +00:00
export default user