App.vue 2.3 KB

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