App.vue 2.4 KB

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