global.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
/*
* @Descripttion:
* @version: 1.0.0
* @Author: limin
* @Date: 2021-06-22 20:10:29
* @LastEditors: limin
* @LastEditTime: 2021-07-06 12:39:29
* @FilePath: \gh-hall\src\mixins\global.js
*/
import store from '@/store'
import api from '@/api'
export default {
data() {
return {}
},
computed: {
isSubmit() {
return store.state.app.isSubmit
}
},
beforeRouteEnter(to, from, next) {
console.log('=============')
const {
meta: { auth, employeeType }
} = to
setKeepAlives(to, from)
if (store.state.app.user) {
if (to.name == 'SalesCustomerDetail' && store.state.app.isDennied) {
return next({ name: 'PermissionDennied' })
}
return next()
}
if (auth) {
const code = qs('code')
if (code) {
store
.dispatch('login', { code, type: employeeType })
.then(res => {
if (to.name == 'SalesCustomerDetail') {
api.user
.customerDetailPermission()
.then(({ code, data }) => {
if (code == 200 && data == true) {
next()
} else {
throw new Error('PermissionDennied')
}
})
.catch(err => {
store.dispatch('setIsDennied', true)
next({ name: 'PermissionDennied' })
})
// .catch(err => next())
} else {
next()
}
})
.catch(res => next(false))
} else {
next(false)
}
} else {
next()
}
}
}
function setKeepAlives(to, from) {
const isBack = to.meta.deepth < from.meta.deepth
const tranName = `slide-${{ true: 'right', false: 'left' }[isBack]}`
store.dispatch('setTransitionName', tranName)
// 如果要to(进入)的页面是需要keepAlive缓存的,把name push进keepAlives中
if (to.meta.keepAlive) {
store.dispatch('pushKeep', to.name)
}
// 如果 要 form(离开) 的页面是 keepAlive缓存的,
// 再根据 deepth 来判断是前进还是返回
// 如果是返回删除之前的keepAlive缓存:
if (from.meta.keepAlive && isBack) {
store.dispatch('removeKeep', [from.name])
}
}
function qs(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
var r = window.location.search.substr(1).match(reg)
if (r != null) return unescape(r[2])
return null
}