From 568701a80ededf7da6759c79595c8d278e009c77 Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Fri, 17 Oct 2025 16:23:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B0=86=E8=A7=A3=E5=86=B3=E6=96=B9?= =?UTF-8?q?=E6=A1=88=E9=A1=B5=E8=BF=81=E7=A7=BB=E8=87=B3directus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将/solutions与/solutions/[slug]页现在由Directus作为CMS - 添加solution页的composable API --- app/models/mappers/solutionMapper.ts | 54 ++++++++++++++++++++++++++++ app/models/views/SolutionListView.ts | 20 +++++++++++ app/models/views/SolutionView.ts | 20 +++++++++++ app/pages/solutions/[...slug].vue | 22 +++++------- app/pages/solutions/index.vue | 39 ++++++++------------ 5 files changed, 117 insertions(+), 38 deletions(-) create mode 100644 app/models/mappers/solutionMapper.ts create mode 100644 app/models/views/SolutionListView.ts create mode 100644 app/models/views/SolutionView.ts 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 @@