Skip to content

Commit

Permalink
[*] 修改路由规则,按照 Vue3 推荐规范,.vue 文件名至少使用两个词组成
Browse files Browse the repository at this point in the history
  • Loading branch information
Muska-Ami committed Sep 18, 2024
1 parent a06b253 commit 559f566
Show file tree
Hide file tree
Showing 28 changed files with 138 additions and 89 deletions.
8 changes: 4 additions & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import {
NNotificationProvider,
useOsTheme
} from 'naive-ui'
import MainNav from './components/MainNav.vue'
import GuestNav from './components/GuestNav.vue'
import Notification from './components/Notification.vue'
import MainNav from '@components/MainNav.vue'
import GuestNav from '@components/GuestNav.vue'
import Notification from '@components/Notification.vue'
import { computed } from 'vue'
import userData from '@/utils/stores/userData/store'
import hljs from 'highlight.js/lib/core'
Expand All @@ -39,7 +39,7 @@ import nginx from 'highlight.js/lib/languages/nginx'
import api from '@/api'
import { sendWarningMessage } from '@/utils/message'
import { logout } from '@/utils/profile'
import router from '@/router/index'
import router from '@router'
// import { init_ws, SetOnMessageFunction } from "@/utils/websocket.js";
// import { sendInfoNotification } from '@/utils/notification'
Expand Down
2 changes: 1 addition & 1 deletion src/components/GuestNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import { h, ref, onMounted } from 'vue'
import { NGradientText } from 'naive-ui'
import GuestSideBar from './GuestSideBar.vue'
import router from '@/router/index'
import router from '@router'
import { get } from '@/utils/request'
// 手机状态下收缩菜单栏
Expand Down
2 changes: 1 addition & 1 deletion src/components/GuestSideBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const menuOptions = [
</script>
<script>
import { ref } from 'vue'
import router from '@/router/index'
import router from '@router'
const active = ref('')
Expand Down
1 change: 1 addition & 0 deletions src/components/InstallCsApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<n-space vertical>
<n-spin :show="show">
<div style="text-align: center">
<n-alert title="已废弃" type="warning"> 请改用软件下载 </n-alert>
文件名: {{ file_name }}
<br />
<br />
Expand Down
2 changes: 1 addition & 1 deletion src/components/MainNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ import { h, ref, onMounted } from 'vue'
import { NGradientText } from 'naive-ui'
import SideBar from './MainSideBar.vue'
import userData from '@/utils/stores/userData/store'
import router from '@/router/index'
import router from '@router'
import UserInfo, { changeUserInfoShow } from './UserInfo.vue'
import { get } from '@/utils/request'
import { GitAlt } from '@vicons/fa'
Expand Down
4 changes: 2 additions & 2 deletions src/components/MainSideBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const menuOptions = [
icon: renderIcon(List)
},
{
path: '/config',
path: '/proxies/config',
label: '配置文件',
key: 'Config',
icon: renderIcon(FileTrayFull)
Expand Down Expand Up @@ -204,7 +204,7 @@ const inverted = false
</script>
<script>
import { ref } from 'vue'
import router from '@/router/index'
import router from '@router'
const active = ref('')
const menuInstRef = ref(null)
Expand Down
8 changes: 4 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { createApp } from 'vue'
import './style.css'
import './twemoji-amazing.css'
import App from './App.vue'
import message from './components/Message.vue'
import loadingbar from './components/LoadingBar.vue'
import ndialog from './components/Dialog.vue'
import router from './router/index'
import message from '@components/Message.vue'
import loadingbar from '@components/LoadingBar.vue'
import ndialog from '@components/Dialog.vue'
import router from '@router'
import axios from 'axios'
import VueAxios from 'vue-axios'
import userData from '@/utils/stores/userData/store'
Expand Down
143 changes: 75 additions & 68 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createRouter, createWebHistory } from 'vue-router'
import { finishLoadingBar, startLoadingBar } from '@/utils/loadingbar'
import userData from '@/utils/stores/userData/store'
import { changeMainSideBarShow } from '../components/MainNav.vue'
import { changeShowGuestSideBar } from '../components/GuestNav.vue'
import { setSideBarActiveKey } from '../components/MainSideBar.vue'
import { setGuestSideBarActiveKey } from '../components/GuestSideBar.vue'
import { changeMainSideBarShow } from '@/components/MainNav.vue'
import { changeShowGuestSideBar } from '@/components/GuestNav.vue'
import { setSideBarActiveKey } from '@/components/MainSideBar.vue'
import { setGuestSideBarActiveKey } from '@/components/GuestSideBar.vue'
import logger from '@/utils/logger'

const routes = [
Expand All @@ -15,127 +15,147 @@ const routes = [
path: '/',
name: 'MainPage',
meta: {
keepAlive: true,
title: '首页'
},
component: () => import('../views/Main.vue')
component: () => import('@views/MainView.vue')
},
{
path: '/dashboard',
name: 'Dashboard',
meta: {
title: '仪表盘',
keepAlive: true,
title: '仪表盘'
needLogin: true
},
component: () => import('../views/Dashboard.vue')
component: () => import('@views/DashboardView.vue')
},
{
path: '/sign',
name: 'Sign',
meta: {
title: '签到',
keepAlive: true,
title: '签到'
needLogin: true
},
component: () => import('../views/Sign.vue')
component: () => import('@views/SignView.vue')
},
{
path: '/login',
name: 'Login',
meta: {
title: '登录',
keepAlive: true,
title: '登录'
autoRedirectLogined: true
},
component: () => import('../views/Login.vue')
component: () => import('@views/auth/LoginView.vue')
},
{
path: '/register',
name: 'Register',
meta: {
title: '注册',
keepAlive: true,
title: '注册'
autoRedirectLogined: true
},
component: () => import('../views/Register.vue')
component: () => import('@views/auth/RegisterView.vue')
},
{
path: '/reset_password',
name: 'ResetPassword',
meta: {
title: '重置密码',
keepAlive: true,
title: '重置密码'
autoRedirectLogined: true
},
component: () => import('../views/ResetPassword.vue')
component: () => import('@views/auth/ResetPasswordView.vue')
},
{
path: '/proxies',
name: 'Proxies',
meta: {
title: '隧道列表',
keepAlive: true,
title: '隧道列表'
needLogin: true
},
component: () => import('../views/Proxies.vue')
component: () => import('@views/proxies/ProxiesView.vue')
},
{
path: '/proxies/add',
name: 'AddProxies',
meta: {
title: '添加隧道',
keepAlive: true,
title: '添加隧道'
needLogin: true
},
component: () => import('../views/AddProxies.vue')
component: () => import('@views/proxies/AddView.vue')
},
{
path: '/realname',
name: 'RealName',
meta: {
title: '实名认证',
keepAlive: true,
title: '实名认证'
needLogin: true
},
component: () => import('../views/RealName.vue')
component: () => import('@views/RealNameView.vue')
},
{
path: '/hello2024',
name: 'NewYear',
meta: {
title: '留言',
keepAlive: true,
title: '留言'
needLogin: true
},
component: () => import('../views/NewYear.vue')
component: () => import('@views/activities/NewYearView.vue')
},
{
path: '/prize',
name: 'Prize',
meta: {
title: '抽奖',
keepAlive: true,
title: '抽奖'
needLogin: true
},
component: () => import('../views/Prize.vue')
component: () => import('@views/activities/PrizeView.vue')
},
{
path: '/config',
path: '/proxies/config',
name: 'Config',
meta: {
title: '配置文件',
keepAlive: true,
title: '配置文件'
needLogin: true
},
component: () => import('../views/Config.vue')
component: () => import('@views/proxies/ConfigView.vue')
},
{
path: '/donate',
name: 'Donate',
meta: {
title: '赞助',
keepAlive: true,
title: '赞助'
needLogin: true
},
component: () => import('../views/Donate.vue')
component: () => import('@views/DonateView.vue')
},
{
path: '/icp',
name: 'Icp',
meta: {
title: '域名白名单',
keepAlive: true,
title: '域名白名单'
needLogin: true
},
component: () => import('../views/IcpCheck.vue')
component: () => import('@views/IcpCheckView.vue')
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
meta: {
title: '页面未找到'
},
component: () => import('@views/NotFoundView.vue')
}
// 没做完的
// {
Expand All @@ -145,7 +165,7 @@ const routes = [
// keepAlive: true,
// title: '多人游戏大厅'
// },
// component: () => import('../views/LanLobby.vue')
// component: () => import('@views/LanLobby.vue')
// }
]
}
Expand All @@ -161,54 +181,41 @@ const router = createRouter({
// userData.commit('set_token', localStorage.getItem('token'))
// }

// 检测到已登录之后自动跳转 /dashboard 的界面
const _autoRedirectLogined = ['Login', 'Register', 'ResetPassword']

router.beforeEach((to, from, next) => {
logger.info(`Routing from ${from.name} to ${to.name}`)
startLoadingBar()
if (_autoRedirectLogined.includes(to.name)) {
const hasToken = userData.getters.get_token != ''
logger.info(`Has token: ${hasToken}${hasToken ? ` ${userData.getters.get_token}` : ''}`)
if (hasToken) next({ name: 'Dashboard' })
else next()
} else if (to.name === 'MainPage') {
next()
} else if (userData.getters.get_token == '') {

if (to.name === 'NotFound') {
next() // 直接放行 404 路由
return
}

const hasToken = userData.getters.get_token !== ''

if (to.meta.needLogin && !hasToken) {
next({ name: 'Login', query: { redirect: location.pathname } })
} else {
next()
}
if (to.meta.autoRedirectLogined && hasToken) {
next({ name: 'Dashboard' })
}
next()
})

// from next
router.afterEach((to) => {
finishLoadingBar()
if (to.meta.title) {
//设置标题
// 设置标题
document.title = to.meta.title + ' | LoCyanFrp'
}

switch (to.name) {
case 'MainPage':
changeMainSideBarShow(false)
changeShowGuestSideBar(false)
break
case 'Login':
changeMainSideBarShow(false)
changeShowGuestSideBar(true)
break
case 'Register':
changeMainSideBarShow(false)
changeShowGuestSideBar(true)
break
case 'ResetPassword':
changeMainSideBarShow(false)
changeShowGuestSideBar(true)
break
default:
changeMainSideBarShow(true)
changeShowGuestSideBar(false)
// 需要登录则展示主 sidebar
if (to.meta.needLogin) {
changeMainSideBarShow(true)
changeShowGuestSideBar(false)
} else {
changeMainSideBarShow(false)
changeShowGuestSideBar(false)
}

if (userData.getters.get_token) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/profile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios'
import { ref } from 'vue'
import userData from './stores/userData/store'
import router from '@/router/index'
import router from '@router'
import { get } from './request'
import { sendWarningMessage } from './message'

Expand Down
2 changes: 1 addition & 1 deletion src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import axios from 'axios'
import QS from 'qs'
// vuex
import userData from './stores/userData/store'
import router from '@/router/index'
import router from '@router'
import Base64 from 'qs/lib/utils'
import { sendErrorMessage } from './message'
import logger from '@/utils/logger'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/views/Main.vue → src/views/MainView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</template>

<script setup>
import router from '@/router/index'
import router from '@router'
const GoToDocs = () => {
window.open('https://doc.locyan.cn')
Expand Down
Loading

0 comments on commit 559f566

Please sign in to comment.