| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <template>
- <div id="app">
- <!-- 顶部菜单栏 -->
- <div v-if="showHeader" class="app-header">
- <router-link to="/">首页</router-link>
- <router-link to="/pages/novel/list">小说列表</router-link>
- <router-link to="/pages/author/apply">作者申请</router-link>
- <router-link to="/pages/search/index">搜索</router-link>
- </div>
-
- <!-- 主内容区 -->
- <router-view v-if="isRouterAlive" />
-
- <!-- 使用自定义TabBar组件 -->
- <CustomTabbar v-if="showTabBar" />
- </div>
- </template>
-
- <script>
- import CustomTabbar from '@/components/custom-tabbar/index.vue';
-
- export default {
- name: 'App',
- provide() {
- return {
- reload: this.reload
- }
- },
- components: { CustomTabbar },
- data() {
- return {
- // 确保所有属性都在这里定义
- isRouterAlive: true,
- showTabBar: false,
- showHeader: false,
- isMounted: false, // 添加组件挂载状态标志
- tabBarPages: [
- '/pages/index/index',
- '/pages/novel/list',
- '/pages/bookshelf/index',
- '/pages/me/index'
- ],
- hideMenuRoutes: [
- '/login',
- '/register',
- '/forgot-password',
- '/novel/reader'
- ]
- }
- },
- mounted() {
- this.isMounted = true;
- this.checkRouteAuth();
- this.initTheme(); // 确保正确调用
- this.initStoreState();
- this.safeUpdateMenuVisibility();
-
- },
- beforeDestroy() {
- this.isMounted = false;
- },
- watch: {
- // 安全监听路由变化
- '$route': {
- immediate: true,
- handler(newRoute) {
- if (this.isMounted && newRoute && newRoute.path) {
- this.safeUpdateMenuVisibility();
- }
- }
- },
- '$store.state.token': {
- handler(newToken) {
- if (newToken) {
- console.log('检测到新token,重新加载数据');
- this.reloadData();
- }
- },
- immediate: true
- }
- },
- methods: {
- // 确保 initTheme 方法正确定义
- initTheme() {
- console.log('主题初始化开始');
-
- // 设置默认主题
- //const themeName = uni.getStorageSync('selectedTheme') || 'aydzBlue';
- const themeName = localStorage.getItem('selectedTheme') || 'aydzBlue';
- // 应用主题变量
- const themes = {
- aydzBlue: {
- '--primary-color': '#2a5caa',
- '--bg-color': '#e6f7ff',
- '--text-color': '#1a3353',
- '--card-bg': '#d0e8ff',
- '--header-bg': '#2a5caa'
- },
- default: {
- '--primary-color': '#1890ff',
- '--bg-color': '#f8f9fa',
- '--text-color': '#333',
- '--card-bg': '#ffffff'
- }
- };
- const theme = themes[themeName] || themes.default;
-
- // 应用主题变量
- Object.keys(theme).forEach(key => {
- document.documentElement.style.setProperty(key, theme[key]);
- });
-
- console.log('主题初始化完成');
- },
- reload() {
- this.isRouterAlive = false;
- this.$nextTick(() => {
- this.isRouterAlive = true;
- this.safeUpdateMenuVisibility();
- });
- },
-
- safeUpdateMenuVisibility() {
- // 确保组件已挂载且路由对象存在
- if (!this.isMounted || !this.$route || !this.$route.path) return;
-
- const currentPath = this.$route.path;
-
- // 检查是否显示底部TabBar
- this.showTabBar = this.tabBarPages.some(path =>
- currentPath.includes(path) || currentPath === path
- );
-
- // 检查是否显示顶部菜单
- this.showHeader = !this.hideMenuRoutes.some(route =>
- currentPath.includes(route) || currentPath === route
- );
- },
-
- initStoreState() {
- const token = uni.getStorageSync('token') || ''
- const readingProgress = uni.getStorageSync('readingProgress') || 1
-
- // 确保 store 存在并正确提交
- if (this.$store) {
- this.$store.commit('SET_TOKEN', token)
- this.$store.commit('SET_READING_PROGRESS', readingProgress)
- console.log('Store initialized:', this.$store.state)
- } else {
- console.error('Store is not available!')
- }
- },
-
- checkRouteAuth() {
- // 简单的路由权限检查
- if (!this.$route || !this.$route.path) return;
-
- const authRequiredRoutes = [
- '/pages/author/apply',
- '/pages/bookshelf/index',
- '/pages/me/index'
- ];
-
- if (authRequiredRoutes.some(route => this.$route.path.includes(route))) {
- if (!this.$store.getters.token) {
- uni.showToast({ title: '请先登录', icon: 'none' });
- this.$router.push('/pages/login');
- }
- }
- },
- reloadData() {
- // 在需要的地方调用此方法重新加载数据
- if (this.$route.path === '/pages/novel/list') {
- this.$refs.novelList?.initData?.();
- }
- }
- }
- }
- </script>
-
- <style lang="scss">
- @import '@/styles/index.scss';
-
- /* 全局样式修复 */
- #app {
- min-height: 100vh;
- background-color: var(--bg-color);
- color: var(--text-color);
- }
-
- /* 确保菜单不被其他元素覆盖 */
- .custom-tabbar {
- z-index: 99999 !important;
- position: fixed !important;
- bottom: 0 !important;
- left: 0 !important;
- right: 0 !important;
- }
-
- /* 修复页面内容被遮挡的问题 */
- .page-content {
- padding-bottom: 140rpx !important;
- }
-
- /* 确保tabbar显示 */
- uni-tabbar {
- display: flex !important;
- position: fixed;
- bottom: 0;
- left: 0;
- right: 0;
- z-index: 9999;
- background-color: #fff;
- box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
- }
-
- /* 修复高度问题 */
- uni-tabbar .uni-tabbar {
- height: 50px !important;
- }
-
- /* 页面内容区域 */
- .page-content {
- padding-bottom: 60px !important;
- }
-
- .app-header {
- background: var(--header-bg, #2a5caa);
- padding: 10px;
- display: flex;
- justify-content: space-around;
- position: sticky;
- top: 0;
- z-index: 1000;
-
- a {
- color: white;
- text-decoration: none;
- font-weight: bold;
-
- &.router-link-exact-active {
- color: #ffcc00;
- border-bottom: 2px solid #ffcc00;
- }
- }
- }
- </style>
|