index.js
2.24 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
import Vuex from 'vuex'
const createStore = () => {
return new Vuex.Store({
state: {
firstUrl: '',
token: "",
redirect: "",
user: {},
index: {
tab: 'borrow',
height: 0,
},
snackbar: {
display: false,
msg: '',
redirect: ''
}
},
mutations: {
updateUserToken (state, token) {
state.token = token
},
createUserInfo (state, info) {
state.user = Object.assign({}, info)
},
updateUser (state, info) {
let oldInfo = Object.assign({}, state.user)
state.user = Object.assign(oldInfo, info)
},
displayMessage(state, msg) {
state.snackbar.msg = msg
state.snackbar.display = true
},
hideMessage(state) {
state.snackbar.display = false
},
setRedirect(state, url) {
state.redirect = url
},
cleanRedirect(state) {
state.redirect = ''
},
changeIndexTab(state, type) {
state.index.tab = type
},
updateFirstUrl(state, url) {
state.firstUrl = url
},
setMessageRedirect(state, redirect) {
state.snackbar.redirect = redirect
}
},
actions: {
async nuxtServerInit ({ commit }, { req, res, app, query, redirect, store, params, route }) {
console.log(route)
store.commit('updateFirstUrl', `https://www.51liuliang.cc/netiou${route.fullPath}`)
if (!store.state.user.id || !store.state.token) {
let { data, headers } = await app.$axios.get(`/user?code=${query.code || ''}`)
if (!data.success) {
console.log('redirect')
redirect(301, data.redirect)
} else {
res.cookie('qicaidai_token', data.token, {maxAge: 2592000000, signed: true, httpOnly: true})
store.commit('updateUserToken', data.token)
store.commit('createUserInfo', data.user)
}
}
},
displayMessage({ commit }, msg) {
commit('displayMessage', msg)
},
hideMessage({ commit }) {
commit('hideMessage')
},
setMessageRedirect({ commit }, redirect) {
commit('setMessageRedirect', redirect)
}
}
})
}
export default createStore