You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/ui/routes.js

70 lines
1.5 KiB
JavaScript

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.

/*
* routes.js
* Copyright (C) 2025 veypi <i@veypi.com>
*
* Distributed under terms of the MIT license.
*/
import token from './token.js'
function wrapAxios(axios) {
axios.interceptors.response.use(function(response) {
if (response.data && response.data.code === 0) {
return response.data.data
}
return response;
}, function(error) {
let data = error.response ? error.response.data : error.response
if (data.code >= 400) {
return Promise.reject(data.message)
}
return Promise.reject(data.message);
});
}
async function checkAuth(to, from, next) {
if (token.isExpired()) {
await token.refresh()
}
if (token.isExpired()) {
token.logout()
}
}
/**
* 定义路由
* @param{{$axios:Axios, $bus:EventBus, $router: VRouter, root:string}} env
* */
function setup(env) {
token.setRoot(env.root)
token.wrapAxios(env.$axios)
wrapAxios(env.$axios)
let user = token.body()
env.Guser = user
env.Gtoken = token.getToken()
// 添加路由
env.$router.addRoutes([
{
path: '/',
component: '/page/index.html',
name: 'home',
},
{
path: '/404',
component: '/page/404.html',
name: '404'
},
{
path: '*', // 通配符,匹配所有未定义的路由
component: (path) => {
if (path.endsWith('.html')) {
return path; // 如果是HTML文件直接返回
}
path = '/page' + path
return path + '.html'
},
}
])
}
export default setup