App.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <div id="app">
  3. <router-view />
  4. <cache-viewer v-if="isCDNEnabled" :visible.sync="showCacheViewer" />
  5. </div>
  6. </template>
  7. <style lang="scss">
  8. #app {
  9. font-family: Avenir, Helvetica, Arial, sans-serif;
  10. -webkit-font-smoothing: antialiased;
  11. -moz-osx-font-smoothing: grayscale;
  12. text-align: center;
  13. color: #2c3e50;
  14. }
  15. nav {
  16. padding: 30px;
  17. a {
  18. font-weight: bold;
  19. color: #2c3e50;
  20. &.router-link-exact-active {
  21. color: #42b983;
  22. }
  23. }
  24. }
  25. .copyright {
  26. padding: 0 !important;
  27. color: rgb(0, 0, 0);
  28. font-size: 12px;
  29. font-weight: 400;
  30. margin-top: auto;
  31. width: 100%;
  32. height: 100%;
  33. display: flex;
  34. justify-content: center;
  35. align-items: center;
  36. }
  37. .el-message {
  38. top: 70px !important;
  39. }
  40. </style>
  41. <script>
  42. import CacheViewer from '@/components/CacheViewer.vue';
  43. import { logCacheStatus } from '@/utils/cacheViewer';
  44. export default {
  45. name: 'App',
  46. components: {
  47. CacheViewer
  48. },
  49. data() {
  50. return {
  51. showCacheViewer: false,
  52. isCDNEnabled: process.env.VUE_APP_USE_CDN === 'true'
  53. };
  54. },
  55. created() {
  56. // 挂载 store 状态
  57. this.$store.commit('setUserInfo', JSON.parse(localStorage.getItem('userInfo') || '{}'));
  58. this.$store.commit('setPubConfig', JSON.parse(localStorage.getItem('pubConfig') || '{}'));
  59. },
  60. mounted() {
  61. // 检测是否为移动设备且VUE_APP_H5_URL不为空,如果两个条件都满足则跳转到H5页面
  62. if (this.isMobileDevice() && process.env.VUE_APP_H5_URL) {
  63. window.location.href = process.env.VUE_APP_H5_URL;
  64. return;
  65. }
  66. // 只有在启用CDN时才添加相关事件和功能
  67. if (this.isCDNEnabled) {
  68. // 添加全局快捷键Alt+C用于显示缓存查看器
  69. document.addEventListener('keydown', this.handleKeyDown);
  70. // 在全局对象上添加缓存检查方法,便于调试
  71. window.checkCDNCacheStatus = () => {
  72. this.showCacheViewer = true;
  73. };
  74. // 在控制台输出提示信息
  75. console.info(
  76. '%c[' + this.$t('system.name') + '] ' + this.$t('cache.cdnEnabled'),
  77. 'color: #409EFF; font-weight: bold;'
  78. );
  79. console.info(
  80. '按下 Alt+C 组合键或在控制台运行 checkCDNCacheStatus() 可以查看CDN缓存状态'
  81. );
  82. // 检查Service Worker状态
  83. this.checkServiceWorkerStatus();
  84. } else {
  85. console.info(
  86. '%c[' + this.$t('system.name') + '] ' + this.$t('cache.cdnDisabled'),
  87. 'color: #67C23A; font-weight: bold;'
  88. );
  89. }
  90. },
  91. beforeDestroy() {
  92. // 只有在启用CDN时才需要移除事件监听
  93. if (this.isCDNEnabled) {
  94. document.removeEventListener('keydown', this.handleKeyDown);
  95. }
  96. },
  97. methods: {
  98. handleKeyDown(e) {
  99. // Alt+C 快捷键
  100. if (e.altKey && e.key === 'c') {
  101. this.showCacheViewer = true;
  102. }
  103. },
  104. isMobileDevice() {
  105. // 检测是否为移动设备的函数
  106. return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
  107. },
  108. async checkServiceWorkerStatus() {
  109. // 检查Service Worker是否已注册
  110. if ('serviceWorker' in navigator) {
  111. try {
  112. const registrations = await navigator.serviceWorker.getRegistrations();
  113. if (registrations.length > 0) {
  114. console.info(
  115. '%c[' + this.$t('system.name') + '] ' + this.$t('cache.serviceWorkerRegistered'),
  116. 'color: #67C23A; font-weight: bold;'
  117. );
  118. // 输出缓存状态到控制台
  119. setTimeout(async () => {
  120. const hasCaches = await logCacheStatus();
  121. if (!hasCaches) {
  122. console.info(
  123. '%c[' + this.$t('system.name') + '] ' + this.$t('cache.noCacheDetected'),
  124. 'color: #E6A23C; font-weight: bold;'
  125. );
  126. // 开发环境下提供额外提示
  127. if (process.env.NODE_ENV === 'development') {
  128. console.info(
  129. '%c[' + this.$t('system.name') + '] ' + this.$t('cache.swDevEnvWarning'),
  130. 'color: #E6A23C; font-weight: bold;'
  131. );
  132. console.info(this.$t('cache.swCheckMethods'));
  133. console.info('1. ' + this.$t('cache.swCheckMethod1'));
  134. console.info('2. ' + this.$t('cache.swCheckMethod2'));
  135. console.info('3. ' + this.$t('cache.swCheckMethod3'));
  136. }
  137. }
  138. }, 2000);
  139. } else {
  140. console.info(
  141. '%c[' + this.$t('system.name') + '] ' + this.$t('cache.serviceWorkerNotRegistered'),
  142. 'color: #F56C6C; font-weight: bold;'
  143. );
  144. if (process.env.NODE_ENV === 'development') {
  145. console.info(
  146. '%c[' + this.$t('system.name') + '] ' + this.$t('cache.swDevEnvNormal'),
  147. 'color: #E6A23C; font-weight: bold;'
  148. );
  149. console.info(this.$t('cache.swProdOnly'));
  150. console.info(this.$t('cache.swTestingTitle'));
  151. console.info('1. ' + this.$t('cache.swTestingStep1'));
  152. console.info('2. ' + this.$t('cache.swTestingStep2'));
  153. }
  154. }
  155. } catch (error) {
  156. console.error('检查Service Worker状态失败:', error);
  157. }
  158. } else {
  159. console.warn(this.$t('cache.swNotSupported'));
  160. }
  161. }
  162. }
  163. };
  164. </script>