index.js
3.18 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import {
Toast,
Notify
} from 'vant'
import store from '@/store'
import { common } from '../api/api'
Vue.use(VueRouter)
const router = new VueRouter({
routes
})
router.beforeEach((to, from, next) => {
let { meta: { authBase } } = to
if (to.name != 'common.phoneLogin') store.commit('jumpPath', to.name)
const params = getParams(window.location.href)
console.log(params.appId)
if (params.appId) sessionStorage.setItem('GL_WX_APPID', params.appId)
if (params.merchantId) sessionStorage.setItem('GL_WX_MERCHANTID', params.merchantId)
window.onMessageFromLoginBack = (type, userId, sessionId) => {
console.log('onMessageFromLoginBack', type, userId, sessionId)
if (type == 0) {
store.commit('reload')
} else {
store.commit('clearSessionId')
next()
}
return
}
if (to.matched.some(recode => recode.meta.wxAuth)) {
const ua = navigator.userAgent.toLowerCase()
if (window.__wxjs_environment === 'miniprogram') {
setUserInfo(params)
next()
} else {
if (ua.match(/MicroMessenger/i) == 'micromessenger') {
let userInfo = sessionStorage.getItem('GL_EQUITY_USER_INFO')
if (userInfo) {
setUserInfo(JSON.parse(userInfo))
next()
} else {
let [redirectUrl] = window.location.href.split('?')
getSessionId(redirectUrl, authBase ? 'snsapi_base' : 'snsapi_userinfo')
}
return
} else {
setUserInfo(params)
next()
}
}
} else {
setUserInfo(params)
next()
}
})
const getSessionId = (redirectUrl, state) => {
const params = getParams(window.location.href)
console.log(params)
if (params.code) {
const appId = sessionStorage.getItem('GL_WX_APPID')
common.getSessionId({ code: params.code, state, appId }).then(res => {
if (res) {
let { code, data, message } = res
if (code == 0) {
setUserInfo(data)
} else {
Notify({ type: 'danger', message })
}
} else {
Notify({ type: 'danger', message: '系统异常' })
}
})
} else {
Toast({
type: 'loading',
message: '加载中...',
forbidClick: true,
duration: 0
})
sessionStorage.setItem('GL_WX_APPID', params.appId)
sessionStorage.setItem('URL_PARAM', JSON.stringify(params))
window.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${params.appId}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=${state}&state=${state}#wechat_redirect`)
}
}
const setUserInfo = params => {
if (params.sessionId) {
store.commit('setUserInfo', params)
}
}
const getParams = str => {
let params = str.split('?')
if (params.length < 2) return {}
let param = params[1].split('&')
let obj = {}
for (let i = 0; i < param.length; i++) {
let paramsA = param[i].split('=')
let key = paramsA[0]
let value = paramsA[1]
obj[key] = value
}
return obj
}
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch(err => err)
}
export default router