unis-oa/frontend/src/composables/useReturnTo.ts

114 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 详情页「返回上一级」的统一解析,配合 utils/returnPath 与 BackButton 使用。
*
* 一个工作详情能被流程监控、我发起的、已办、待阅、工作台、首页待办面板好几个地方点开,
* 返回目标不能写死成某一条列表。这里按三步定:
* 1. 站内来路(进当前页之前那一页),优先回它;
* 2. 直接粘地址、刷新或新开标签页进来时没有来路,先认地址栏里 `?from=xxx` 写明的那条来路
* (列表页跳详情时顺手带上的),再退到调用方声明的备选目标,逐个跳过当前身份没权限的;
* 3. 一个都不可用时给出 null,界面上就不渲染返回按钮,避免出现点了没反应的死按钮。
*/
import { computed, toValue, type ComputedRef, type MaybeRefOrGetter } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRoute, useRouter, type RouteLocationRaw, type RouteLocationResolved, type Router } from 'vue-router'
import { returnPath } from '@/utils/returnPath'
import { useAuthStore } from '@/stores/auth'
/** 备选返回目标:路由名,或者带参数的完整位置。 */
export type ReturnFallback = string | RouteLocationRaw
export interface ReturnToState {
/** 解析出的返回目标;站内无来路且备选都不可用时为 null。 */
target: ComputedRef<RouteLocationRaw | null>
/** 按钮文案:认得出来源就是「返回流程监控」,否则退回通用的「返回」。 */
text: ComputedRef<string>
/** 是否存在可用的返回目标。 */
canGoBack: ComputedRef<boolean>
goBack: () => void
}
/** 缺参数的位置对象会让 resolve 直接抛错,调用方按「这条备选不可用」处理。 */
function resolveSafely(router: Router, location: ReturnFallback): RouteLocationResolved | null {
try {
return router.resolve(location)
} catch {
return null
}
}
/**
* 地址栏里 `?from=xxx` 那一句来路声明 → 路由名。
*
* 只认这一张表里点名的几个值:from 谁都能写进地址栏,认下任意字符串就等于让一个陌生链接
* 把返回按钮指到系统里任何一页。以后别的列表也要带来路,在这里加一行即可。
*/
const DECLARED_ORIGINS: Record<string, string> = {
read: 'my-read'
}
function toList(fallback: ReturnFallback | ReturnFallback[] | undefined): ReturnFallback[] {
if (fallback === undefined) return []
const list = Array.isArray(fallback) ? fallback : [fallback]
// 字符串一律当路由名:直接把它交给 resolve 会被当成相对路径,
// 于是 'my-created' 会撞上 /work/:job 这类带参路由,返回到错的页面。
return list.map((item) => (typeof item === 'string' ? { name: item } : item))
}
export function useReturnTo(
fallback: MaybeRefOrGetter<ReturnFallback | ReturnFallback[] | undefined> = undefined
): ReturnToState {
const router = useRouter()
const route = useRoute()
const auth = useAuthStore()
const { t } = useI18n()
/**
* `?from=read` 声明的那条来路,排在调用方的备选之前:站内来路只在同一次会话里记着,
* 从新标签页打开或刷新之后就没有了,这时不认这一句,返回只能掉回 my-created / 工作台那种
* 默认落点,人等于被踢出自己正在处理的那张列表。
*/
const declared = computed<ReturnFallback[]>(() => {
const name = DECLARED_ORIGINS[String(route.query?.from ?? '')]
return name ? [{ name }] : []
})
const target = computed<RouteLocationRaw | null>(() => {
const origin = returnPath()
if (origin && origin !== route.fullPath) return origin
for (const item of [...declared.value, ...toList(toValue(fallback))]) {
const resolved = resolveSafely(router, item)
if (!resolved || !resolved.matched.length) continue
// 路由 meta.permission 为空表示登录即可;备选指回当前页自己也算不可用。
if (!auth.can(String(resolved.meta?.permission ?? ''))) continue
if (resolved.fullPath !== route.fullPath) return item
}
return null
})
const resolvedTarget = computed(() => (target.value === null ? null : resolveSafely(router, target.value)))
const href = computed(() => resolvedTarget.value?.fullPath ?? '')
const originName = computed(() => {
const key = String(resolvedTarget.value?.meta?.titleKey ?? '')
return key ? t(key) : ''
})
const text = computed(() => (originName.value ? t('common.backTo', { to: originName.value }) : t('common.back')))
const canGoBack = computed(() => target.value !== null)
function goBack() {
const location = target.value
if (!location) return
// 目标正好就是来路时走 router.back(),浏览器的前进后退栈才不会越堆越长。
if (returnPath() === href.value) {
router.back()
return
}
router.push(location)
}
return { target, text, canGoBack, goBack }
}