App.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <HcAppConfig>
  3. <router-view />
  4. </HcAppConfig>
  5. </template>
  6. <script setup>
  7. import { nextTick, ref, watch } from 'vue'
  8. import { useAppStore } from '~src/store'
  9. import { useOsTheme } from 'hc-vue3-ui'
  10. import { getObjValue, setElementMainColor } from 'js-fast-way'
  11. import { getStoreValue, setStoreValue } from '~uti/storage'
  12. import { getVersionJson } from '~api/other'
  13. //初始变量
  14. const appStore = useAppStore()
  15. const UserTheme = ref(appStore.getTheme)
  16. //监听
  17. watch(() => [
  18. appStore.getTheme,
  19. appStore.getThemeVal,
  20. appStore.getColor,
  21. ], ([Theme, ThemeVal, ColorVal]) => {
  22. UserTheme.value = Theme
  23. setUserTheme(ThemeVal, ColorVal)
  24. })
  25. nextTick(() => {
  26. setUserTheme(appStore.getThemeVal, appStore.getColor)
  27. //当屏幕分辨率宽度低于1920时,自动折叠菜单
  28. const width = document.body.clientWidth
  29. if (width < 1920) {
  30. appStore.setCollapse(true)
  31. }
  32. //生产环境下,检测更新
  33. if (import.meta.env.PROD && appStore.isSource !== 'app') {
  34. getVersionJsonApi()
  35. //检测新版本
  36. setInterval(() => {
  37. getVersionJsonApi()
  38. }, 1000 * 60)
  39. }
  40. })
  41. //获取版本更新信息
  42. const getVersionJsonApi = async () => {
  43. const cache_version = getStoreValue('version')
  44. const { res } = await getVersionJson()
  45. const version = getObjValue(res)?.value
  46. setStoreValue('version', version)
  47. if (cache_version && cache_version !== version) {
  48. window?.$messageBox?.alert('检测到有新版本更新,请点击更新,或手动刷新网页更新,如果不更新,将无法使用相关功能', '更新提醒', {
  49. showCancelButton: true,
  50. confirmButtonText: '立即更新',
  51. cancelButtonText: '暂不更新',
  52. type: 'warning',
  53. callback: (action) => {
  54. if (action === 'confirm') {
  55. //刷新页面
  56. window.location.reload()
  57. }
  58. },
  59. })
  60. }
  61. }
  62. //设置主题
  63. const setUserTheme = (theme, appColor) => {
  64. const colorVal = getObjValue(appColor)
  65. //设置主色调
  66. setElementMainColor(colorVal?.color)
  67. const colorName = colorVal?.name || 'green'
  68. //设置主题
  69. let val = UserTheme.value
  70. if (val === 'auto') {
  71. theme = useOsTheme()
  72. }
  73. if (theme === '') {
  74. theme = val
  75. }
  76. //挂载相关样式
  77. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  78. }
  79. </script>