Files
reader-star/apps/admin/src/hooks/common/router.ts

141 lines
3.4 KiB
TypeScript
Raw Normal View History

2026-01-16 13:06:44 +08:00
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'
2026-01-16 13:06:44 +08:00
/**
*
2026-01-16 13:06:44 +08:00
*
* router.push
2026-01-16 13:06:44 +08:00
*
* @param inSetup vue script setup
2026-01-16 13:06:44 +08:00
*/
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
*/
2026-01-16 13:06:44 +08:00
async function routerPushByKey(key: RouteKey, options?: App.Global.RouterPushOptions) {
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
}
return routerPush(routeLocation)
}
/**
* meta query
*
* @param key
*/
2026-01-16 13:06:44 +08:00
function routerPushByKeyWithMetaQuery(key: RouteKey) {
const routeStore = useRouteStore()
2026-01-16 13:06:44 +08:00
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)
}
}
2026-01-16 13:06:44 +08:00
const query: Record<string, string> = {}
meta?.query?.forEach((item) => {
query[item.key] = item.value
})
return routerPushByKey(key, { query })
}
async function toHome() {
return routerPushByKey('root')
}
/**
*
2026-01-16 13:06:44 +08:00
*
* @param loginModule
* @param redirectUrl fullPath
2026-01-16 13:06:44 +08:00
*/
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)
}
/**
*
2026-01-16 13:06:44 +08:00
*
* @param module
*/
async function toggleLoginModule(module: UnionKey.LoginModule) {
const query = route.value.query as Record<string, string>
return routerPushByKey('login', { query, params: { module } })
}
/**
*
2026-01-16 13:06:44 +08:00
*
* @param [needRedirect] `true`
2026-01-16 13:06:44 +08:00
*/
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,
}
}