|
|
@@ -8,7 +8,6 @@ const TAB_PAGE_PATHS = new Set([
|
|
8
|
8
|
'pages/mine/index'
|
|
9
|
9
|
])
|
|
10
|
10
|
|
|
11
|
|
-/** 运行时会改 TabBar 的 uni API(非 Tab 页调用会报 not TabBar page) */
|
|
12
|
11
|
const TABBAR_API_NAMES = [
|
|
13
|
12
|
'setTabBarStyle',
|
|
14
|
13
|
'setTabBarItem',
|
|
|
@@ -21,11 +20,6 @@ const TABBAR_API_NAMES = [
|
|
21
|
20
|
'setTabBarMidButton'
|
|
22
|
21
|
]
|
|
23
|
22
|
|
|
24
|
|
-/** 首次安装时缓存的原始 API(避免重复 bind 已包装函数) */
|
|
25
|
|
-const TABBAR_RAW_APIS = {}
|
|
26
|
|
-
|
|
27
|
|
-let tabBarEntryTask = null
|
|
28
|
|
-
|
|
29
|
23
|
function readUniTabPagePaths() {
|
|
30
|
24
|
try {
|
|
31
|
25
|
const list =
|
|
|
@@ -78,173 +72,20 @@ function normalizePageRoute(route) {
|
|
78
|
72
|
return r
|
|
79
|
73
|
}
|
|
80
|
74
|
|
|
81
|
|
-function getCurrentPageRoute() {
|
|
82
|
|
- const stack = getCurrentPages()
|
|
83
|
|
- if (stack.length) {
|
|
84
|
|
- const cur = stack[stack.length - 1]
|
|
85
|
|
- return cur.route || cur.$page?.route || cur.$page?.fullPath || ''
|
|
86
|
|
- }
|
|
87
|
|
- if (typeof window !== 'undefined' && window.location && window.location.pathname) {
|
|
88
|
|
- return window.location.pathname
|
|
89
|
|
- }
|
|
90
|
|
- return ''
|
|
91
|
|
-}
|
|
92
|
|
-
|
|
93
|
|
-function getCurrentPageMeta() {
|
|
94
|
|
- const stack = getCurrentPages()
|
|
95
|
|
- if (!stack.length) {
|
|
96
|
|
- return null
|
|
97
|
|
- }
|
|
98
|
|
- const cur = stack[stack.length - 1]
|
|
99
|
|
- return cur.$page?.meta || cur.$vm?.$page?.meta || null
|
|
100
|
|
-}
|
|
101
|
|
-
|
|
102
|
|
-/**
|
|
103
|
|
- * 路由是否对应 tabBar.list 中的页面(不等于当前栈顶一定是 Tab 上下文)
|
|
104
|
|
- */
|
|
105
|
|
-export function isTabBarRoute(route) {
|
|
106
|
|
- const normalized = normalizePageRoute(route || getCurrentPageRoute())
|
|
107
|
|
- return !!normalized && TAB_PAGE_PATHS.has(normalized)
|
|
108
|
|
-}
|
|
109
|
|
-
|
|
110
|
75
|
/**
|
|
111
|
|
- * 当前栈顶是否为原生 TabBar 页
|
|
112
|
|
- * 须同时满足:meta.isTabBar === true 且 route 在 tabBar.list 内
|
|
113
|
|
- * (仅 meta 或仅 route 在 H5 生产环境都可能误判,导致 setTabBarStyle:fail)
|
|
|
76
|
+ * 当前栈顶是否为 TabBar 页(与 ruoyi-jiaoyi 一致:按 route 判断,非 Tab 页拦截全部 TabBar API)
|
|
114
|
77
|
*/
|
|
115
|
78
|
export function isTabBarPage() {
|
|
116
|
|
- if (!isTabBarRoute()) {
|
|
|
79
|
+ const stack = getCurrentPages()
|
|
|
80
|
+ if (!stack.length) {
|
|
117
|
81
|
return false
|
|
118
|
82
|
}
|
|
119
|
|
- const meta = getCurrentPageMeta()
|
|
120
|
|
- return !!(meta && meta.isTabBar === true)
|
|
121
|
|
-}
|
|
122
|
|
-
|
|
123
|
|
-/**
|
|
124
|
|
- * H5 生产环境直开/刷新 Tab URL 时,用 switchTab 切回真正的 Tab 上下文
|
|
125
|
|
- * @returns {Promise<boolean>} 切换后是否为 TabBar 页
|
|
126
|
|
- */
|
|
127
|
|
-export function ensureTabBarEntry() {
|
|
128
|
|
- if (isTabBarPage()) {
|
|
129
|
|
- return Promise.resolve(true)
|
|
130
|
|
- }
|
|
131
|
|
- if (!isTabBarRoute()) {
|
|
132
|
|
- return Promise.resolve(false)
|
|
133
|
|
- }
|
|
134
|
|
- if (tabBarEntryTask) {
|
|
135
|
|
- return tabBarEntryTask
|
|
136
|
|
- }
|
|
137
|
|
- const route = normalizePageRoute(getCurrentPageRoute())
|
|
138
|
|
- tabBarEntryTask = new Promise((resolve) => {
|
|
139
|
|
- uni.switchTab({
|
|
140
|
|
- url: `/${route}`,
|
|
141
|
|
- complete: () => {
|
|
142
|
|
- tabBarEntryTask = null
|
|
143
|
|
- resolve(isTabBarPage())
|
|
144
|
|
- }
|
|
145
|
|
- })
|
|
146
|
|
- })
|
|
147
|
|
- return tabBarEntryTask
|
|
148
|
|
-}
|
|
149
|
|
-
|
|
150
|
|
-function noopTabBarResult(name) {
|
|
151
|
|
- return { errMsg: `${name}:ok` }
|
|
152
|
|
-}
|
|
153
|
|
-
|
|
154
|
|
-function finishNoopTabBarCall(name, options) {
|
|
155
|
|
- const result = noopTabBarResult(name)
|
|
156
|
|
- if (options && typeof options.success === 'function') {
|
|
157
|
|
- options.success(result)
|
|
158
|
|
- }
|
|
159
|
|
- if (options && typeof options.complete === 'function') {
|
|
160
|
|
- options.complete(result)
|
|
161
|
|
- }
|
|
162
|
|
- return Promise.resolve(result)
|
|
163
|
|
-}
|
|
164
|
|
-
|
|
165
|
|
-function isTabBarFailMsg(msg) {
|
|
166
|
|
- return String(msg || '').includes('TabBar') && String(msg || '').includes('fail')
|
|
167
|
|
-}
|
|
168
|
|
-
|
|
169
|
|
-function rememberRawTabBarApi(target, name) {
|
|
170
|
|
- if (TABBAR_RAW_APIS[name]) {
|
|
171
|
|
- return TABBAR_RAW_APIS[name]
|
|
172
|
|
- }
|
|
173
|
|
- const fn = target[name]
|
|
174
|
|
- if (typeof fn !== 'function') {
|
|
175
|
|
- return null
|
|
176
|
|
- }
|
|
177
|
|
- if (fn._tabBarGuardRaw) {
|
|
178
|
|
- TABBAR_RAW_APIS[name] = fn._tabBarGuardRaw
|
|
179
|
|
- return TABBAR_RAW_APIS[name]
|
|
180
|
|
- }
|
|
181
|
|
- TABBAR_RAW_APIS[name] = fn.bind(target)
|
|
182
|
|
- return TABBAR_RAW_APIS[name]
|
|
183
|
|
-}
|
|
184
|
|
-
|
|
185
|
|
-/**
|
|
186
|
|
- * H5 生产包中框架 / uview 可能在我们安装守卫后又写回 uni API,故每次启动 / 切页都重装
|
|
187
|
|
- */
|
|
188
|
|
-function wrapTabBarApi(target, name) {
|
|
189
|
|
- if (!target || typeof target[name] !== 'function') {
|
|
190
|
|
- return
|
|
191
|
|
- }
|
|
192
|
|
- const raw = rememberRawTabBarApi(target, name)
|
|
193
|
|
- if (!raw) {
|
|
194
|
|
- return
|
|
195
|
|
- }
|
|
196
|
|
- function patchedTabBarApi(options) {
|
|
197
|
|
- const opts = options || {}
|
|
198
|
|
- if (!isTabBarPage()) {
|
|
199
|
|
- return finishNoopTabBarCall(name, opts)
|
|
200
|
|
- }
|
|
201
|
|
- try {
|
|
202
|
|
- const ret = raw(opts)
|
|
203
|
|
- if (ret && typeof ret.catch === 'function') {
|
|
204
|
|
- ret.catch((err) => {
|
|
205
|
|
- if (isTabBarFailMsg(err && err.errMsg)) {
|
|
206
|
|
- return finishNoopTabBarCall(name, opts)
|
|
207
|
|
- }
|
|
208
|
|
- throw err
|
|
209
|
|
- })
|
|
210
|
|
- }
|
|
211
|
|
- if (ret && typeof ret.then === 'function') {
|
|
212
|
|
- return ret.then(
|
|
213
|
|
- (res) => {
|
|
214
|
|
- if (res && isTabBarFailMsg(res.errMsg)) {
|
|
215
|
|
- return noopTabBarResult(name)
|
|
216
|
|
- }
|
|
217
|
|
- return res
|
|
218
|
|
- },
|
|
219
|
|
- (err) => {
|
|
220
|
|
- if (isTabBarFailMsg(err && err.errMsg)) {
|
|
221
|
|
- return noopTabBarResult(name)
|
|
222
|
|
- }
|
|
223
|
|
- throw err
|
|
224
|
|
- }
|
|
225
|
|
- )
|
|
226
|
|
- }
|
|
227
|
|
- return ret
|
|
228
|
|
- } catch (err) {
|
|
229
|
|
- if (isTabBarFailMsg(err && err.errMsg)) {
|
|
230
|
|
- return finishNoopTabBarCall(name, opts)
|
|
231
|
|
- }
|
|
232
|
|
- throw err
|
|
233
|
|
- }
|
|
234
|
|
- }
|
|
235
|
|
- patchedTabBarApi._tabBarGuardPatched = true
|
|
236
|
|
- patchedTabBarApi._tabBarGuardRaw = raw
|
|
237
|
|
- target[name] = patchedTabBarApi
|
|
238
|
|
-}
|
|
239
|
|
-
|
|
240
|
|
-function patchTabBarApis(target) {
|
|
241
|
|
- if (!target) {
|
|
242
|
|
- return
|
|
243
|
|
- }
|
|
244
|
|
- TABBAR_API_NAMES.forEach((name) => wrapTabBarApi(target, name))
|
|
|
83
|
+ const cur = stack[stack.length - 1]
|
|
|
84
|
+ const route = normalizePageRoute(cur.route || cur.$page?.route || '')
|
|
|
85
|
+ return TAB_PAGE_PATHS.has(route)
|
|
245
|
86
|
}
|
|
246
|
87
|
|
|
247
|
|
-function installTabBarInterceptors() {
|
|
|
88
|
+function installTabBarApiGuard() {
|
|
248
|
89
|
if (typeof uni === 'undefined' || !uni.addInterceptor) {
|
|
249
|
90
|
return
|
|
250
|
91
|
}
|
|
|
@@ -262,17 +103,7 @@ function installTabBarInterceptors() {
|
|
262
|
103
|
})
|
|
263
|
104
|
}
|
|
264
|
105
|
|
|
265
|
|
-export function installTabBarApiGuard() {
|
|
266
|
|
- readUniTabPagePaths()
|
|
267
|
|
- if (typeof uni !== 'undefined') {
|
|
268
|
|
- patchTabBarApis(uni)
|
|
269
|
|
- installTabBarInterceptors()
|
|
270
|
|
- }
|
|
271
|
|
- if (typeof wx !== 'undefined') {
|
|
272
|
|
- patchTabBarApis(wx)
|
|
273
|
|
- }
|
|
274
|
|
-}
|
|
275
|
|
-
|
|
|
106
|
+readUniTabPagePaths()
|
|
276
|
107
|
installTabBarApiGuard()
|
|
277
|
108
|
|
|
278
|
109
|
/**
|