App.vue 2.5 KB

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