App.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 openCheckVersion = true // 开启检测版本
  7. const timer = ref(null)
  8. const setIntervalTime = 3000 // 300000 五分钟
  9. function open () {
  10. if (timer.value) clearInterval(timer.value)
  11. timer.value = setInterval(() => { checkVersion() }, setIntervalTime)
  12. }
  13. onMounted(() => {
  14. nextTick(() => {
  15. console.log('baseurl:', import.meta.env?.VITE_BASE_URL, 'version:', import.meta.env?.VITE_VERSION, process?.env?.NODE_ENV) // 打印
  16. const process_ENV = process?.env?.NODE_ENV || ''
  17. if ((process_ENV === 'production' || process_ENV === 'development') && openCheckVersion) {
  18. open()
  19. }
  20. })
  21. })
  22. onUnmounted(() => {
  23. if (timer.value) clearInterval(timer.value)
  24. })
  25. let ConfirmDone = false
  26. // 检查版本号
  27. const checkVersion = () => {
  28. const baseUrl = import.meta.env.VITE_BASE_URL || ''
  29. const version = import.meta.env?.VITE_VERSION || ''
  30. const tenantId = import.meta.env?.VITE_TENANTCODE || ''
  31. //
  32. if (!baseUrl || !version || !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. if (res?.data?.data === version) {
  38. return
  39. }
  40. if (ConfirmDone) {
  41. return
  42. }
  43. ConfirmDone = true
  44. Confirm('系统提示', '发现新版本,将立即刷新页面', { hideCancelBtn: true }).then(() => {
  45. window.location.reload()
  46. })
  47. }).catch(err => {
  48. console.log('checkVersion-err', err)
  49. })
  50. }
  51. </script>
  52. <template>
  53. <v-app>
  54. <RouterView />
  55. </v-app>
  56. </template>
  57. <style scoped>
  58. </style>