feat: 将首页由Strapi迁移至Directus
- 相关路由界面修改 - 增添响应的视图模型与转换方法
This commit is contained in:
51
app/models/mappers/homepageMapper.ts
Normal file
51
app/models/mappers/homepageMapper.ts
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* 将 Directus 返回的 Homepage 数据转换为 HomepageView 视图模型
|
||||
*
|
||||
* @param raw: 原始的 Homepage 数据
|
||||
* @returns 转换后的 HomepageView 对象
|
||||
*
|
||||
* @example
|
||||
* const view = toHomepageView(rawHomepage);
|
||||
*/
|
||||
export function toHomepageView(raw: Homepage): HomepageView {
|
||||
const carousel = (raw.carousel ?? [])
|
||||
.filter(isObject<HomepageFile>)
|
||||
.map((item) => item.directus_files_id)
|
||||
.filter((item) => typeof item === 'string');
|
||||
|
||||
const products = (raw.recommend_products ?? [])
|
||||
.filter(isObject<Product>)
|
||||
.map((item) => {
|
||||
const cover = isObject<DirectusFile>(item.cover)
|
||||
? item.cover.id
|
||||
: item.cover;
|
||||
return {
|
||||
id: item.id,
|
||||
name: item.translations?.[0].name,
|
||||
summary: item.translations?.[0].summary,
|
||||
cover: cover,
|
||||
} satisfies HomepageProductView;
|
||||
});
|
||||
|
||||
const solutions = (raw.recommend_solutions ?? [])
|
||||
.filter(isObject<Solution>)
|
||||
.map((item) => {
|
||||
const cover = isObject<DirectusFile>(item.cover)
|
||||
? item.cover.id
|
||||
: item.cover;
|
||||
|
||||
return {
|
||||
id: item.id,
|
||||
title: item.translations?.[0].title,
|
||||
summary: item.translations?.[0].summary,
|
||||
cover: cover,
|
||||
} satisfies HomepageSolutionView;
|
||||
});
|
||||
|
||||
return {
|
||||
id: raw.id,
|
||||
carousel: carousel ?? [],
|
||||
recommendProducts: products ?? [],
|
||||
recommendSolutions: solutions ?? [],
|
||||
};
|
||||
}
|
||||
50
app/models/views/HomepageView.ts
Normal file
50
app/models/views/HomepageView.ts
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 主页推荐产品视图模型
|
||||
*/
|
||||
export interface HomepageProductView {
|
||||
/** 唯一标识符 **/
|
||||
id: number;
|
||||
|
||||
/** 产品名称 **/
|
||||
name: string;
|
||||
|
||||
/** 产品简介 **/
|
||||
summary: string;
|
||||
|
||||
/** 产品封面 **/
|
||||
cover: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主页推荐解决方案视图模型
|
||||
*/
|
||||
export interface HomepageSolutionView {
|
||||
/** 唯一标识符 **/
|
||||
id: number;
|
||||
|
||||
/** 解决方案标题 **/
|
||||
title: string;
|
||||
|
||||
/** 解决方案摘要 **/
|
||||
summary: string;
|
||||
|
||||
/** 解决方案封面 **/
|
||||
cover: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主页视图模型
|
||||
*/
|
||||
export interface HomepageView {
|
||||
/** 唯一标识符 **/
|
||||
id: number;
|
||||
|
||||
/** 首页图片 **/
|
||||
carousel: string[];
|
||||
|
||||
/** 首页推荐产品 **/
|
||||
recommendProducts: HomepageProductView[];
|
||||
|
||||
/** 首页推荐解决方案 **/
|
||||
recommendSolutions: HomepageSolutionView[];
|
||||
}
|
||||
Reference in New Issue
Block a user