浏览代码

feat: 添加 sitemap.ts 动态生成站点地图并更新生产环境域名配置

nahida 1 月之前
父节点
当前提交
c567541d6c
共有 2 个文件被更改,包括 91 次插入1 次删除
  1. 2 1
      .env.production
  2. 89 0
      src/app/sitemap.ts

+ 2 - 1
.env.production

@@ -2,4 +2,5 @@ NEXT_PUBLIC_API_BASE=http://localhost:8040
 NEXT_PUBLIC_BASE_URL=http://localhost:8040
 NEXT_PUBLIC_REMOTE_BASE_URL=http://47.107.107.47:8040
 NEXT_PUBLIC_BASE_WS_URL = 'ws://47.107.107.47:8081/ws'
-NEXT_PUBLIC_CLIENT_URL=http://47.107.107.47:8040
+NEXT_PUBLIC_CLIENT_URL=http://47.107.107.47:8040
+NEXT_PUBLIC_DOMAIN=http://www.hnsoyon.com

+ 89 - 0
src/app/sitemap.ts

@@ -0,0 +1,89 @@
+import { MetadataRoute } from 'next'
+import { serverGet, serverPost } from "@/utils/request"
+
+export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
+  const baseUrl = process.env.NEXT_PUBLIC_DOMAIN || 'http://www.hnsoyon.com'
+
+  // 1. 静态路由
+  const staticRoutes: MetadataRoute.Sitemap = [
+    '',
+    '/about',
+    '/about/recruitList',
+    '/business',
+    '/news',
+    '/products',
+    '/solutions',
+    '/support',
+  ].map((route) => ({
+    url: `${baseUrl}${route}`,
+    lastModified: new Date(),
+    changeFrequency: route === '' ? 'daily' : 'weekly',
+    priority: route === '' ? 1 : 0.8,
+  }))
+
+  // 2. 动态路由获取
+  const dynamicRoutes: MetadataRoute.Sitemap = []
+
+  // 获取新闻列表
+  try {
+    const newsRes = await serverGet<any>("/webSite/getNewsUpdatesListWithoutSpecialNews", { pageNum: 1, pageSize: 100 })
+    const specialNewsRes = await serverGet<any>("/webSite/getSpecialNewsUpdatesList", { pageNum: 1, pageSize: 20 })
+    const newsRecords = [
+      ...(newsRes.code === 200 ? (newsRes.data?.records || []) : []),
+      ...(specialNewsRes.code === 200 ? (specialNewsRes.data?.records || []) : [])
+    ]
+    const newsUrls: MetadataRoute.Sitemap = newsRecords.map((item: any) => ({
+      url: `${baseUrl}/news/${item.id}`,
+      lastModified: item.updateTime ? new Date(item.updateTime) : new Date(),
+      changeFrequency: 'monthly' as const,
+      priority: 0.6,
+    }))
+    dynamicRoutes.push(...newsUrls)
+  } catch (error) {
+    console.error("生成 Sitemap 时获取新闻列表失败:", error)
+  }
+
+  // 获取产品列表
+  try {
+    const productRes = await serverGet<any>("/webSite/getProductByPage", { pageNum: 1, pageSize: 100 })
+    const productUrls: MetadataRoute.Sitemap = (productRes.code === 200 ? (productRes.data?.records || []) : []).map((item: any) => ({
+      url: `${baseUrl}/products/${item.id}`,
+      lastModified: item.updateTime ? new Date(item.updateTime) : new Date(),
+      changeFrequency: 'monthly' as const,
+      priority: 0.7,
+    }))
+    dynamicRoutes.push(...productUrls)
+  } catch (error) {
+    console.error("生成 Sitemap 时获取产品列表失败:", error)
+  }
+
+  // 获取解决方案列表
+  try {
+    const solutionRes = await serverGet<any>("/webSite/getSolution", { pageNum: 1, pageSize: 100 })
+    const solutionUrls: MetadataRoute.Sitemap = (solutionRes.code === 200 ? (solutionRes.data?.records || []) : []).map((item: any) => ({
+      url: `${baseUrl}/solutions/${item.id}`,
+      lastModified: item.updateTime ? new Date(item.updateTime) : new Date(),
+      changeFrequency: 'monthly' as const,
+      priority: 0.7,
+    }))
+    dynamicRoutes.push(...solutionUrls)
+  } catch (error) {
+    console.error("生成 Sitemap 时获取解决方案失败:", error)
+  }
+
+  // 获取招聘列表 (POST 请求)
+  try {
+    const recruitRes = await serverPost<any>("/webSite/getRecruitmentInfoByPage", { pageNum: 1, pageSize: 50 })
+    const recruitUrls: MetadataRoute.Sitemap = (recruitRes.code === 200 ? (recruitRes.data?.records || []) : []).map((item: any) => ({
+      url: `${baseUrl}/about/recruitList/${item.id}`,
+      lastModified: item.updateTime ? new Date(item.updateTime) : new Date(),
+      changeFrequency: 'monthly' as const,
+      priority: 0.5,
+    }))
+    dynamicRoutes.push(...recruitUrls)
+  } catch (error) {
+    console.error("生成 Sitemap 时获取招聘列表失败:", error)
+  }
+
+  return [...staticRoutes, ...dynamicRoutes]
+}