App.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. const timer = ref(null)
  7. const setIntervalTime = 3000 // 接口调用间隔时间
  8. function open () {
  9. if (timer.value) clearInterval(timer.value)
  10. timer.value = setInterval(() => { checkVersion() }, setIntervalTime)
  11. }
  12. onMounted(() => {
  13. nextTick(() => {
  14. console.log('baseurl:', import.meta.env?.VITE_BASE_URL, 'version:', import.meta.env?.VITE_VERSION) // 打印
  15. const process_ENV = process?.env?.NODE_ENV || ''
  16. if (process_ENV === 'production' || process_ENV === 'development') {
  17. open()
  18. }
  19. })
  20. })
  21. onUnmounted(() => {
  22. if (timer.value) clearInterval(timer.value)
  23. })
  24. let ConfirmDone = false
  25. // 检查版本号
  26. const checkVersion = () => {
  27. const baseUrl = import.meta.env.VITE_BASE_URL || ''
  28. const env_v = import.meta.env?.VITE_VERSION || ''
  29. const tenantId = import.meta.env?.VITE_TENANTCODE || ''
  30. const get_v = localStorage.getItem('RES_VERSION') || ''
  31. //
  32. if (!baseUrl || !env_v || !tenantId) return
  33. axios.get(`${baseUrl}/app-api/menduner/system/get/version`, {
  34. headers: { ['tenant-id']: tenantId },
  35. cache: 'no-store' // 禁用缓存
  36. }).then((res) => {
  37. const res_v = res?.data?.data || ''
  38. if (!res_v || res_v === env_v ) { // 接口报错和版本一致不弹Confirm
  39. return
  40. }
  41. if (ConfirmDone) {
  42. return
  43. }
  44. if (res_v === get_v) { // 刷新过的不再刷新,避免刷新不成功但一直刷新
  45. return
  46. }
  47. ConfirmDone = true // 后续不弹Confirm
  48. Confirm('系统提示', '发现新版本,将立即刷新页面', { hideCancelBtn: true }).then(() => {
  49. localStorage.setItem('RES_VERSION', res_v)
  50. window.location.reload()
  51. })
  52. }).catch(err => {
  53. console.log('checkVersion-err', err)
  54. })
  55. }
  56. </script>
  57. <template>
  58. <v-app>
  59. <RouterView />
  60. </v-app>
  61. </template>
  62. <style scoped>
  63. </style>