index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <script setup lang="ts">
  2. import "animate.css";
  3. // 引入 src/components/ReIcon/src/offlineIcon.ts 文件中所有使用addIcon添加过的本地图标
  4. import "@/components/ReIcon/src/offlineIcon";
  5. import { setType } from "./types";
  6. import { useI18n } from "vue-i18n";
  7. import { useLayout } from "./hooks/useLayout";
  8. import { useAppStoreHook } from "@/store/modules/app";
  9. import { useSettingStoreHook } from "@/store/modules/settings";
  10. import { useDataThemeChange } from "@/layout/hooks/useDataThemeChange";
  11. import {
  12. h,
  13. ref,
  14. reactive,
  15. computed,
  16. onMounted,
  17. onBeforeMount,
  18. defineComponent
  19. } from "vue";
  20. import {
  21. useDark,
  22. useGlobal,
  23. deviceDetection,
  24. useResizeObserver
  25. } from "@pureadmin/utils";
  26. import LayTag from "./components/lay-tag/index.vue";
  27. import LayNavbar from "./components/lay-navbar/index.vue";
  28. import LayContent from "./components/lay-content/index.vue";
  29. import LaySetting from "./components/lay-setting/index.vue";
  30. import NavVertical from "./components/lay-sidebar/NavVertical.vue";
  31. import NavHorizontal from "./components/lay-sidebar/NavHorizontal.vue";
  32. import BackTopIcon from "@/assets/svg/back_top.svg?component";
  33. import { fetchSysCfg } from "@/api/system";
  34. const { t } = useI18n();
  35. const appWrapperRef = ref();
  36. const { isDark } = useDark();
  37. const { layout } = useLayout();
  38. const isMobile = deviceDetection();
  39. const pureSetting = useSettingStoreHook();
  40. const { $storage } = useGlobal<GlobalPropertiesApi>();
  41. import { provide } from "vue";
  42. // 创建响应式全局变量(单例)
  43. const globalState = reactive(window.vueGlobal);
  44. provide("vGlobal", globalState);
  45. const vGlobal = window.vueGlobal;
  46. const set: setType = reactive({
  47. sidebar: computed(() => {
  48. return useAppStoreHook().sidebar;
  49. }),
  50. device: computed(() => {
  51. return useAppStoreHook().device;
  52. }),
  53. fixedHeader: computed(() => {
  54. return pureSetting.fixedHeader;
  55. }),
  56. classes: computed(() => {
  57. return {
  58. hideSidebar: !set.sidebar.opened,
  59. openSidebar: set.sidebar.opened,
  60. withoutAnimation: set.sidebar.withoutAnimation,
  61. mobile: set.device === "mobile"
  62. };
  63. }),
  64. hideTabs: computed(() => {
  65. return $storage?.configure.hideTabs;
  66. })
  67. });
  68. function setTheme(layoutModel: string) {
  69. window.document.body.setAttribute("layout", layoutModel);
  70. $storage.layout = {
  71. layout: `${layoutModel}`,
  72. theme: $storage.layout?.theme,
  73. darkMode: $storage.layout?.darkMode,
  74. sidebarStatus: $storage.layout?.sidebarStatus,
  75. epThemeColor: $storage.layout?.epThemeColor,
  76. themeColor: $storage.layout?.themeColor,
  77. overallStyle: $storage.layout?.overallStyle
  78. };
  79. }
  80. function toggle(device: string, bool: boolean) {
  81. useAppStoreHook().toggleDevice(device);
  82. useAppStoreHook().toggleSideBar(bool, "resize");
  83. }
  84. // 判断是否可自动关闭菜单栏
  85. let isAutoCloseSidebar = true;
  86. useResizeObserver(appWrapperRef, entries => {
  87. if (isMobile) return;
  88. const entry = entries[0];
  89. // 兼容处理:优先使用borderBoxSize,若获取失败则用contentRect
  90. const borderBoxSize = entry.borderBoxSize || [];
  91. const width = borderBoxSize[0]?.inlineSize || entry.contentRect.width;
  92. const height = borderBoxSize[0]?.blockSize || entry.contentRect.height;
  93. useAppStoreHook().setViewportSize({ width, height });
  94. width <= 760 ? setTheme("vertical") : setTheme(useAppStoreHook().layout);
  95. /** width app-wrapper类容器宽度
  96. * 0 < width <= 760 隐藏侧边栏
  97. * 760 < width <= 990 折叠侧边栏
  98. * width > 990 展开侧边栏
  99. */
  100. if (width > 0 && width <= 760) {
  101. toggle("mobile", false);
  102. isAutoCloseSidebar = true;
  103. } else if (width > 760 && width <= 990) {
  104. if (isAutoCloseSidebar) {
  105. toggle("desktop", false);
  106. isAutoCloseSidebar = false;
  107. }
  108. } else if (width > 990 && !set.sidebar.isClickCollapse) {
  109. toggle("desktop", true);
  110. isAutoCloseSidebar = true;
  111. } else {
  112. toggle("desktop", false);
  113. isAutoCloseSidebar = false;
  114. }
  115. });
  116. onMounted(() => {
  117. if (isMobile) {
  118. toggle("mobile", false);
  119. }
  120. });
  121. onBeforeMount(() => {
  122. if (!vGlobal.isInited()) {
  123. console.log("vGlobal 未初始化,进行初始化...");
  124. vGlobal.init(() => {
  125. console.log("vGlobal Step:", vGlobal.stepInit);
  126. });
  127. const ci = setInterval(() => {
  128. if (vGlobal.isInited()) {
  129. clearInterval(ci);
  130. console.log("vGlobal 初始化完成");
  131. // fetchSysCfg({}).then((response) => {
  132. // console.log('系统配置项:', response);
  133. // if (response.success) {
  134. // vGlobal.sysCfg.init(response.dict);
  135. // vGlobal.applySysCfg();
  136. // }
  137. // })
  138. }
  139. }, 100);
  140. }
  141. useDataThemeChange().dataThemeChange($storage.layout?.overallStyle);
  142. });
  143. const LayHeader = defineComponent({
  144. name: "LayHeader",
  145. render() {
  146. return h(
  147. "div",
  148. {
  149. class: { "fixed-header": set.fixedHeader },
  150. // style: [
  151. // set.hideTabs && layout.value.includes("horizontal")
  152. // ? isDark.value
  153. // ? "box-shadow: 0 1px 4px #0d0d0d"
  154. // : "box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08)"
  155. // : ""
  156. // ]
  157. // 简化样式判断,避免复杂条件导致的解析问题
  158. style:
  159. set.hideTabs && layout.value.includes("horizontal")
  160. ? isDark.value
  161. ? { boxShadow: "0 1px 4px #0d0d0d" } // 使用对象形式更兼容
  162. : { boxShadow: "0 1px 4px rgba(0, 21, 41, 0.08)" }
  163. : {}
  164. },
  165. {
  166. default: () => [
  167. !pureSetting.hiddenSideBar &&
  168. (layout.value.includes("vertical") || layout.value.includes("mix"))
  169. ? h(LayNavbar)
  170. : null,
  171. !pureSetting.hiddenSideBar && layout.value.includes("horizontal")
  172. ? h(NavHorizontal)
  173. : null,
  174. h(LayTag)
  175. ]
  176. }
  177. );
  178. }
  179. });
  180. </script>
  181. <template>
  182. <div ref="appWrapperRef" :class="['app-wrapper', set.classes]">
  183. <div
  184. v-show="
  185. set.device === 'mobile' &&
  186. set.sidebar.opened &&
  187. layout.includes('vertical')
  188. "
  189. class="app-mask"
  190. @click="useAppStoreHook().toggleSideBar()"
  191. />
  192. <NavVertical
  193. v-show="
  194. !pureSetting.hiddenSideBar &&
  195. (layout.includes('vertical') || layout.includes('mix'))
  196. "
  197. />
  198. <div
  199. :class="[
  200. 'main-container',
  201. pureSetting.hiddenSideBar ? 'main-hidden' : ''
  202. ]"
  203. >
  204. <div v-if="set.fixedHeader">
  205. <LayHeader />
  206. <!-- 主体内容 -->
  207. <LayContent :fixed-header="set.fixedHeader" />
  208. </div>
  209. <el-scrollbar v-else>
  210. <el-backtop
  211. :title="t('buttons.pureBackTop')"
  212. target=".main-container .el-scrollbar__wrap"
  213. >
  214. <BackTopIcon />
  215. </el-backtop>
  216. <LayHeader />
  217. <!-- 主体内容 -->
  218. <LayContent :fixed-header="set.fixedHeader" />
  219. </el-scrollbar>
  220. </div>
  221. <!-- 系统设置 -->
  222. <LaySetting />
  223. </div>
  224. </template>
  225. <style lang="scss" scoped>
  226. .app-wrapper {
  227. position: relative;
  228. width: 100%;
  229. height: 100vh; // 确保全屏高度
  230. overflow: hidden;
  231. &::after {
  232. clear: both;
  233. display: table;
  234. content: "";
  235. }
  236. &.mobile.openSidebar {
  237. position: fixed;
  238. top: 0;
  239. }
  240. }
  241. .main-container {
  242. height: 100%;
  243. // 确保滚动容器占满剩余高度
  244. .el-scrollbar {
  245. height: 100%;
  246. .el-scrollbar__wrap {
  247. height: 100% !important; // 强制覆盖默认样式
  248. }
  249. }
  250. }
  251. .app-mask {
  252. position: absolute;
  253. top: 0;
  254. z-index: 2001;
  255. width: 100%;
  256. height: 100%;
  257. background: #000;
  258. opacity: 0.3;
  259. }
  260. .re-screen {
  261. margin-top: 12px;
  262. }
  263. </style>