diff --git a/app/models/mappers/solutionMapper.ts b/app/models/mappers/solutionMapper.ts new file mode 100644 index 0000000..4fd05b1 --- /dev/null +++ b/app/models/mappers/solutionMapper.ts @@ -0,0 +1,54 @@ +/** + * 将 Directus 返回的 Solution 数据转换为 SolutionListView 视图模型 + * + * @param raw: 原始的 Solution 数据 + * @returns 转换后的 SolutionListView 对象 + * + * --- + * + * @example + * const view = toSolutionListView(rawSolution); + */ +export function toSolutionListView(raw: Solution): SolutionListView { + const trans = raw.translations?.[0] ?? { + title: '', + summary: '', + }; + + return { + id: raw.id, + title: trans.title, + summary: trans.summary, + solution_type: isObject(raw.type) + ? raw.type.translations[0].name + : '', + cover: isObject(raw.cover) ? raw.cover.id : raw.cover, + }; +} + +/** + * 将 Directus 返回的 Solution 数据转换为 SolutionView 视图模型 + * + * @param raw: 原始的 Solution 数据 + * @returns 转换后的 SolutionView 对象 + * + * --- + * + * @example + * const view = toSolutionView(rawSolution); + */ +export function toSolutionView(raw: Solution): SolutionView { + const trans = raw.translations?.[0] ?? { + title: '', + summary: '', + content: '', + }; + + return { + id: raw.id, + title: trans.title, + summary: trans.summary, + content: trans.content, + createAt: raw.create_at, + }; +} diff --git a/app/models/views/SolutionListView.ts b/app/models/views/SolutionListView.ts new file mode 100644 index 0000000..fa41cfe --- /dev/null +++ b/app/models/views/SolutionListView.ts @@ -0,0 +1,20 @@ +/** + * 解决方案列表模型 + * 用于解决方案列表(/solutions)渲染的数据结构 + */ +export interface SolutionListView { + /** 唯一标识符 **/ + id: number; + + /** 标题 **/ + title: string; + + /** 摘要 **/ + summary: string; + + /** 解决方案类型 **/ + solution_type: string; + + /** 解决方案封面(图片id) **/ + cover: string; +} diff --git a/app/models/views/SolutionView.ts b/app/models/views/SolutionView.ts new file mode 100644 index 0000000..f5bda88 --- /dev/null +++ b/app/models/views/SolutionView.ts @@ -0,0 +1,20 @@ +/** + * 解决方案模型 + * 用于解决方案页(/solutions/[slug])渲染的数据结构 + */ +export interface SolutionView { + /** 唯一标识符 **/ + id: number; + + /** 标题 **/ + title: string; + + /** 摘要 **/ + summary: string; + + /** 内容 **/ + content: string; + + /** 创建时间 **/ + createAt: string; +} diff --git a/app/pages/solutions/[...slug].vue b/app/pages/solutions/[...slug].vue index 79ac81a..7bfc7f2 100644 --- a/app/pages/solutions/[...slug].vue +++ b/app/pages/solutions/[...slug].vue @@ -25,7 +25,7 @@
CreatedAt: - {{ new Date(solution.createdAt).toLocaleDateString() }} + {{ new Date(solution.createAt).toLocaleDateString() }}
@@ -57,23 +57,17 @@