|
|
@@ -21,11 +21,32 @@ const TABBAR_API_NAMES = [
|
|
21
|
21
|
'setTabBarMidButton'
|
|
22
|
22
|
]
|
|
23
|
23
|
|
|
|
24
|
+function readUniTabPagePaths() {
|
|
|
25
|
+ try {
|
|
|
26
|
+ const list =
|
|
|
27
|
+ (typeof __uniConfig !== 'undefined' &&
|
|
|
28
|
+ __uniConfig.tabBar &&
|
|
|
29
|
+ __uniConfig.tabBar.list) ||
|
|
|
30
|
+ []
|
|
|
31
|
+ list.forEach((item) => {
|
|
|
32
|
+ const path = normalizePageRoute(item.pagePath || '')
|
|
|
33
|
+ if (path) {
|
|
|
34
|
+ TAB_PAGE_PATHS.add(path)
|
|
|
35
|
+ }
|
|
|
36
|
+ })
|
|
|
37
|
+ } catch (e) {
|
|
|
38
|
+ // ignore
|
|
|
39
|
+ }
|
|
|
40
|
+}
|
|
|
41
|
+
|
|
24
|
42
|
function normalizePageRoute(route) {
|
|
25
|
43
|
if (!route) {
|
|
26
|
44
|
return ''
|
|
27
|
45
|
}
|
|
28
|
|
- let r = String(route).replace(/^\//, '')
|
|
|
46
|
+ let r = String(route).split('?')[0].replace(/^\//, '')
|
|
|
47
|
+ if (r.endsWith('.html')) {
|
|
|
48
|
+ r = r.slice(0, -5)
|
|
|
49
|
+ }
|
|
29
|
50
|
const prefixes = ['bqH5/', 'bqjy/']
|
|
30
|
51
|
for (const prefix of prefixes) {
|
|
31
|
52
|
if (r.startsWith(prefix)) {
|
|
|
@@ -35,21 +56,77 @@ function normalizePageRoute(route) {
|
|
35
|
56
|
return r
|
|
36
|
57
|
}
|
|
37
|
58
|
|
|
|
59
|
+function getCurrentPageRoute() {
|
|
|
60
|
+ const stack = getCurrentPages()
|
|
|
61
|
+ if (!stack.length) {
|
|
|
62
|
+ return ''
|
|
|
63
|
+ }
|
|
|
64
|
+ const cur = stack[stack.length - 1]
|
|
|
65
|
+ return cur.route || cur.$page?.route || cur.$page?.fullPath || ''
|
|
|
66
|
+}
|
|
|
67
|
+
|
|
38
|
68
|
/**
|
|
39
|
69
|
* 当前栈顶是否为原生 TabBar 页
|
|
40
|
70
|
*/
|
|
41
|
71
|
export function isTabBarPage() {
|
|
42
|
|
- const stack = getCurrentPages()
|
|
43
|
|
- if (!stack.length) {
|
|
44
|
|
- return false
|
|
|
72
|
+ const route = normalizePageRoute(getCurrentPageRoute())
|
|
|
73
|
+ return !!route && TAB_PAGE_PATHS.has(route)
|
|
|
74
|
+}
|
|
|
75
|
+
|
|
|
76
|
+function noopTabBarResult(name) {
|
|
|
77
|
+ return { errMsg: `${name}:ok` }
|
|
|
78
|
+}
|
|
|
79
|
+
|
|
|
80
|
+function finishNoopTabBarCall(name, options) {
|
|
|
81
|
+ const result = noopTabBarResult(name)
|
|
|
82
|
+ if (options && typeof options.success === 'function') {
|
|
|
83
|
+ options.success(result)
|
|
|
84
|
+ }
|
|
|
85
|
+ if (options && typeof options.complete === 'function') {
|
|
|
86
|
+ options.complete(result)
|
|
|
87
|
+ }
|
|
|
88
|
+ return Promise.resolve(result)
|
|
|
89
|
+}
|
|
|
90
|
+
|
|
|
91
|
+/**
|
|
|
92
|
+ * H5 生产包中 addInterceptor 偶发拦不住框架内部调用,直接包装 uni API 更稳
|
|
|
93
|
+ */
|
|
|
94
|
+function wrapTabBarApi(name) {
|
|
|
95
|
+ if (typeof uni === 'undefined' || typeof uni[name] !== 'function') {
|
|
|
96
|
+ return
|
|
|
97
|
+ }
|
|
|
98
|
+ const raw = uni[name].bind(uni)
|
|
|
99
|
+ uni[name] = function patchedTabBarApi(options) {
|
|
|
100
|
+ const opts = options || {}
|
|
|
101
|
+ if (!isTabBarPage()) {
|
|
|
102
|
+ return finishNoopTabBarCall(name, opts)
|
|
|
103
|
+ }
|
|
|
104
|
+ try {
|
|
|
105
|
+ const ret = raw(opts)
|
|
|
106
|
+ if (ret && typeof ret.catch === 'function') {
|
|
|
107
|
+ ret.catch((err) => {
|
|
|
108
|
+ const msg = err && err.errMsg ? String(err.errMsg) : ''
|
|
|
109
|
+ if (msg.includes('TabBar') && msg.includes('fail')) {
|
|
|
110
|
+ return
|
|
|
111
|
+ }
|
|
|
112
|
+ throw err
|
|
|
113
|
+ })
|
|
|
114
|
+ }
|
|
|
115
|
+ return ret
|
|
|
116
|
+ } catch (err) {
|
|
|
117
|
+ const msg = err && err.errMsg ? String(err.errMsg) : ''
|
|
|
118
|
+ if (msg.includes('TabBar') && msg.includes('fail')) {
|
|
|
119
|
+ return finishNoopTabBarCall(name, opts)
|
|
|
120
|
+ }
|
|
|
121
|
+ throw err
|
|
|
122
|
+ }
|
|
45
|
123
|
}
|
|
46
|
|
- const cur = stack[stack.length - 1]
|
|
47
|
|
- const route = normalizePageRoute(cur.route || '')
|
|
48
|
|
- return TAB_PAGE_PATHS.has(route)
|
|
49
|
124
|
}
|
|
50
|
125
|
|
|
51
|
126
|
function installTabBarApiGuard() {
|
|
52
|
|
- if (typeof uni === 'undefined' || !uni.addInterceptor) {
|
|
|
127
|
+ readUniTabPagePaths()
|
|
|
128
|
+ TABBAR_API_NAMES.forEach(wrapTabBarApi)
|
|
|
129
|
+ if (!uni.addInterceptor) {
|
|
53
|
130
|
return
|
|
54
|
131
|
}
|
|
55
|
132
|
TABBAR_API_NAMES.forEach((name) => {
|
|
|
@@ -77,13 +154,9 @@ export function syncTabBarText(t) {
|
|
77
|
154
|
return
|
|
78
|
155
|
}
|
|
79
|
156
|
TAB_KEYS.forEach((key, index) => {
|
|
80
|
|
- const p = uni.setTabBarItem({
|
|
|
157
|
+ uni.setTabBarItem({
|
|
81
|
158
|
index,
|
|
82
|
|
- text: t(key),
|
|
83
|
|
- fail() {}
|
|
|
159
|
+ text: t(key)
|
|
84
|
160
|
})
|
|
85
|
|
- if (p && typeof p.catch === 'function') {
|
|
86
|
|
- p.catch(() => {})
|
|
87
|
|
- }
|
|
88
|
161
|
})
|
|
89
|
162
|
}
|