App.vue 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <script setup>
  2. import { RouterView } from 'vue-router'
  3. import { ref, onMounted, onUnmounted, nextTick } from 'vue'
  4. import Confirm from '@/plugins/confirm'
  5. import axios from 'axios'
  6. import { vue_version } from './version.js'
  7. import { useSystem } from '@/store/system'
  8. const { setTimeDifference } = useSystem()
  9. const timer = ref(null)
  10. // const setFn = ref()
  11. const setIntervalTime = 3000 // 接口调用间隔时间
  12. function open () {
  13. if (timer.value) clearInterval(timer.value)
  14. timer.value = setInterval(() => { checkVersion() }, setIntervalTime)
  15. }
  16. onMounted(() => {
  17. nextTick(() => {
  18. const process_ENV = import.meta.env.VITE_USER_NODE_ENV || ''
  19. if (process_ENV === 'production') {
  20. console.log(vue_version) // 打印版本号
  21. open()
  22. }
  23. })
  24. })
  25. onUnmounted(() => {
  26. if (timer.value) clearInterval(timer.value)
  27. })
  28. // window.location.reload() // 方法会根据缓存的有效期和修改时间,决定是否重新从服务器下载内容。如果缓存的内容没有过期或没有修改,就会直接使用缓存,这样可以节省流量和时间
  29. // window.location.reload(true) // true参数会忽略缓存的内容,强制重新从服务器下载所有内容.包括 JavaScript 文件,图像,文本文件等。这样可以保证显示网页的最新内容,但是会消耗更多的流量和时间。
  30. // window.location.replace(window.location.href) // 方法会把浏览器中的临时文件夹的文件删除再重新从服务器下载。这样可以清除一些可能造成问题的缓存文件,但是也会消耗更多的流量和时间。
  31. const getVersion = () => {
  32. return new Promise((resolve, reject) => {
  33. const baseUrl = import.meta.env.VITE_BASE_URL || ''
  34. const tenantId = import.meta.env?.VITE_TENANTCODE || ''
  35. axios.get(`${baseUrl}/app-api/menduner/system/get/version`, {
  36. headers: { ['tenant-id']: tenantId },
  37. cache: 'no-store' // 禁用缓存
  38. }).then(resolve)
  39. })
  40. }
  41. getVersion().then(({ data }) => {
  42. const _now = new Date().getTime()
  43. const { time } = data?.data
  44. // 设置浏览器和服务器时间差
  45. setTimeDifference(time - _now)
  46. })
  47. let ConfirmDone = false
  48. // 检查版本号
  49. const checkVersion = () => {
  50. const baseUrl = import.meta.env.VITE_BASE_URL || ''
  51. // const env_v = import.meta.env?.VITE_VERSION || ''
  52. const tenantId = import.meta.env?.VITE_TENANTCODE || ''
  53. const get_v = localStorage.getItem('RES_VERSION') || ''
  54. //
  55. if (!baseUrl || !vue_version || !tenantId) return
  56. getVersion().then(({ data }) => {
  57. const { version, time } = data?.data
  58. if (!version || version === vue_version ) { // 接口报错和版本一致不弹Confirm
  59. return
  60. }
  61. if (ConfirmDone) {
  62. return
  63. }
  64. if (version === get_v && version !== vue_version) { // reload(true)刷新不成功,清除缓存刷新
  65. localStorage.clear()
  66. window.location.replace(window.location.href)
  67. return
  68. }
  69. ConfirmDone = true // 后续不弹Confirm
  70. Confirm('系统提示', '发现新版本,将立即刷新页面', { hideCancelBtn: true }).then(() => {
  71. localStorage.setItem('RES_VERSION', version)
  72. window.location.reload(true)
  73. })
  74. }).catch(err => {
  75. console.log('checkVersion-err', err)
  76. })
  77. }
  78. </script>
  79. <template>
  80. <v-app>
  81. <RouterView />
  82. </v-app>
  83. </template>
  84. <style scoped>
  85. </style>