App.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <script setup>
  2. import storage from "@/utils/storage.js";
  3. import { onLaunch, onShow, onHide } from '@dcloudio/uni-app';
  4. import { useStore } from 'vuex';
  5. import { getCurrentInstance } from 'vue';
  6. const store = useStore();
  7. const instance = getCurrentInstance();
  8. // 登录状态检查函数
  9. const checkLoginStatus = () => {
  10. // 登录状态检查,对敏感页面进行拦截
  11. const pages = ['pages/device/index'];
  12. uni.addInterceptor('navigateTo', {
  13. invoke(e) {
  14. const url = e.url;
  15. // 检查是否属于需要登录的页面
  16. const needLogin = pages.some(page => url.indexOf(page) > -1);
  17. if (needLogin && !storage.isLoggedIn()) {
  18. uni.navigateTo({
  19. url: '/pages/login/index'
  20. });
  21. return false;
  22. }
  23. return true;
  24. }
  25. });
  26. uni.addInterceptor('switchTab', {
  27. invoke(e) {
  28. const url = e.url;
  29. // 检查是否属于需要登录的页面
  30. const needLogin = pages.some(page => url.indexOf(page) > -1);
  31. if (needLogin && !storage.isLoggedIn()) {
  32. uni.navigateTo({
  33. url: '/pages/login/index'
  34. });
  35. return false;
  36. }
  37. return true;
  38. }
  39. });
  40. };
  41. // 初始化其他插件(uview-plus 已在 main.js 中注册)
  42. const initPlugins = async () => {
  43. try {
  44. // #ifdef H5
  45. // H5 环境加载 jessibuca 插件
  46. if (typeof window !== 'undefined') {
  47. const jessibucaModule = await import('./utils/jessibuca-plugin');
  48. if (instance && instance.appContext && instance.appContext.app) {
  49. instance.appContext.app.use(jessibucaModule.default);
  50. }
  51. }
  52. // #endif
  53. } catch (error) {
  54. console.error('插件初始化失败:', error);
  55. }
  56. };
  57. // uni-app 生命周期钩子 (保持原有命名)
  58. onLaunch(() => {
  59. console.log('App Launch');
  60. // 初始化插件
  61. initPlugins();
  62. // 初始化 store 状态
  63. store.dispatch('init');
  64. checkLoginStatus();
  65. });
  66. onShow(() => {
  67. console.log('App Show');
  68. });
  69. onHide(() => {
  70. console.log('App Hide');
  71. });
  72. </script>
  73. <!-- App.vue中不需要template,uni-app会自动处理页面跳转 -->
  74. <style lang="scss">
  75. /* 全局基础样式 */
  76. @import "uview-plus/index.scss";
  77. page {
  78. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  79. font-size: 28rpx;
  80. line-height: 1.5;
  81. color: #333;
  82. background-color: #f5f5f5;
  83. }
  84. /* 去除按钮默认边框 */
  85. button::after {
  86. border: none;
  87. }
  88. /* 隐藏滚动条 */
  89. ::-webkit-scrollbar {
  90. width: 0;
  91. height: 0;
  92. color: transparent;
  93. }
  94. /* H5环境特殊样式 */
  95. /* #ifdef H5 */
  96. html, body {
  97. height: 100%;
  98. width: 100%;
  99. overflow-x: hidden;
  100. }
  101. .uni-page-head {
  102. display: flex !important;
  103. }
  104. /* #endif */
  105. </style>