├── 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.

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