App.vue 2.0 KB

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