wxOnAppRoute.js
3.43 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
111
112
113
114
115
116
117
118
119
120
import util from '../utils/util.js'
import { getUserInfo } from '~/utils/cache/userInfo'
import { IMG_URL } from '~/utils/constantData'
/**
* wx.onAppRoute钩子方法,
* 所有的参数都是路由类上的参数
* @param {*} obj.query
* @param {*} obj.routeHooks
* @param {*} obj.routerList
* @param {*} obj.getPageInstance
* @param {*} obj.getRootName
* @param {*} obj.setNavigationBarTitleText
* @param {*} obj.getRouterList
*/
export const wxOnAppRouteFn = obj => {
wx.onAppRoute &&
wx.onAppRoute(e => {
let thisPage = obj.getPageInstance()
const rootName = obj.getRootName()
if (rootName) {
const selectedColor = rootName == 'packageEquity' ? '#f7dcbc' : '#FC5B32'
wx.setTabBarStyle({
selectedColor,
color: '#979899',
backgroundColor: '#FFFFFF',
borderStyle: 'white'
})
}
if (thisPage) {
obj.setNavigationBarTitleText(thisPage)
let onRoute = thisPage.onRoute
thisPage.$query = obj.query
typeof onRoute === 'function' && onRoute(obj.query)
thisPage.rootName = rootName
obj.getRouterList().find(item => {
if (`/${thisPage.__route__}` === item.url) {
const authStatus = wx.$authStatus
// if (item.isTab) thisPage.setData({
// showLogin: authStatus
// })
}
})
if (!wx.$version)
util.getImgVersion().then(({ version }) => {
wx.$version = version || '1.0.0'
thisPage.setData({
img_version: wx.$version
})
})
else
thisPage.setData({
img_version: wx.$version
})
// console.log(app)
thisPage.setData({
IMGURL: IMG_URL
})
let userInfo = getUserInfo()
if (userInfo && userInfo.phone) {
if (wx.$authStatus) {
thisPage.setData({
userInfo: null
})
} else {
thisPage.setData({
userInfo
})
if (thisPage.init) thisPage.init(userInfo)
}
}
}
obj.routeHooks.forEach(fn => {
try {
typeof fn === 'function' && fn(e, obj.query, obj.routerList)
} catch (err) {
console.log(err)
}
})
obj.query = {}
})
}
/**
* 生成特定格式路由
*
* @param {*} routerList 路由参数
* @returns
*/
export const generateRoutList = routerList => {
let _routerList = routerList
try {
__wxConfig &&
__wxConfig.tabBar &&
__wxConfig.tabBar.list &&
(_routerList = routerList.map(item => {
let obj = {}
if (item.parent != '__plugin__') {
obj = {
...item,
isTab: __wxConfig.tabBar.list.some(({ pagePath }) => item.url.includes(pagePath.replace('.html', '')))
}
if (item.parent !== 'pages') {
let parentList = item.url.split(`/`)
if (parentList.length > 5) {
item.url = item.url.replace(/\/__plugin__\/[a-z0-9A-Z]+/, '')
obj.subPages = item.url.split(`/${item.parent}/pages/`)[1].split('/')[0]
}
}
if (obj.isTab) {
__wxConfig.tabBar.list.map(e => {
if (obj.url === `/${e.pagePath.replace('.html', '')}`) obj.rootName = e.rootName
})
}
}
return obj
}))
} catch (err) {}
return _routerList
}