user.js
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import store from '../index'
import router, { resetRouter } from '@/router'
import { Message } from "element-ui";
import Cookies from 'js-cookie';
const state = {
name: Cookies.get('username'),
avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
introduction: '',
userInfo: null
}
const mutations = {
SET_INTRODUCTION: (state, introduction) => {
state.introduction = introduction
},
SET_NAME: (state, name) => {
state.name = name
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_USERINFO: (state, userInfo) => {
state.userInfo = userInfo
}
}
const actions = {
// user login
login ({ commit }, userInfo) {
const { username, password, code, num, location } = userInfo
return new Promise((resolve, reject) => {
login({ username: username.trim(), password: password, code: code, location: location }).then(response => {
const { result, code, msg } = response
if (code === 200) {
Cookies.set('token', result.token)
commit('SET_USERINFO', result.userInfo)
Cookies.set("userInfo", result.userInfo.id, { expires: 720 })
Cookies.set("username", result.userInfo.username, { expires: 720 })
commit('SET_NAME', result.userInfo.username)
} else {
Message.error(msg);
}
resolve(response)
}).catch(error => {
reject(error)
})
})
},
// user logout
logout ({ commit, state, dispatch }) {
return new Promise((resolve, reject) => {
// logout(state.token).then(() => {
commit('SET_USERINFO', null)
Cookies.remove("userInfo")
Cookies.remove("routerTo")
Cookies.remove('token')
resetRouter()
store.dispatch('permission/resetRoutes')
// reset visited views and cached views
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
dispatch('tagsView/delAllViews', null, { root: true })
resolve()
})
},
// remove token
resetToken ({ commit }) {
return new Promise(resolve => {
commit('SET_USERINFO', null)
Cookies.remove("userInfo")
resolve()
})
},
}
export default {
namespaced: true,
state,
mutations,
actions
}