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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import router from './router/index'
import store from '@/store'
// import {validatenull} from '@/util/validate'
import { getParamsByUrl, removeUrlParam } from '@/util/index'
import { getOauth, getUserByToken } from '@/api/auth/auth'
import baseUrl from '@/util/baseUrl'
import { Toast } from 'mint-ui'
// import { wxpermission } from '@/wxpermission'
const defaultUrl = baseUrl.authUrl
router.beforeEach((to, from, next) => {
console.log('缓冲设置')
console.log('code ===' + store.getters.code)
let storecode = store.getters.code
if (!storecode || storecode === '') {
let params = getParamsByUrl(window.location.href)
if (params.code && params.code !== 'undefined') {
store.dispatch('setCode', params.code)
getOauthFn(() => {
next()
})
} else {
getWXOauth()
}
} else {
next()
}
// 判断链接是否被微信分享篡改,false需重定向
// if (redirectUrl()) {
// // store.dispatch('setToken', '')
// // store.dispatch('setUserId', '')
// // let accessToken = store.getters.access_token
// let storecode = store.getters.code
// if (!storecode || storecode === '') {
// let params = getParamsByUrl(window.location.href)
// if (params.code) {
// store.dispatch('setCode', params.code)
// getOauthFn(() => {
// // wxpermission(0, () => {})
// next()
// })
// } else {
// getWXOauth()
// }
// } else {
// // wxpermission(0, () => {})
// next()
// }
// } else {
// if (globalNewUrl !== '') {
// window.location.href = globalNewUrl
// }
// }
// store.dispatch('setToken', '490cd9b7-dca7-4cc3-87e9-5eacbb5a5d77')
})
let getWXOauth = () => {
let url = removeUrlParam('code')
// store.dispatch('setCode', '')
let authId = ''
if (getParamsByUrl(window.location.href).auth_id) {
authId = '&auth_id=' + getParamsByUrl(window.location.href).auth_id
store.dispatch('setAuthId', getParamsByUrl(window.location.href).auth_id)
}
// window.location.reload(defaultUrl + '/wxoauth/route?redirect_uri=' + encodeURIComponent(url) + authId + '&auth_scene=voucher&v=' + timeStamp)
window.location.href = defaultUrl + '/wxoauth/route?redirect_uri=' + encodeURIComponent(url) + authId + '&auth_scene=voucher'
}
let getOauthFn = (callback) => {
if (getParamsByUrl(window.location.href).auth_id) {
store.dispatch('setAuthId', getParamsByUrl(window.location.href).auth_id)
}
let spoauthParams = {
code: store.getters.code
}
getOauth(spoauthParams).then(function (res) {
if (res.token && res.token !== '') {
getUserByTokenFn(res.token, store.getters.code, callback)
} else {
store.dispatch('setCode', '')
setTimeout(function () {
getWXOauth(() => {
callback()
})
}, 3 * 1000)
Toast({
message: '微信token已失效!',
position: 'center',
duration: 3 * 1000
})
}
}).catch((error) => {
console.log(error)
Toast({
message: '鉴权失败',
position: 'center',
duration: 3 * 1000
})
// setTimeout(function () {
// getWXOauth()
// }, 3 * 1000)
})
}
const getUserByTokenFn = (token, code, callback) => {
let spoauthParams = {
token: token
}
getUserByToken(spoauthParams).then(function (res) {
let accessToken = ''
let userId = ''
let accountId = res.accountid
if (accountId && accountId !== '') {
store.dispatch('setAccountId', accountId)
} else {
Toast({
message: 'account id不能为空!',
position: 'center',
duration: 3 * 1000
})
}
if (res.ext && res.ext !== '') {
let extOBJ = JSON.parse(res.ext)
accessToken = extOBJ.access_token
userId = extOBJ.user_id
}
if (accessToken !== '') {
if (userId !== '') {
store.dispatch('setToken', accessToken)
store.dispatch('setUserId', userId)
store.dispatch('setCode', code)
callback()
} else {
setTimeout(function () {
getWXOauth(() => {
callback()
})
}, 3 * 1000)
Toast({
message: 'user id不能为空!',
position: 'center',
duration: 3 * 1000
})
}
} else {
setTimeout(function () {
getWXOauth(() => {
callback()
})
}, 3 * 1000)
Toast({
message: 'token不能为空!',
position: 'center',
duration: 3 * 1000
})
}
}).catch((error) => {
console.log(error)
Toast({
message: '鉴权失败',
position: 'center',
duration: 3 * 1000
})
setTimeout(function () {
getWXOauth(() => {
callback()
})
}, 3 * 1000)
})
}
// let globalNewUrl = ''
// 判断链接是否被微信分享篡改
// const redirectUrl = () => {
// const curUrl = window.location.href
// const curUrlArr = curUrl.split('?')
// // Toast({
// // message: curUrl,
// // position: 'center',
// // duration: 2 * 1000
// // })
// // console.log(curUrlArr[1].indexOf('from=singlemessage'))
// if (curUrlArr.length === 3 && curUrlArr[1].indexOf('from=singlemessage') >= 0) {
// let newUrl = curUrlArr[0] + '#' + curUrlArr[1].split('#')[1] + '?' + curUrlArr[2]
// // Toast({
// // message: curUrlArr[2],
// // position: 'center',
// // duration: 5 * 1000
// // })
// globalNewUrl = newUrl
// // window.location.href = newUrl
// return false
// } else if (curUrl.indexOf('?scene=0') >= 0) {
// let newUrl = curUrl.replace('?scene=0', '')
// globalNewUrl = newUrl
// return false
// } else if (curUrl.indexOf('/voucher/?') >= 0) {
// const urlArr = curUrl.split('/voucher/?')
// const locationStr = urlArr[0]
// const pathArr = urlArr[1].split('#')
// let newUrl = locationStr + '/voucher/#' + pathArr[1]
// globalNewUrl = newUrl
// return false
// } else {
// return true
// }
// }
// const nextToPath = (to, from, next) => {
// let toquery = to.query
// if (from.query.code) {
// toquery.code = from.query.code
// }
// next({
// path: to.path,
// query: toquery
// })
// }