Files
cloudflare_temp_email/frontend/src/router/index.js
2026-04-30 15:33:06 +08:00

95 lines
2.5 KiB
JavaScript

import { createRouter, createWebHistory } from 'vue-router'
import Index from '../views/Index.vue'
import User from '../views/User.vue'
import UserOauth2Callback from '../views/user/UserOauth2Callback.vue'
import i18n from '../i18n'
import { useGlobalState } from '../store'
import {
DEFAULT_LOCALE,
getBrowserLocales,
getPreferredLocale,
replaceLocaleInFullPath,
resolveSupportedLocale,
} from '../i18n/utils'
const { jwt, preferredLocale } = useGlobalState()
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/',
alias: '/:lang/',
component: Index
},
{
path: '/user',
alias: '/:lang/user',
component: User
},
{
path: '/user/oauth2/callback',
alias: '/:lang/user/oauth2/callback',
component: UserOauth2Callback
},
{
path: '/admin',
alias: '/:lang/admin',
component: () => import('../views/Admin.vue')
},
{
path: '/telegram_mail',
alias: '/:lang/telegram_mail',
component: () => import('../views/telegram/Mail.vue')
},
{
name: 'not-found',
path: '/:pathMatch(.*)*',
redirect: '/'
}
]
});
router.beforeEach((to, from, next) => {
const routeLocale = resolveSupportedLocale(to.path.split('/')[1])
const resolvedLocale = routeLocale || DEFAULT_LOCALE
i18n.global.locale.value = resolvedLocale
if (routeLocale) {
preferredLocale.value = routeLocale
} else if (!preferredLocale.value) {
preferredLocale.value = getPreferredLocale('', getBrowserLocales())
}
if (Object.prototype.hasOwnProperty.call(to.query, 'jwt')) {
const jwtQuery = Array.isArray(to.query.jwt) ? to.query.jwt[0] : to.query.jwt
if (typeof jwtQuery === 'string') {
jwt.value = jwtQuery
}
const query = { ...to.query }
delete query.jwt
next({
path: to.path,
query,
hash: to.hash,
replace: true,
})
return
}
if (routeLocale) {
const canonicalRoutePath = replaceLocaleInFullPath(to.fullPath, routeLocale)
if (canonicalRoutePath !== to.fullPath) {
return next(canonicalRoutePath)
}
}
if (routeLocale === DEFAULT_LOCALE) {
return next(replaceLocaleInFullPath(to.fullPath, DEFAULT_LOCALE))
}
next()
});
export default router