Yansen 1 year ago
parent
commit
9b0a679375

+ 3
- 3
src/components/NavMenu/index.jsx View File

31
   }, [])
31
   }, [])
32
 
32
 
33
   return (
33
   return (
34
-    <Fragment>
34
+    <div ref={elementRef}>
35
       <nav
35
       <nav
36
         id="pageNav"
36
         id="pageNav"
37
-        ref={elementRef}
37
+        
38
         className="navbar navbar-expand-lg navbar-light bg-light fixed-top p-0"
38
         className="navbar navbar-expand-lg navbar-light bg-light fixed-top p-0"
39
         style={{ '--bs-bg-opacity': 0.9 }}
39
         style={{ '--bs-bg-opacity': 0.9 }}
40
       >
40
       >
93
         menus={current?.children}
93
         menus={current?.children}
94
         onShowChange={setShow}
94
         onShowChange={setShow}
95
       />
95
       />
96
-    </Fragment>
96
+    </div>
97
   );
97
   );
98
 }
98
 }

+ 1
- 1
src/components/NavMenu/style.less View File

1
 
1
 
2
 #sub-menu {
2
 #sub-menu {
3
   visibility: hidden;
3
   visibility: hidden;
4
-  position: absolute;
4
+  position: fixed;
5
   max-height: 0;
5
   max-height: 0;
6
   left: 0;
6
   left: 0;
7
   opacity: 0;
7
   opacity: 0;

src/data/homepage.js → src/data/zh/homepage.js View File


src/data/menu.js → src/data/zh/menu.js View File


+ 32
- 10
src/middleware.js View File

1
-// import { sequence } from "astro:middleware";
2
-// // import { getCategary } from "./services/apis";
3
-// import menus from './data/menu';
1
+import { sequence } from "astro:middleware";
4
 
2
 
5
-// async function getGloalData(context, next) {
6
-//   // const menus = await getCategary();
7
-//   context.locals._menus = menus;
3
+let globalData = null;
8
 
4
 
9
-//   context.data = { ...(context.data || {}), menus };
5
+function getKey(path) {
6
+  // 去掉 .js 后缀
7
+  const parts = path.replace(/\.js$/, '').split('/');
8
+  // 去掉 /data/zh/
9
+  return parts.slice(3).join('.');
10
+}
11
+
12
+async function impData() {
13
+  const data = await import.meta.glob('./data/zh/*.js');
14
+
15
+  Object.keys(data).forEach(async (key) => {
16
+    const dt = await data[key]();
17
+    globalData = {
18
+      ...(globalData || {}),
19
+      [getKey(key)]: dt.default,
20
+    }
21
+  });
22
+}
23
+
24
+async function getGloalData(context, next) {
25
+
26
+  if (!globalData) {
27
+    await impData();
28
+  }
29
+
30
+  context.locals.globalData = globalData;
31
+  console.log('globalData', globalData);
10
   
32
   
11
-//   return next();
12
-// }
33
+  return next();
34
+}
13
 
35
 
14
-// export const onRequest = sequence(getGloalData);
36
+export const onRequest = sequence(getGloalData);

+ 2
- 2
src/pages/[lang]/index.astro View File

10
 import VerticalNavigationContent from '@/components/VerticalNavigationContent.astro'
10
 import VerticalNavigationContent from '@/components/VerticalNavigationContent.astro'
11
 import LeftImgRightText from '@/components/LeftImgRightText.astro'
11
 import LeftImgRightText from '@/components/LeftImgRightText.astro'
12
 import FooterMenu from '@/components/NavMenu/FooterMenu.jsx'
12
 import FooterMenu from '@/components/NavMenu/FooterMenu.jsx'
13
-import menus from '@/data/menu'
14
-import pageData from '@/data/homepage'
13
+import menus from '@/data/zh/menuenu'
14
+import pageData from '@/data/zh/homepageage'
15
 
15
 
16
 import Container from '@/components/Container.astro'
16
 import Container from '@/components/Container.astro'
17
 import MediaCard from '@/components/MediaCard/MediaCard1.astro'
17
 import MediaCard from '@/components/MediaCard/MediaCard1.astro'