├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.vue 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <div id="app">
  3. <!-- 顶部菜单栏 -->
  4. <div v-if="showHeader" class="app-header">
  5. <router-link to="/">首页</router-link>
  6. <router-link to="/pages/novel/list">小说列表</router-link>
  7. <router-link to="/pages/author/apply">作者申请</router-link>
  8. <router-link to="/pages/search/index">搜索</router-link>
  9. </div>
  10. <!-- 主内容区 -->
  11. <router-view v-if="isRouterAlive" />
  12. <!-- 使用自定义TabBar组件 -->
  13. <CustomTabbar v-if="showTabBar" />
  14. </div>
  15. </template>
  16. <script>
  17. import CustomTabbar from '@/components/custom-tabbar/index.vue';
  18. export default {
  19. name: 'App',
  20. provide() {
  21. return {
  22. reload: this.reload
  23. }
  24. },
  25. components: { CustomTabbar },
  26. data() {
  27. return {
  28. // 确保所有属性都在这里定义
  29. isRouterAlive: true,
  30. showTabBar: false,
  31. showHeader: false,
  32. isMounted: false, // 添加组件挂载状态标志
  33. tabBarPages: [
  34. '/pages/index/index',
  35. '/pages/novel/list',
  36. '/pages/bookshelf/index',
  37. '/pages/me/index'
  38. ],
  39. hideMenuRoutes: [
  40. '/login',
  41. '/register',
  42. '/forgot-password',
  43. '/novel/reader'
  44. ]
  45. }
  46. },
  47. mounted() {
  48. this.isMounted = true;
  49. // 详细的环境检查
  50. this.checkEnvironment();
  51. this.checkRouteAuth();
  52. this.initTheme();
  53. this.initStoreState();
  54. this.safeUpdateMenuVisibility();
  55. // 添加全局错误处理
  56. window.addEventListener('error', (event) => {
  57. console.error('全局错误捕获:', event.error);
  58. this.logError(event.error);
  59. });
  60. window.addEventListener('unhandledrejection', (event) => {
  61. console.error('未处理的Promise拒绝:', event.reason);
  62. this.logError(event.reason);
  63. });
  64. },
  65. beforeDestroy() {
  66. this.isMounted = false;
  67. },
  68. watch: {
  69. // 安全监听路由变化
  70. '$route': {
  71. immediate: true,
  72. handler(newRoute) {
  73. if (this.isMounted && newRoute && newRoute.path) {
  74. this.safeUpdateMenuVisibility();
  75. }
  76. }
  77. },
  78. '$store.state.token': {
  79. handler(newToken) {
  80. if (newToken) {
  81. console.log('检测到新token,重新加载数据');
  82. this.reloadData();
  83. }
  84. },
  85. immediate: true
  86. }
  87. },
  88. methods: {
  89. // 环境检查方法
  90. checkEnvironment() {
  91. console.log('开始环境检查...');
  92. // 检查uni对象
  93. if (typeof uni === 'undefined') {
  94. console.error('uni对象未定义,uni-app环境可能未正确初始化');
  95. this.injectFallbackUni();
  96. return;
  97. }
  98. // 检查uni.navigateTo方法
  99. if (typeof uni.navigateTo !== 'function') {
  100. console.error('uni.navigateTo方法不存在');
  101. this.injectFallbackUni();
  102. return;
  103. }
  104. // 检查其他必要的uni API
  105. const requiredMethods = ['showToast', 'navigateBack', 'redirectTo'];
  106. requiredMethods.forEach(method => {
  107. if (typeof uni[method] !== 'function') {
  108. console.warn(`uni.${method}方法不存在`);
  109. }
  110. });
  111. console.log('环境检查完成');
  112. },
  113. // 注入备用uni对象
  114. injectFallbackUni() {
  115. console.log('注入备用uni对象');
  116. // 确保window.uni存在
  117. if (typeof window !== 'undefined' && typeof window.uni === 'undefined') {
  118. window.uni = {
  119. navigateTo: (options) => {
  120. console.log('备用navigateTo被调用:', options);
  121. if (options && options.url) {
  122. // 使用Vue Router进行跳转
  123. if (this.$router && typeof this.$router.push === 'function') {
  124. const path = options.url.split('?')[0];
  125. const query = {};
  126. if (options.url.includes('?')) {
  127. const queryString = options.url.split('?')[1];
  128. queryString.split('&').forEach(param => {
  129. const [key, value] = param.split('=');
  130. query[key] = decodeURIComponent(value);
  131. });
  132. }
  133. this.$router.push({
  134. path: path,
  135. query: query
  136. });
  137. } else {
  138. // 降级到window.location
  139. window.location.href = options.url;
  140. }
  141. }
  142. },
  143. showToast: (options) => {
  144. console.log('备用showToast被调用:', options);
  145. alert(options.title || '提示信息');
  146. },
  147. // 添加其他必要的方法
  148. navigateBack: () => {
  149. if (this.$router && typeof this.$router.back === 'function') {
  150. this.$router.back();
  151. } else {
  152. window.history.back();
  153. }
  154. },
  155. redirectTo: (options) => {
  156. if (options && options.url) {
  157. if (this.$router && typeof this.$router.replace === 'function') {
  158. const path = options.url.split('?')[0];
  159. const query = {};
  160. if (options.url.includes('?')) {
  161. const queryString = options.url.split('?')[1];
  162. queryString.split('&').forEach(param => {
  163. const [key, value] = param.split('=');
  164. query[key] = decodeURIComponent(value);
  165. });
  166. }
  167. this.$router.replace({
  168. path: path,
  169. query: query
  170. });
  171. } else {
  172. window.location.replace(options.url);
  173. }
  174. }
  175. },
  176. getStorageSync: (key) => {
  177. return localStorage.getItem(key);
  178. },
  179. setStorageSync: (key, value) => {
  180. localStorage.setItem(key, value);
  181. }
  182. };
  183. }
  184. },
  185. // 错误日志记录
  186. logError(error) {
  187. // 这里可以添加错误上报逻辑
  188. console.error('记录错误:', error);
  189. // 如果是导航相关错误,尝试修复
  190. if (error.message && error.message.includes('navigate')) {
  191. this.injectFallbackUni();
  192. }
  193. },
  194. // 平台检测
  195. checkPlatform() {
  196. // 检测运行平台
  197. const platform = this.getPlatform();
  198. console.log('当前运行平台:', platform);
  199. // 根据不同平台采取不同策略
  200. if (platform === 'h5') {
  201. this.initH5Environment();
  202. } else if (platform === 'weapp') {
  203. this.initWeappEnvironment();
  204. } else {
  205. this.initDefaultEnvironment();
  206. }
  207. },
  208. getPlatform() {
  209. // 判断当前运行环境
  210. if (typeof wx !== 'undefined' && wx && wx.request) {
  211. return 'weapp'; // 微信小程序
  212. } else if (typeof window !== 'undefined' && window.document) {
  213. return 'h5'; // H5环境
  214. } else if (typeof plus !== 'undefined') {
  215. return 'app'; // 5+App环境
  216. }
  217. return 'unknown';
  218. },
  219. initH5Environment() {
  220. console.log('初始化H5环境');
  221. // H5环境特定初始化
  222. },
  223. initWeappEnvironment() {
  224. console.log('初始化微信小程序环境');
  225. // 微信小程序环境特定初始化
  226. },
  227. initDefaultEnvironment() {
  228. console.log('初始化默认环境');
  229. // 默认环境初始化
  230. },
  231. // 确保 initTheme 方法正确定义
  232. initTheme() {
  233. console.log('主题初始化开始');
  234. // 设置默认主题
  235. const themeName = localStorage.getItem('selectedTheme') || 'aydzBlue';
  236. // 应用主题变量
  237. const themes = {
  238. aydzBlue: {
  239. '--primary-color': '#2a5caa',
  240. '--bg-color': '#e6f7ff',
  241. '--text-color': '#1a3353',
  242. '--card-bg': '#d0e8ff',
  243. '--header-bg': '#2a5caa'
  244. },
  245. default: {
  246. '--primary-color': '#1890ff',
  247. '--bg-color': '#f8f9fa',
  248. '--text-color': '#333',
  249. '--card-bg': '#ffffff'
  250. }
  251. };
  252. const theme = themes[themeName] || themes.default;
  253. // 应用主题变量
  254. Object.keys(theme).forEach(key => {
  255. document.documentElement.style.setProperty(key, theme[key]);
  256. });
  257. console.log('主题初始化完成');
  258. },
  259. reload() {
  260. this.isRouterAlive = false;
  261. this.$nextTick(() => {
  262. this.isRouterAlive = true;
  263. this.safeUpdateMenuVisibility();
  264. });
  265. },
  266. safeUpdateMenuVisibility() {
  267. // 确保组件已挂载且路由对象存在
  268. if (!this.isMounted || !this.$route || !this.$route.path) return;
  269. const currentPath = this.$route.path;
  270. // 检查是否显示底部TabBar
  271. this.showTabBar = this.tabBarPages.some(path =>
  272. currentPath.includes(path) || currentPath === path
  273. );
  274. // 检查是否显示顶部菜单
  275. this.showHeader = !this.hideMenuRoutes.some(route =>
  276. currentPath.includes(route) || currentPath === route
  277. );
  278. },
  279. initStoreState() {
  280. // 使用备用存储方法
  281. const token = (uni && uni.getStorageSync) ? uni.getStorageSync('token') : localStorage.getItem('token') || '';
  282. const readingProgress = (uni && uni.getStorageSync) ? uni.getStorageSync('readingProgress') : localStorage.getItem('readingProgress') || 1;
  283. // 确保 store 存在并正确提交
  284. if (this.$store) {
  285. this.$store.commit('SET_TOKEN', token);
  286. this.$store.commit('SET_READING_PROGRESS', readingProgress);
  287. console.log('Store initialized:', this.$store.state);
  288. } else {
  289. console.error('Store is not available!');
  290. }
  291. },
  292. checkRouteAuth() {
  293. // 简单的路由权限检查
  294. if (!this.$route || !this.$route.path) return;
  295. const authRequiredRoutes = [
  296. '/pages/author/apply',
  297. '/pages/bookshelf/index',
  298. '/pages/me/index'
  299. ];
  300. if (authRequiredRoutes.some(route => this.$route.path.includes(route))) {
  301. if (!this.$store.getters.token) {
  302. // 使用统一的提示方法
  303. if (uni && uni.showToast) {
  304. uni.showToast({ title: '请先登录', icon: 'none' });
  305. } else {
  306. alert('请先登录');
  307. }
  308. if (this.$router) {
  309. this.$router.push('/pages/login');
  310. } else if (uni && uni.navigateTo) {
  311. uni.navigateTo({ url: '/pages/login' });
  312. }
  313. }
  314. }
  315. },
  316. reloadData() {
  317. // 在需要的地方调用此方法重新加载数据
  318. if (this.$route.path === '/pages/novel/list') {
  319. this.$refs.novelList?.initData?.();
  320. }
  321. }
  322. }
  323. }
  324. </script>
  325. <style lang="scss">
  326. @import '@/styles/index.scss';
  327. /* 全局样式修复 */
  328. #app {
  329. min-height: 100vh;
  330. background-color: var(--bg-color);
  331. color: var(--text-color);
  332. }
  333. /* 确保菜单不被其他元素覆盖 */
  334. .custom-tabbar {
  335. z-index: 99999 !important;
  336. position: fixed !important;
  337. bottom: 0 !important;
  338. left: 0 !important;
  339. right: 0 !important;
  340. }
  341. /* 修复页面内容被遮挡的问题 */
  342. .page-content {
  343. padding-bottom: 140rpx !important;
  344. }
  345. /* 确保tabbar显示 */
  346. uni-tabbar {
  347. display: flex !important;
  348. position: fixed;
  349. bottom: 0;
  350. left: 0;
  351. right: 0;
  352. z-index: 9999;
  353. background-color: #fff;
  354. box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
  355. }
  356. /* 修复高度问题 */
  357. uni-tabbar .uni-tabbar {
  358. height: 50px !important;
  359. }
  360. /* 页面内容区域 */
  361. .page-content {
  362. padding-bottom: 60px !important;
  363. }
  364. .app-header {
  365. background: var(--header-bg, #2a5caa);
  366. padding: 10px;
  367. display: flex;
  368. justify-content: space-around;
  369. position: sticky;
  370. top: 0;
  371. z-index: 1000;
  372. a {
  373. color: white;
  374. text-decoration: none;
  375. font-weight: bold;
  376. &.router-link-exact-active {
  377. color: #ffcc00;
  378. border-bottom: 2px solid #ffcc00;
  379. }
  380. }
  381. }
  382. </style>