App.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <script setup>
  2. import { ref, onMounted, nextTick, watch, onBeforeUnmount, getCurrentInstance } from 'vue';
  3. import { useI18n } from "vue-i18n";
  4. const { locale } = useI18n();
  5. import {
  6. onLoad,
  7. onShow,
  8. onLaunch
  9. } from '@dcloudio/uni-app'
  10. import {
  11. updateRoute
  12. } from "@/hooks/useRoute";
  13. import useGlobalStore from "@/stores/use-global-store";
  14. // import { useAppUpdate } from '@/hooks/useAppUpdate'
  15. // const { checkUpdate } = useAppUpdate()
  16. const globalStore = useGlobalStore()
  17. onLoad((options) => {
  18. updateRoute();
  19. // checkUpdate()
  20. })
  21. onShow((options) => {
  22. updateRoute();
  23. // checkUpdate()
  24. })
  25. // App.vue 或你的初始化文件中
  26. function initTheme() {
  27. // #ifdef H5
  28. // H5 端:使用 matchMedia 主动获取当前系统主题
  29. const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
  30. const theme = isDarkMode ? 'dark' : 'light'
  31. globalStore.setGlobalTheme(theme)
  32. // 监听变化(你的 onThemeChange 已经能工作,但可以再加一层保险)
  33. const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)')
  34. const handleChange = (e) => {
  35. const newTheme = e.matches ? 'dark' : 'light'
  36. globalStore.setGlobalTheme(newTheme)
  37. }
  38. // 兼容旧版浏览器
  39. if (darkModeQuery.addEventListener) {
  40. darkModeQuery.addEventListener('change', handleChange)
  41. } else {
  42. darkModeQuery.addListener(handleChange)
  43. }
  44. // 设置 data-bs-theme 属性到 html 标签
  45. document.documentElement.setAttribute('data-bs-theme', theme);
  46. document.documentElement.setAttribute('data-color-theme', 'blue');
  47. // 同时设置到 body 标签
  48. document.body.setAttribute('data-bs-theme', theme);
  49. document.body.setAttribute('data-color-theme', 'blue');
  50. // #endif
  51. // #ifdef APP-PLUS
  52. // App 端:使用原生 API
  53. uni.getSystemInfo({
  54. success: (res) => {
  55. let theme = res.osTheme || 'light'
  56. globalStore.setGlobalTheme(theme)
  57. }
  58. })
  59. uni.onThemeChange((res) => {
  60. globalStore.setGlobalTheme(res.theme)
  61. })
  62. // #endif
  63. }
  64. onLaunch((options) => {
  65. // updateRoute();
  66. // checkUpdate()
  67. // 调用初始化
  68. // initTheme()
  69. })
  70. watch(locale, () => {
  71. // const currentPath = route.path;
  72. // menu.value.forEach((item, index) => {
  73. // if (item.children) {
  74. // const isActive = item.children.some(child => child.path.includes(currentPath));
  75. // menu.value[index].isOpenMenu = isActive;
  76. // if (isActive) {
  77. // nextTick(() => {
  78. // updateSubmenuHeight(index);
  79. // });
  80. // }
  81. // }
  82. // });
  83. }, { immediate: true })
  84. onMounted(() => {
  85. const sysInfo = uni.getSystemInfoSync();
  86. globalStore.setBarHeight(sysInfo.statusBarHeight || 60);
  87. // ---------- 新增 H5 端专属初始化 ----------
  88. // 仅在 H5 端执行(通过环境判断)
  89. // #ifdef H5
  90. if (typeof window !== 'undefined') {
  91. const instance = getCurrentInstance()
  92. if (instance) {
  93. window.vm = instance.proxy
  94. }
  95. }
  96. // #endif
  97. });
  98. </script>
  99. <style>
  100. /*每个页面公共css */
  101. </style>
  102. <style lang="scss">
  103. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  104. @import "uview-plus/index.scss";
  105. @import "@/static/scss/global/global.scss";
  106. @import "@/static/scss/global/vu.css";
  107. @import "/static/scss/style.scss";
  108. @font-face {
  109. font-family: 'Google Sans';
  110. src: url('/static/Google_Sans/GoogleSans-VariableFont_GRAD,opsz,wght.ttf') format('truetype-variations');
  111. font-weight: 100 900;
  112. font-style: normal;
  113. font-display: swap;
  114. }
  115. /* 全局字体,不破坏 uni-icons 图标 */
  116. view,
  117. text,
  118. button,
  119. input,
  120. textarea,
  121. label {
  122. font-family: 'Google Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  123. font-weight: 400;
  124. }
  125. /* 专门保护 uni-icons 不被覆盖 */
  126. .uni-icon,
  127. [class*="uni-icons-"],
  128. .uni-icons {
  129. font-family: uniicons !important;
  130. }
  131. /* 让整个项目文字都能选中 */
  132. * {
  133. -webkit-user-select: text !important;
  134. user-select: text !important;
  135. }
  136. /* 修复滚动层无法选中 */
  137. view,
  138. text,
  139. div,
  140. span {
  141. -webkit-user-select: text !important;
  142. user-select: text !important;
  143. }
  144. /* 强制修复 uni-datetime-picker 重复渲染双日历 */
  145. // .uni-calendar+.uni-calendar {
  146. // display: none !important;
  147. // }
  148. :deep(.u-toolbar__wrapper__confirm) {
  149. font-size: 20px !important;
  150. }
  151. :deep(.u-toolbar__wrapper__cancel) {
  152. font-size: 20px !important;
  153. }
  154. .page {
  155. /* padding: 31px 31px 110px 31px; */
  156. box-sizing: border-box;
  157. /* background: var(--main-bg); */
  158. }
  159. html {
  160. --bs-bg-opacity: 1;
  161. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  162. font-size: 16px !important;
  163. }
  164. uni-page-body {
  165. height: 100%;
  166. }
  167. page {
  168. --bs-bg-opacity: 1;
  169. background-color: rgba(var(--bs-body-bg-rgb), var(--bs-bg-opacity)) !important;
  170. }
  171. </style>