feat: 添加产品中心功能并优化博客系统
- 新增产品中心功能,包括产品列表页和详情页 - 实现产品多语言支持(中文、英文、越南语) - 重构博客系统,从API获取改为本地MDX文件管理 - 更新favicon为PNG格式并修复相关引用 - 添加产品类型定义和获取逻辑 - 优化首页应用场景图片和链接 - 完善国际化配置和翻译 - 新增产品详情页标签切换组件 - 修复代理配置中的favicon路径问题
This commit is contained in:
116
lib/getBlogs.ts
116
lib/getBlogs.ts
@@ -1,90 +1,66 @@
|
||||
import { DEFAULT_LOCALE } from '@/i18n/routing';
|
||||
import { BlogPost } from '@/types/blog';
|
||||
import fs from 'fs';
|
||||
import matter from 'gray-matter';
|
||||
import path from 'path';
|
||||
|
||||
export async function getPosts(locale: string = DEFAULT_LOCALE, category?: string): Promise<{ posts: BlogPost[], total: number }> {
|
||||
const Id_Map: Record<string, string> = {
|
||||
'announce': '1989174108560080897',
|
||||
'news': '1988814504336605185',
|
||||
'event': '1989174018231549954',
|
||||
const POSTS_BATCH_SIZE = 10;
|
||||
|
||||
export async function getPosts(locale: string = DEFAULT_LOCALE): Promise<{ posts: BlogPost[] }> {
|
||||
const postsDirectory = path.join(process.cwd(), 'blogs', locale);
|
||||
|
||||
// is directory exist
|
||||
if (!fs.existsSync(postsDirectory)) {
|
||||
return { posts: [] };
|
||||
}
|
||||
|
||||
let url = 'http://49.232.154.205:18081/export/article/list?pageNum=1&pageSize=10&langCode=' + locale;
|
||||
if (category) {
|
||||
url += '&categoryId=' + Id_Map[category || 'announce']
|
||||
}
|
||||
let filenames = await fs.promises.readdir(postsDirectory);
|
||||
filenames = filenames.reverse();
|
||||
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
const posts = data.rows.map((item: any) => {
|
||||
return {
|
||||
locale, // use locale parameter
|
||||
title: item.title,
|
||||
description: item.summary,
|
||||
image: item.cover || '',
|
||||
slug: item.articleId,
|
||||
tags: '',
|
||||
date: item.publishedTime,
|
||||
// visible: data.visible || 'published',
|
||||
pin: false,
|
||||
content: item.content,
|
||||
metadata: data,
|
||||
}
|
||||
|
||||
}) || [];
|
||||
|
||||
// // is directory exist
|
||||
// if (!fs.existsSync(postsDirectory)) {
|
||||
// return { posts: [] };
|
||||
// }
|
||||
|
||||
// let filenames = await fs.promises.readdir(postsDirectory);
|
||||
// filenames = filenames.reverse();
|
||||
|
||||
// let allPosts: BlogPost[] = [];
|
||||
let allPosts: BlogPost[] = [];
|
||||
|
||||
// read file by batch
|
||||
// for (let i = 0; i < filenames.length; i += POSTS_BATCH_SIZE) {
|
||||
// const batchFilenames = filenames.slice(i, i + POSTS_BATCH_SIZE);
|
||||
for (let i = 0; i < filenames.length; i += POSTS_BATCH_SIZE) {
|
||||
const batchFilenames = filenames.slice(i, i + POSTS_BATCH_SIZE);
|
||||
|
||||
// const batchPosts: BlogPost[] = await Promise.all(
|
||||
// batchFilenames.map(async (filename) => {
|
||||
// const fullPath = path.join(postsDirectory, filename);
|
||||
// const fileContents = await fs.promises.readFile(fullPath, 'utf8');
|
||||
const batchPosts: BlogPost[] = await Promise.all(
|
||||
batchFilenames.map(async (filename) => {
|
||||
const fullPath = path.join(postsDirectory, filename);
|
||||
const fileContents = await fs.promises.readFile(fullPath, 'utf8');
|
||||
|
||||
// const { data, content } = matter(fileContents);
|
||||
const { data, content } = matter(fileContents);
|
||||
|
||||
// return {
|
||||
// locale, // use locale parameter
|
||||
// title: data.title,
|
||||
// description: data.description,
|
||||
// image: data.image || '',
|
||||
// slug: data.slug,
|
||||
// tags: data.tags,
|
||||
// date: data.date,
|
||||
// visible: data.visible || 'published',
|
||||
// pin: data.pin || false,
|
||||
// content,
|
||||
// metadata: data,
|
||||
// };
|
||||
// })
|
||||
// );
|
||||
return {
|
||||
locale, // use locale parameter
|
||||
title: data.title,
|
||||
description: data.description,
|
||||
image: data.image || '',
|
||||
slug: data.slug,
|
||||
tags: data.tags,
|
||||
date: data.date,
|
||||
visible: data.visible || 'published',
|
||||
pin: data.pin || false,
|
||||
content,
|
||||
metadata: data,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
// allPosts.push(...batchPosts);
|
||||
// }
|
||||
allPosts.push(...batchPosts);
|
||||
}
|
||||
|
||||
// filter out non-published articles
|
||||
// allPosts = allPosts.filter(post => post.visible === 'published');
|
||||
allPosts = allPosts.filter(post => post.visible === 'published');
|
||||
|
||||
// sort posts by pin and date
|
||||
// allPosts = allPosts.sort((a, b) => {
|
||||
// if (a.pin !== b.pin) {
|
||||
// return (b.pin ? 1 : 0) - (a.pin ? 1 : 0);
|
||||
// }
|
||||
// return new Date(b.date).getTime() - new Date(a.date).getTime();
|
||||
// });
|
||||
allPosts = allPosts.sort((a, b) => {
|
||||
if (a.pin !== b.pin) {
|
||||
return (b.pin ? 1 : 0) - (a.pin ? 1 : 0);
|
||||
}
|
||||
return new Date(b.date).getTime() - new Date(a.date).getTime();
|
||||
});
|
||||
|
||||
return {
|
||||
posts,
|
||||
total: data.total,
|
||||
posts: allPosts,
|
||||
};
|
||||
}
|
||||
57
lib/getProducts.ts
Normal file
57
lib/getProducts.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { DEFAULT_LOCALE } from '@/i18n/routing';
|
||||
import { Product } from '@/types/product';
|
||||
import fs from 'fs';
|
||||
import matter from 'gray-matter';
|
||||
import path from 'path';
|
||||
|
||||
const POSTS_BATCH_SIZE = 10;
|
||||
|
||||
export async function getProducts(locale: string = DEFAULT_LOCALE): Promise<{ products: Product[] }> {
|
||||
const postsDirectory = path.join(process.cwd(), 'content', 'product', locale);
|
||||
|
||||
// is directory exist
|
||||
if (!fs.existsSync(postsDirectory)) {
|
||||
return { products: [] };
|
||||
}
|
||||
|
||||
let filenames = await fs.promises.readdir(postsDirectory);
|
||||
filenames = filenames.reverse();
|
||||
|
||||
let allPosts: Product[] = [];
|
||||
|
||||
// read file by batch
|
||||
for (let i = 0; i < filenames.length; i += POSTS_BATCH_SIZE) {
|
||||
const batchFilenames = filenames.slice(i, i + POSTS_BATCH_SIZE);
|
||||
|
||||
const batchPosts: Product[] = await Promise.all(
|
||||
batchFilenames.map(async (filename) => {
|
||||
const fullPath = path.join(postsDirectory, filename);
|
||||
const fileContents = await fs.promises.readFile(fullPath, 'utf8');
|
||||
|
||||
const { data, content } = matter(fileContents);
|
||||
console.log(data);
|
||||
|
||||
return {
|
||||
locale, // use locale parameter
|
||||
title: data.title,
|
||||
model: data.model,
|
||||
place: data.place,
|
||||
publishedTime: data.publishedTime,
|
||||
images: data.images || [],
|
||||
detail: data.detail,
|
||||
spec: data.spec || [],
|
||||
packaging: data.packaging || '',
|
||||
slug: data.slug || '',
|
||||
content,
|
||||
metadata: data,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
allPosts.push(...batchPosts);
|
||||
}
|
||||
|
||||
return {
|
||||
products: allPosts,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user