App.vue 3.6 KB

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