Forráskód Böngészése

feat:增加 navigateBackPlus 方法,支持 fallbackUrl 在 navigateBack 失败时,跳转到 fallbackUrl 地址
feat:将 navigateBackPlus 接入到 user 管理中

YunaiV 4 hónapja
szülő
commit
6ea6d04d08

+ 2 - 1
src/pages-system/user/detail/index.vue

@@ -77,6 +77,7 @@ import { CommonStatusEnum, DICT_TYPE } from '@/utils/constants'
 import { formatDateTime } from '@/utils/date'
 import PasswordForm from './components/password-form.vue'
 import RoleAssignForm from './components/role-assign-form.vue'
+import { navigateBackPlus } from '@/utils';
 
 const props = defineProps<{
   id: number
@@ -116,7 +117,7 @@ const hasMoreActions = computed(() => moreActions.value.length > 0)
 
 /** 返回上一页 */
 function handleBack() {
-  uni.navigateBack()
+  navigateBackPlus('/pages-system/user/index')
 }
 
 /** 加载用户详情 */

+ 2 - 1
src/pages-system/user/form/index.vue

@@ -106,6 +106,7 @@ import { CommonStatusEnum, DICT_TYPE } from '@/utils/constants'
 import { isEmail, isMobile } from '@/utils/validator'
 import DeptPicker from './components/dept-picker.vue'
 import PostPicker from './components/post-picker.vue'
+import { navigateBackPlus } from '@/utils';
 
 const props = defineProps<{
   id?: number
@@ -147,7 +148,7 @@ const formRef = ref()
 
 /** 返回上一页 */
 function handleBack() {
-  uni.navigateBack()
+  navigateBackPlus('/pages-system/user/index')
 }
 
 /** 加载用户详情 */

+ 2 - 1
src/pages-system/user/index.vue

@@ -92,6 +92,7 @@ import { getUserPage } from '@/api/system/user'
 import { useAccess } from '@/hooks/useAccess'
 import { DICT_TYPE } from '@/utils/constants'
 import SearchForm from './components/search-form.vue'
+import { navigateBackPlus } from '@/utils';
 
 definePage({
   style: {
@@ -114,7 +115,7 @@ const queryParams = reactive({
 
 /** 返回上一页 */
 function handleBack() {
-  uni.navigateBack()
+  navigateBackPlus()
 }
 
 /** 查询用户列表 */

+ 36 - 5
src/utils/index.ts

@@ -1,7 +1,6 @@
 import type { PageMetaDatum, SubPackages } from '@uni-helper/vite-plugin-uni-pages'
 import { isMpWeixin } from '@uni-helper/uni-env'
 import { pages, subPackages } from '@/pages.json'
-import { tabbarList } from '@/tabbar/config'
 import { isPageTabbar } from '@/tabbar/store'
 
 export type PageInstance = Page.PageInstance<AnyObject, object> & { $page: Page.PageInstance<AnyObject, object> & { fullPath: string } }
@@ -175,21 +174,53 @@ export const isDoubleTokenMode = import.meta.env.VITE_AUTH_MODE === 'double'
  */
 export const HOME_PAGE = `/${(pages as PageMetaDatum[]).find(page => page.type === 'home')?.path || (pages as PageMetaDatum[])[0].path}`
 
-// TODO @芋艿:这里要不要换成 HOME_PAGE?
 /**
  * 登录成功后跳转
- * @param redirectUrl 重定向地址,为空则跳转到默认首页(tabbar 第一个页面)
+ *
+ * @author 芋艿
+ * @param redirectUrl 重定向地址,为空则跳转到默认首页(HOME_PAGE)
  */
 export function redirectAfterLogin(redirectUrl?: string) {
-  let path = redirectUrl || tabbarList[0].pagePath
+  let path = redirectUrl || HOME_PAGE
   if (!path.startsWith('/')) {
     path = `/${path}`
   }
   const { path: _path } = parseUrlToObj(path)
   if (isPageTabbar(_path)) {
     uni.switchTab({ url: path })
+  } else {
+    uni.navigateBack()
   }
-  else {
+}
+
+/**
+ * 增强的返回方法
+ * 1. 如果存在上一页,则返回上一页
+ * 2. 如果不存在上一页,则跳转到传入的 fallbackUrl 地址
+ * 3. 如果 fallbackUrl 也不存在,则跳转到首页
+ *
+ * @author 芋艿
+ * @param fallbackUrl 备选跳转地址,当不存在上一页时使用
+ */
+export function navigateBackPlus(fallbackUrl?: string) {
+  const pages = getCurrentPages()
+  // 情况一:如果存在上一页(页面栈长度大于 1),则直接返回
+  if (pages.length > 1) {
     uni.navigateBack()
+    return
+  }
+
+  // 情况二 + 三:不存在上一页,尝试跳转到传入的 fallbackUrl
+  let targetUrl = fallbackUrl || HOME_PAGE
+  // 确保路径以 / 开头
+  if (!targetUrl.startsWith('/')) {
+    targetUrl = `/${targetUrl}`
+  }
+  // 解析路径,判断是否是 tabbar 页面
+  const { path } = parseUrlToObj(targetUrl)
+  if (isPageTabbar(path)) {
+    uni.switchTab({ url: targetUrl })
+  } else {
+    uni.reLaunch({ url: targetUrl })
   }
 }