App.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <hc-app-config>
  3. <router-view />
  4. </hc-app-config>
  5. </template>
  6. <script setup>
  7. import { nextTick, ref, watch } from 'vue'
  8. import { useAppStore } from '~src/store'
  9. import { detectionBrowser, 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. //生产环境下,检测更新
  28. if (import.meta.env.PROD && appStore.isSource !== 'app') {
  29. detectionBrowser()
  30. getVersionJsonApi()
  31. //检测新版本
  32. setInterval(() => {
  33. getVersionJsonApi()
  34. }, 1000 * 60)
  35. }
  36. })
  37. //获取版本更新信息
  38. const getVersionJsonApi = async () => {
  39. const cache_version = getStoreValue('version')
  40. const { res } = await getVersionJson()
  41. const version = getObjValue(res)?.value
  42. setStoreValue('version', version)
  43. if (cache_version && cache_version !== version) {
  44. window?.$messageBox?.alert('检测到有新版本更新,请点击更新,或手动刷新网页更新,如果不更新,将无法使用相关功能', '更新提醒', {
  45. showCancelButton: true,
  46. confirmButtonText: '立即更新',
  47. cancelButtonText: '暂不更新',
  48. type: 'warning',
  49. callback: (action) => {
  50. if (action === 'confirm') {
  51. //刷新页面
  52. window.location.reload()
  53. }
  54. },
  55. })
  56. }
  57. }
  58. //设置主题
  59. const setUserTheme = (theme, appColor) => {
  60. const colorVal = getObjValue(appColor)
  61. //设置主色调
  62. setElementMainColor(colorVal?.color)
  63. const colorName = colorVal?.name || 'green'
  64. //设置主题
  65. let val = UserTheme.value
  66. if (val === 'auto') {
  67. theme = useOsTheme()
  68. }
  69. if (theme === '') {
  70. theme = val
  71. }
  72. //挂载相关样式
  73. document.documentElement.setAttribute('class', `${theme} color-${colorName}`)
  74. }
  75. </script>