Files
reader-star/apps/admin/src/hooks/common/router.ts
rjl 11fb5481d3 feat(competition): 支持决赛环节并添加加时赛管理功能
- 扩展竞赛环节类型,支持决赛环节(RoundType=3)
- 新增加时赛创建功能,支持多分组队伍选择
- 添加题目库禁用/启用功能,支持批量操作
- 优化用户端竞赛流程,区分常规赛和加时赛
- 重构操作按钮组件,提高代码复用性
2026-03-09 17:34:40 +08:00

147 lines
3.5 KiB
TypeScript

import type { RouteKey } from '@elegant-router/types'
import type { RouteLocationRaw } from 'vue-router'
import { useRouter } from 'vue-router'
import { router as globalRouter } from '@/router'
import { useRouteStore } from '@/store/modules/route'
/**
* 路由跳转
*
* 跳转到指定路由,可替换函数 router.push
*
* @param inSetup 是否在 vue script setup 中
*/
export function useRouterPush(inSetup = true) {
const router = inSetup ? useRouter() : globalRouter
const route = globalRouter.currentRoute
const routerPush = router.push
const routerBack = router.back
/**
* 路由跳转
*
* @param key 路由名称
* @param options 路由参数
*/
async function routerPushByKey(key: RouteKey, options?: App.Global.RouterPushOptions, newWindow?: boolean) {
const { query, params } = options || {}
const routeLocation: RouteLocationRaw = {
name: key,
}
if (Object.keys(query || {}).length) {
routeLocation.query = query
}
if (Object.keys(params || {}).length) {
routeLocation.params = params
}
if (newWindow) {
const routeData = router.resolve(routeLocation)
window.open(routeData.href, '_blank')
}
else {
return routerPush(routeLocation)
}
}
/**
* 路由跳转并携带 meta 中的 query 参数
*
* @param key 路由名称
*/
function routerPushByKeyWithMetaQuery(key: RouteKey) {
const routeStore = useRouteStore()
const allRoutes = router.getRoutes()
const meta = allRoutes.find(item => item.name === key)?.meta || null
const menu = routeStore.menus.find(item => item.key === key)
if (menu?.children && menu.children.length > 0) {
const visibleChildren = menu.children.filter((child) => {
const childRoute = allRoutes.find(r => r.name === child.key)
return childRoute && !childRoute.meta?.hideInMenu
})
if (visibleChildren.length === 1) {
return routerPushByKey(visibleChildren[0].key as RouteKey)
}
}
const query: Record<string, string> = {}
meta?.query?.forEach((item) => {
query[item.key] = item.value
})
return routerPushByKey(key, { query })
}
async function toHome() {
return routerPushByKey('root')
}
/**
* 跳转到登录页
*
* @param loginModule 登录模块
* @param redirectUrl 重定向地址,若未指定,则为当前路由 fullPath
*/
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
const module = loginModule || 'pwd-login'
const options: App.Global.RouterPushOptions = {
params: {
module,
},
}
const redirect = redirectUrl || route.value.fullPath
options.query = {
redirect,
}
return routerPushByKey('login', options)
}
/**
* 切换登录模块
*
* @param module
*/
async function toggleLoginModule(module: UnionKey.LoginModule) {
const query = route.value.query as Record<string, string>
return routerPushByKey('login', { query, params: { module } })
}
/**
* 从登录页重定向
*
* @param [needRedirect] 登录后是否需要重定向。默认为 `true`
*/
async function redirectFromLogin(needRedirect = true) {
const redirect = route.value.query?.redirect as string
if (needRedirect && redirect) {
await routerPush(redirect)
}
else {
await toHome()
}
}
return {
routerPush,
routerBack,
routerPushByKey,
routerPushByKeyWithMetaQuery,
toLogin,
toggleLoginModule,
redirectFromLogin,
}
}