├── php-api/ # 改造后的PHP接口层 ├── java-ad-service/ # 若依框架微服务(广告+VIP+分账) ├── uniapp-reader/ # UniApp前端项目 │ ├── pages/ # 各端页面 │ └──
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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