user.js
2.4 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { CRGX_TOKEN, USER_ID } from '@/store/mutation-types'
import storage from '@/utils/storage'
import { userLogin } from '@/api/user.js'
// 登陆成功后执行
const loginSuccess = (commit, token) => {
// 过期时间30天
const expiryTime = 30 * 86400
// 保存tokne和userId到缓存
// storage.set(USER_ID, userId, expiryTime)
storage.set(CRGX_TOKEN, token, expiryTime)
// 记录到store全局变量
commit('SET_TOKEN', token)
// commit('SET_USER_ID', userId)
}
const user = {
state: {
// 用户认证token
token: '',
// 用户ID
userId: null
},
mutations: {
SET_TOKEN: (state, value) => {
state.token = value
},
SET_USER_ID: (state, value) => {
state.userId = value
}
},
actions: {
// 用户登录 (普通登录: 输入手机号和验证码)
Login({ commit }, data) {
return new Promise((resolve, reject) => {
userLogin(data)
.then(response => {
const { token } = response
loginSuccess(commit, token)
resolve(response)
})
.catch(reject)
})
},
// 微信小程序一键授权登录 (获取用户基本信息)
LoginMpWx({ commit }, data) {
return new Promise((resolve, reject) => {
// LoginApi.loginMpWx({ form: data }, { isPrompt: false })
// .then(response => {
// const result = response.data
// loginSuccess(commit, result)
// resolve(response)
// })
// .catch(reject)
})
},
// 微信小程序一键授权登录 (授权手机号)
LoginMpWxMobile({ commit }, data) {
return new Promise((resolve, reject) => {
// LoginApi.loginMpWxMobile({ form: data }, { isPrompt: false })
// .then(response => {
// const result = response.data
// loginSuccess(commit, result)
// resolve(response)
// })
// .catch(reject)
})
},
// 退出登录
Logout({ commit }, data) {
const store = this
return new Promise((resolve, reject) => {
// if (store.getters.userId > 0) {
// // 删除缓存中的tokne和userId
// storage.remove(USER_ID)
// storage.remove(ACCESS_TOKEN)
// // 记录到store全局变量
// commit('SET_TOKEN', '')
// commit('SET_USER_ID', null)
// resolve()
// }
})
}
}
}
export default user