mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2026-11-28 15:33:42 +08:00
Enforce live session grants for runtime routes and support profile and credential updates in both the backend and demo modes. Add the account settings workspace and document fullstack setup and PC acceptance.
80 lines
1.9 KiB
TypeScript
80 lines
1.9 KiB
TypeScript
import type { App } from 'vue'
|
|
import { createRouter, createWebHashHistory } from 'vue-router'
|
|
import { getStaticAppRoutes, setAppRouteModules } from './app-route-modules'
|
|
import { loadRouteConfig, resetRouteSource } from './config/source'
|
|
import {
|
|
constantRoutes,
|
|
getAccountProfileRoute,
|
|
getLayoutRoutes,
|
|
getSystemRoutes,
|
|
} from './routes'
|
|
import { scrollViewToTop, vueRouterRegister } from './utils'
|
|
|
|
export const router = createRouter({
|
|
history: createWebHashHistory(),
|
|
routes: constantRoutes,
|
|
scrollBehavior: (to) => {
|
|
scrollViewToTop(to)
|
|
},
|
|
})
|
|
|
|
let pending: Promise<void> | undefined
|
|
let controller: AbortController | undefined
|
|
let removeRoutes: (() => void)[] = []
|
|
|
|
export function clearAccountRoutes() {
|
|
controller?.abort()
|
|
pending = void 0
|
|
|
|
for (const remove of removeRoutes.splice(0)) remove()
|
|
|
|
setAppRouteModules([])
|
|
resetRouteSource()
|
|
}
|
|
|
|
export function loadAccountRoutes() {
|
|
if (pending) return pending
|
|
|
|
const current = new AbortController()
|
|
|
|
controller = current
|
|
const load = (async () => {
|
|
const config = await loadRouteConfig(current.signal)
|
|
const routes = config
|
|
? (await import('./config/contract')).toRuntimeRoutes(config)
|
|
: getStaticAppRoutes()
|
|
|
|
current.signal.throwIfAborted()
|
|
|
|
for (const remove of removeRoutes.splice(0)) remove()
|
|
|
|
setAppRouteModules(routes)
|
|
removeRoutes = getLayoutRoutes().map((route) =>
|
|
router.addRoute('RLayout', route),
|
|
)
|
|
})().finally(() => {
|
|
if (pending === load) pending = void 0
|
|
})
|
|
|
|
pending = load
|
|
|
|
return load
|
|
}
|
|
|
|
let registered = false
|
|
|
|
export async function setupRouter(app: App<Element>, authenticated: boolean) {
|
|
if (authenticated) await loadAccountRoutes()
|
|
else clearAccountRoutes()
|
|
|
|
if (!registered) {
|
|
for (const route of [...getSystemRoutes(), getAccountProfileRoute()])
|
|
router.addRoute('RLayout', route)
|
|
|
|
vueRouterRegister(router)
|
|
registered = true
|
|
}
|
|
|
|
app.use(router)
|
|
}
|