From 6ea6d04d080dbcd509405a5fcfe9ec6e2f2a10a6 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 15 Dec 2025 09:22:33 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=A2=9E=E5=8A=A0=20navigateBackP?= =?UTF-8?q?lus=20=E6=96=B9=E6=B3=95=EF=BC=8C=E6=94=AF=E6=8C=81=20fallbackU?= =?UTF-8?q?rl=20=E5=9C=A8=20navigateBack=20=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E8=B7=B3=E8=BD=AC=E5=88=B0=20fallbackUrl=20=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=20feat=EF=BC=9A=E5=B0=86=20navigateBackPlus=20?= =?UTF-8?q?=E6=8E=A5=E5=85=A5=E5=88=B0=20user=20=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages-system/user/detail/index.vue | 3 +- src/pages-system/user/form/index.vue | 3 +- src/pages-system/user/index.vue | 3 +- src/utils/index.ts | 43 ++++++++++++++++++++++---- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/pages-system/user/detail/index.vue b/src/pages-system/user/detail/index.vue index 43c77dd..fb5f48c 100644 --- a/src/pages-system/user/detail/index.vue +++ b/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') } /** 加载用户详情 */ diff --git a/src/pages-system/user/form/index.vue b/src/pages-system/user/form/index.vue index 81404ce..bba06e2 100644 --- a/src/pages-system/user/form/index.vue +++ b/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') } /** 加载用户详情 */ diff --git a/src/pages-system/user/index.vue b/src/pages-system/user/index.vue index aaa4120..3ced734 100644 --- a/src/pages-system/user/index.vue +++ b/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() } /** 查询用户列表 */ diff --git a/src/utils/index.ts b/src/utils/index.ts index e70b9c0..3229424 100644 --- a/src/utils/index.ts +++ b/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 & { $page: Page.PageInstance & { 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 { + } else { uni.navigateBack() } } + +/** + * 增强的返回方法 + * 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 }) + } +}