App.vue 2.3 KB

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