xsh_1997 3 dagar sedan
förälder
incheckning
99fce2158a

+ 1 - 3
ruoyi-ui-app/App.vue

@@ -1,14 +1,12 @@
1 1
 <script>
2 2
 import { syncUserOnLaunch } from '@/permission'
3
-import { installTabBarApiGuard } from '@/utils/tabBar'
4 3
 
5 4
 export default {
6 5
   onLaunch() {
7
-    installTabBarApiGuard()
8 6
     syncUserOnLaunch().catch(() => {})
9 7
   },
10 8
   onShow() {
11
-    installTabBarApiGuard()
9
+    console.log('App Show')
12 10
   },
13 11
   onHide() {
14 12
     console.log('App Hide')

+ 1 - 1
ruoyi-ui-app/main.js

@@ -1,5 +1,5 @@
1
-import '@/utils/tabBar'
2 1
 import './uni.promisify.adaptor'
2
+import '@/utils/tabBar'
3 3
 import App from './App'
4 4
 
5 5
 // #ifndef VUE3

+ 10 - 23
ruoyi-ui-app/mixins/tabPage.js

@@ -1,4 +1,4 @@
1
-import { ensureTabBarEntry, installTabBarApiGuard, isTabBarPage, syncTabBarText } from '@/utils/tabBar'
1
+import { isTabBarPage, syncTabBarText } from '@/utils/tabBar'
2 2
 
3 3
 /**
4 4
  * Tab 子页共用:根节点语言 class、导航标题、底部 tab 文案与 i18n 同步
@@ -19,30 +19,17 @@ export default {
19 19
     }
20 20
   },
21 21
   onShow() {
22
-    installTabBarApiGuard()
23
-    const applyTabPageUi = () => {
24
-      if (!isTabBarPage()) {
25
-        return
26
-      }
27
-      syncTabBarText((k) => this.$t(k))
28
-      const key = this.navTitleKey
29
-      if (key) {
30
-        const p = uni.setNavigationBarTitle({
31
-          title: this.$t(key)
32
-        })
33
-        if (p && typeof p.catch === 'function') {
34
-          p.catch(() => {})
35
-        }
36
-      }
37
-    }
38 22
     if (isTabBarPage()) {
39
-      applyTabPageUi()
40
-      return
23
+      syncTabBarText((k) => this.$t(k))
41 24
     }
42
-    ensureTabBarEntry().then((ok) => {
43
-      if (ok) {
44
-        applyTabPageUi()
25
+    const key = this.navTitleKey
26
+    if (key) {
27
+      const p = uni.setNavigationBarTitle({
28
+        title: this.$t(key)
29
+      })
30
+      if (p && typeof p.catch === 'function') {
31
+        p.catch(() => {})
45 32
       }
46
-    })
33
+    }
47 34
   }
48 35
 }

+ 8 - 19
ruoyi-ui-app/pages/mine/index.vue

@@ -69,7 +69,7 @@ import UIcon from 'uview-plus/components/u-icon/u-icon.vue'
69 69
 import UButton from 'uview-plus/components/u-button/u-button.vue'
70 70
 import tabPage from '@/mixins/tabPage'
71 71
 import { setStoredLocale } from '@/utils/locale'
72
-import { ensureTabBarEntry, isTabBarPage, syncTabBarText } from '@/utils/tabBar'
72
+import { isTabBarPage, syncTabBarText } from '@/utils/tabBar'
73 73
 import { useUserStore } from '@/store/user'
74 74
 
75 75
 export default {
@@ -168,27 +168,16 @@ export default {
168 168
       this.$i18n.locale = next
169 169
       this.layoutKey += 1
170 170
       this.$nextTick(() => {
171
-        const applyLangUi = () => {
172
-          if (!isTabBarPage()) {
173
-            return
174
-          }
175
-          syncTabBarText((k) => this.$t(k))
176
-          const p = uni.setNavigationBarTitle({
177
-            title: this.$t(this.navTitleKey)
178
-          })
179
-          if (p && typeof p.catch === 'function') {
180
-            p.catch(() => {})
181
-          }
182
-        }
183
-        if (isTabBarPage()) {
184
-          applyLangUi()
171
+        if (!isTabBarPage()) {
185 172
           return
186 173
         }
187
-        ensureTabBarEntry().then((ok) => {
188
-          if (ok) {
189
-            applyLangUi()
190
-          }
174
+        syncTabBarText((k) => this.$t(k))
175
+        const p = uni.setNavigationBarTitle({
176
+          title: this.$t(this.navTitleKey)
191 177
         })
178
+        if (p && typeof p.catch === 'function') {
179
+          p.catch(() => {})
180
+        }
192 181
       })
193 182
     }
194 183
   }

+ 8 - 177
ruoyi-ui-app/utils/tabBar.js

@@ -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
 /**