App.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = 300000 // 300000 五分钟
  9. onMounted(() => {
  10. nextTick(() => {
  11. console.log('baseurl:', import.meta.env?.VITE_BASE_URL, 'version:', import.meta.env?.VITE_VERSION, process?.env?.NODE_ENV)
  12. //
  13. const process_ENV = process?.env?.NODE_ENV || ''
  14. if ((process_ENV === 'production' || process_ENV === 'development') && openCheckVersion) {
  15. if (timer.value) clearInterval(timer.value)
  16. timer.value = setInterval(() => { checkVersion() }, setIntervalTime)
  17. }
  18. })
  19. })
  20. onUnmounted(() => {
  21. if (timer.value) clearInterval(timer.value)
  22. })
  23. // 检查版本号
  24. const checkVersion = () => {
  25. const baseUrl = import.meta.env.VITE_BASE_URL || ''
  26. const version = import.meta.env?.VITE_VERSION || ''
  27. const tenantId = import.meta.env?.VITE_TENANTCODE || ''
  28. //
  29. if (!baseUrl || !version || !tenantId) return
  30. axios.get(`${baseUrl}/app-api/menduner/system/get/version`, {
  31. headers: { ['tenant-id']: tenantId }
  32. }).then((res) => {
  33. if (res?.data?.data !== version) {
  34. const option = {
  35. autoCloseTime: setIntervalTime > 15000 ? setIntervalTime - 10000 : setIntervalTime >= 5000 ? setIntervalTime - 1000 : setIntervalTime
  36. }
  37. Confirm('系统提示', '发现新版本,是否立即刷新页面', option).then(() => {
  38. window.location.reload()
  39. })
  40. }
  41. }).catch(err => {
  42. console.log('checkVersion-err', err)
  43. })
  44. }
  45. </script>
  46. <template>
  47. <v-app>
  48. <RouterView />
  49. </v-app>
  50. </template>
  51. <style scoped>
  52. </style>