diff --git a/src/App.vue b/src/App.vue index 38a7f82..f439bde 100644 --- a/src/App.vue +++ b/src/App.vue @@ -4,12 +4,42 @@ - - - - - - + + + + +
+ +
+ + + + + + + +
+ + +
+
+ +
+ + + +
+ +
+
+
+
+
+ + +
+
+
@@ -27,10 +57,17 @@ import { NNotificationProvider, useOsTheme } from 'naive-ui' -import MainNav from '@components/nav/MainNav.vue' -import GuestNav from '@components/nav/GuestNav.vue' import Notification from '@components/TheNotification.vue' -import { computed } from 'vue' +import TheFooter from '@components/TheFooter.vue' +import TheHeader from '@components/TheHeader.vue' +import UserInfo from '@components/UserInfo.vue' +import MainSidebar from '@components/sidebar/MainSidebar.vue' +import GuestSidebar from '@components/sidebar/GuestSidebar.vue' +import LoadingBar from '@components/LoadingBar.vue' +import TheMessage from '@components/TheMessage.vue' +import TheDialog from '@components/TheDialog.vue' + +import { computed, ref } from 'vue' import userData from '@/utils/stores/userData/store' import hljs from 'highlight.js/lib/core' import ini from 'highlight.js/lib/languages/ini' @@ -39,12 +76,23 @@ import api from '@/api' import { sendWarningMessage } from '@/utils/message' import { logout } from '@/utils/profile' import router from '@router' -// import { init_ws, SetOnMessageFunction } from "@/utils/websocket.js"; -// import { sendInfoNotification } from '@/utils/notification' +import { useRoute } from 'vue-router' + +// 手机状态下收缩菜单栏 +const collapsed = ref(true) +if (document.body.clientWidth >= 1000) { + collapsed.value = false +} const osThemeRef = useOsTheme() +const route = useRoute() const theme = computed(() => (osThemeRef.value === 'dark' ? darkTheme : null)) -let inited = false +// let initFinished = false + +const loading = ref(true) +const tokenValid = ref(false) +const showGuestSidebar = ref(false) +const showMainSidebar = ref(false) hljs.registerLanguage('ini', ini) hljs.registerLanguage('nginx', nginx) @@ -58,41 +106,101 @@ hljs.registerLanguage('nginx', nginx) // } // 初始化websocket -if (inited === false) { - // init_ws(); - // SetOnMessageFunction(getMessage); - inited = true +// if (initFinished === false) { +// // init_ws(); +// // SetOnMessageFunction(getMessage); +// initFinished = true +// } + +async function fetchUserInfo() { + let rs + try { + rs = await api.v2.user.info.root.get(userData.getters.get_username) + } catch (e) { + sendWarningMessage('查询用户信息失败: ' + e + ',请重新登录后台!') + } + if (!rs) return + if (rs.status === 200) { + // console.log(rs) + userData.commit('set_user_email', rs.data.email) + userData.commit('set_user_inbound', rs.data.inbound) + userData.commit('set_user_outbound', rs.data.inbound) + userData.commit('set_user_traffic', rs.data.traffic) + // localStorage.setItem('proxies', res.proxies_num) + // localStorage.setItem('traffic', res.traffic) + // localStorage.setItem('set_limit', res) + // userData.set_limit({ + // inbound: res.inbound, + // outbound: res.outbound, + // }) + return true + } + if (rs.status === 401) { + sendWarningMessage('登录过期或未登录,请重新登录后台!') + logout() + } + return false } -setInterval(async () => { - if (userData.getters.get_token) { - let rs - try { - rs = await api.v2.user.info.root.get(userData.getters.get_username) - } catch (e) { - sendWarningMessage('查询用户信息失败: ' + e + ',请重新登录后台!') - logout() - } - if (!rs) return - if (rs.status === 200) { - // console.log(rs) - userData.commit('set_user_email', rs.data.email) - userData.commit('set_user_inbound', rs.data.inbound) - userData.commit('set_user_outbound', rs.data.inbound) - userData.commit('set_user_traffic', rs.data.traffic) - // localStorage.setItem('proxies', res.proxies_num) - // localStorage.setItem('traffic', res.traffic) - // localStorage.setItem('set_limit', res) - // userData.set_limit({ - // inbound: res.inbound, - // outbound: res.outbound, - // }) +watch( + () => route.meta, + (value) => { + if (value.needLogin) { + showMainSidebar.value = true + showGuestSidebar.value = false + } else { + if (value.sidebar?.guest) { + showMainSidebar.value = false + showGuestSidebar.value = true + } else if (value.sidebar?.main) { + showMainSidebar.value = true + showGuestSidebar.value = false + } else { + showMainSidebar.value = false + showGuestSidebar.value = false + } } - if (rs.status === 401) { - sendWarningMessage('登录过期或未登录,请重新登录后台!') - logout() + } +) + +onMounted(async () => { + if (userData.getters.get_token !== '') { + tokenValid.value = await fetchUserInfo() + } + loading.value = false +}) + +setInterval(async () => { + if (userData.getters.get_token !== '') { + const valid = await fetchUserInfo() + if (!valid) { router.push('/auth/login') } } }, 10000) + + diff --git a/src/components/TheFooter.vue b/src/components/TheFooter.vue index 3ffa378..8e42c89 100644 --- a/src/components/TheFooter.vue +++ b/src/components/TheFooter.vue @@ -1,4 +1,4 @@ - diff --git a/src/components/UserInfo.vue b/src/components/UserInfo.vue index df74be7..f44b4b0 100644 --- a/src/components/UserInfo.vue +++ b/src/components/UserInfo.vue @@ -94,9 +94,10 @@ import { logout } from '@/utils/profile' import userData from '@/utils/stores/userData/store' import { sendErrorMessage, sendSuccessMessage } from '@/utils/message' import { onMounted, ref } from 'vue' -import { useDialog } from 'naive-ui' +import { useDialog, useLoadingBar, useMessage } from 'naive-ui' import api from '@/api' import logger from '@/utils/logger' +import router from '@router' const dialogWidth = ref('30vw') const ldb = useLoadingBar() @@ -139,27 +140,29 @@ const tPassword = ref({ // 检查 QQ 绑定状态 onMounted(async () => { - let rs - try { - rs = await api.v2.user.info.qq(userData.getters.get_username) - } catch (e) { - logger.error(e) - bindQQ.value.isDisable = true - bindQQ.value.msg = ref('未知') - } - if (!rs) return - if (rs.status === 200) { - bindQQ.value.isDisable = true - bindQQ.value.msg = ref('已绑定') - bindQQ.value.unBindDisable = false - bindQQ.value.unBindmsg = ref('解除绑定') - } else if (rs.status === 404) { - bindQQ.value.isDisable = false - bindQQ.value.msg = ref('点击绑定') - bindQQ.value.unBindDisable = true - bindQQ.value.unBindmsg = ref('尚未绑定') - } else { - sendErrorMessage('获取 QQ 绑定状态失败: ' + rs.message) + if (userData.getters.get_token !== '') { + let rs + try { + rs = await api.v2.user.info.qq(userData.getters.get_username) + } catch (e) { + logger.error(e) + bindQQ.value.isDisable = true + bindQQ.value.msg = ref('未知') + } + if (!rs) return + if (rs.status === 200) { + bindQQ.value.isDisable = true + bindQQ.value.msg = ref('已绑定') + bindQQ.value.unBindDisable = false + bindQQ.value.unBindmsg = ref('解除绑定') + } else if (rs.status === 404) { + bindQQ.value.isDisable = false + bindQQ.value.msg = ref('点击绑定') + bindQQ.value.unBindDisable = true + bindQQ.value.unBindmsg = ref('尚未绑定') + } else { + sendErrorMessage('获取 QQ 绑定状态失败: ' + rs.message) + } } }) @@ -294,8 +297,10 @@ async function unBindQQ() { } function doLogOut() { - sendSuccessMessage('您已登出,感谢您的使用!') + changeUserInfoShow(false) logout() + sendSuccessMessage('您已登出,感谢您的使用!') + router.push('/auth/login') } async function changePassword() { diff --git a/src/components/nav/GuestNav.vue b/src/components/nav/GuestNav.vue deleted file mode 100644 index d7ab5cd..0000000 --- a/src/components/nav/GuestNav.vue +++ /dev/null @@ -1,76 +0,0 @@ - - - - - diff --git a/src/components/nav/MainNav.vue b/src/components/nav/MainNav.vue deleted file mode 100644 index 4e3c6ad..0000000 --- a/src/components/nav/MainNav.vue +++ /dev/null @@ -1,167 +0,0 @@ - - - - - - - diff --git a/src/router/index.js b/src/router/index.js index c97327f..34b13d8 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,8 +1,6 @@ import { createRouter, createWebHistory } from 'vue-router' import { finishLoadingBar, startLoadingBar } from '@/utils/loadingbar' import userData from '@/utils/stores/userData/store' -import { changeMainSidebarShow } from '@/components/nav/MainNav.vue' -import { changeShowGuestSidebar } from '@/components/nav/GuestNav.vue' import logger from '@/utils/logger' const routes = [ @@ -321,23 +319,6 @@ router.afterEach((to) => { // 设置标题 document.title = to.meta.title + ' | LoCyanFrp' } - - // 需要登录则展示主 sidebar - if (to.meta.needLogin) { - changeMainSidebarShow(true) - changeShowGuestSidebar(false) - } else { - if (to.meta.sidebar?.guest) { - changeShowGuestSidebar(true) - changeMainSidebarShow(false) - } else if (to.meta.sidebar?.main) { - changeShowGuestSidebar(false) - changeMainSidebarShow(true) - } else { - changeShowGuestSidebar(false) - changeMainSidebarShow(false) - } - } }) export default router diff --git a/src/utils/profile.js b/src/utils/profile.js index d58701c..5f6265c 100644 --- a/src/utils/profile.js +++ b/src/utils/profile.js @@ -51,5 +51,4 @@ export function getProxies(username, token) { export function logout() { userData.commit('delete_user_info') - router.push('/auth/login') }