| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- </template>
- <div id="app">
- <!-- 顶部菜单栏 -->
- <div v-if="showHeader" class="app-header">
- <router-link to="/">首页</router-link>
- <router-link to="/novel/list">小说列表</router-link>
- <router-link to="/author/apply">作者申请</router-link>
- <router-link to="/search">搜索</router-link>
- </div>
-
- <!-- 主内容区 -->
- <router-view v-if="isRouterAlive" />
- <!-- 使用自定义TabBar组件 -->
- <CustomTabbar v-if="showTabBar" />
- </div>
- </template>
-
- <script>
- import CustomTabbar from '@/components/custom-tabbar/index.vue'
- import config from './config'
- import { getToken } from '@/utils/auth'
-
- export default {
- mounted() {
- // 确保方法名称正确
- this.checkRouteAuth();
- },
- name: 'App',
- provide() {
- return {
- reload: this.reload
- }
- },
- components: { CustomTabbar },
- data() {
- return {
- isRouterAlive: true,
- showTabBar: false, // 默认不显示
- tabBarPages: [
- '/pages/index/index',
- '/pages/novel/list',
- '/pages/bookshelf/index',
- '/pages/me/index'
- ],
- // 定义需要隐藏菜单的路由
- hideMenuRoutes: [
- '/login',
- '/register',
- '/forgot-password',
- '/novel/reader'
- ]
- }
- },
- watch: {
- '$route.path': {
- immediate: true,
- handler(newPath) {
- // 检查当前页面是否需要显示菜单
- this.showTabBar = this.tabBarPages.some(path =>
- newPath.includes(path)
- );
-
- // 特殊处理:在小说阅读页面也显示菜单
- if (newPath.includes('/pages/novel/read')) {
- this.showTabBar = true;
- }
-
- // 强制更新CustomTabbar组件
- this.$nextTick(() => {
- if (this.$refs.customTabbar) {
- this.$refs.customTabbar.updateSelectedIndex();
- }
- });
- }
- }
- },
-
- onLaunch() {
- // 初始化主题
- this.initTheme()
-
- // 检查阅读进度
- this.checkReadingProgress()
-
- // 初始化 store 状态
- this.initStoreState()
- console.log('App launched, store:', this.$store)
- },
- onShow() {
- // 初始检查路由
- this.$nextTick(() => {
- const currentPath = this.$route.path;
- this.showTabBar = this.tabBarPages.some(path =>
- currentPath.includes(path)
- );
- });
-
- // 检查云端阅读进度
- if (this.$store.getters.token) {
- this.syncReadingProgress()
- }
- },
- methods: {
- checkRouteAuth() {
- const noAuthPaths = ['/', '/home', '/novel'];
- if (!noAuthPaths.includes(this.$route.path)) {
- // 需要登录的路径处理
- }
- },
- reload() {
- this.isRouterAlive = false
- this.$nextTick(() => {
- this.isRouterAlive = true
- })
- },
- updateMenuVisibility() {
- const currentPath = this.$route.path;
- // 检查当前路由是否需要隐藏菜单
- this.showHeader = !this.hideMenuRoutes.some(route => currentPath.includes(route));
- this.showFooter = !this.hideMenuRoutes.some(route => currentPath.includes(route));
- },
- // 初始化 store 状态
- initStoreState() {
- // 从本地存储恢复状态
- const token = uni.getStorageSync('token')
- const readingProgress = uni.getStorageSync('readingProgress') || 1
-
- // 提交到 store
- this.$store.commit('SET_TOKEN', token)
- this.$store.commit('SET_READING_PROGRESS', readingProgress)
- },
- // 检查阅读进度
- checkReadingProgress() {
- // 直接从本地存储读取
- const chapter = uni.getStorageSync('readingProgress') || 1
-
- if (chapter > 5 && !uni.getStorageSync('token')) {
- uni.showModal({
- title: '登录提示',
- content: `您已阅读到第${chapter}章,登录后继续阅读`,
- confirmText: '立即登录',
- success: (res) => {
- if (res.confirm) {
- uni.navigateTo({ url: '/pages/login' })
- }
- }
- })
- }
- },
- // 同步阅读进度 (修改后)
- async syncReadingProgress() {
- try {
- // 安全访问 store
- if (!this.$store) {
- console.warn('Store 未初始化,无法同步阅读进度')
- return
- }
-
- const token = this.$store.getters.token
- if (!token) {
- console.log('用户未登录,跳过同步')
- return
- }
-
- // 从本地存储获取进度
- const localChapter = uni.getStorageSync('readingProgress') || 1
-
- // 调用 API 同步
- const res = await this.$api.saveReadingProgress(localChapter)
- console.log('进度同步成功:', res.data)
- } catch (err) {
- console.error('同步阅读进度失败', err)
- }
- },
- // 添加 checkLogin 方法
- // checkLogin() {
- // // 这里写登录检查逻辑
- // // 示例:检查本地存储的登录状态
- // const isLoggedIn = uni.getStorageSync('isLogin');
- // if (!isLoggedIn) {
- // uni.navigateTo({ url: '/pages/login' });
- // }
- // },
-
- // 临时主题初始化
- initTheme() {
- const themeName = uni.getStorageSync('selectedTheme') || 'aydzBlue'
-
- // 应用主题变量
- const themes = {
- aydzBlue: {
- '--primary-color': '#2a5caa',
- '--bg-color': '#e6f7ff',
- '--text-color': '#1a3353',
- '--card-bg': '#d0e8ff',
- '--header-bg': '#2a5caa'
- },
- darkMode: {
- '--primary-color': '#52c41a',
- '--bg-color': '#1a1a1a',
- '--text-color': '#e6e6e6',
- '--card-bg': '#2a2a2a'
- },
- default: {
- '--primary-color': '#1890ff',
- '--bg-color': '#f8f9fa',
- '--text-color': '#333',
- '--card-bg': '#ffffff'
- }
- }
-
- const root = document.documentElement
- const themeVars = themes[themeName] || themes.aydzBlue
-
- Object.keys(themeVars).forEach(key => {
- root.style.setProperty(key, themeVars[key])
- })
-
- // 动态设置导航栏颜色
- if (themeName === 'darkMode') {
- uni.setNavigationBarColor({
- frontColor: '#ffffff',
- backgroundColor: '#1a1a1a'
- })
- } else if (themeName === 'aydzBlue') {
- uni.setNavigationBarColor({
- frontColor: '#ffffff',
- backgroundColor: '#2a5caa'
- })
- }
- }
- },
-
- watch: {
- '$route.path': {
- immediate: true,
- handler() {
- this.updateMenuVisibility();
- }
- }
- }
- }
- </script>
-
- <style lang="scss">
- /* 确保自定义TabBar有足够的空间 */
- .app-container {
- padding-bottom: 120rpx !important; /* 增加底部空间 */
- }
-
- /* 确保菜单不被其他元素覆盖 */
- .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;
- }
- /* 保留原有样式 */
- uni-tabbar {
- z-index: 999;
- }
- #app {
- display: flex;
- flex-direction: column;
- min-height: 100vh;
- }
-
- .app-header, .app-footer {
- background: #f5f5f5;
- padding: 10px;
- display: flex;
- justify-content: space-around;
- z-index: 1000;
- }
-
- .app-header {
- position: sticky;
- top: 0;
- }
-
- .app-footer {
- position: fixed;
- bottom: 0;
- width: 100%;
- }
-
- .router-link-active {
- color: #42b983;
- font-weight: bold;
- }
- </style>
|