feat: 将解决方案页迁移至directus

- 将/solutions与/solutions/[slug]页现在由Directus作为CMS
- 添加solution页的composable API
This commit is contained in:
2025-10-17 16:23:48 +08:00
parent 9abe6431a6
commit 568701a80e
5 changed files with 117 additions and 38 deletions

View File

@ -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<SolutionType>(raw.type)
? raw.type.translations[0].name
: '',
cover: isObject<DirectusFile>(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,
};
}

View File

@ -0,0 +1,20 @@
/**
* 解决方案列表模型
* 用于解决方案列表(/solutions)渲染的数据结构
*/
export interface SolutionListView {
/** 唯一标识符 **/
id: number;
/** 标题 **/
title: string;
/** 摘要 **/
summary: string;
/** 解决方案类型 **/
solution_type: string;
/** 解决方案封面(图片id) **/
cover: string;
}

View File

@ -0,0 +1,20 @@
/**
* 解决方案模型
* 用于解决方案页(/solutions/[slug])渲染的数据结构
*/
export interface SolutionView {
/** 唯一标识符 **/
id: number;
/** 标题 **/
title: string;
/** 摘要 **/
summary: string;
/** 内容 **/
content: string;
/** 创建时间 **/
createAt: string;
}

View File

@ -25,7 +25,7 @@
<div class="solution-meta"> <div class="solution-meta">
<span class="solution-date"> <span class="solution-date">
CreatedAt: CreatedAt:
{{ new Date(solution.createdAt).toLocaleDateString() }} {{ new Date(solution.createAt).toLocaleDateString() }}
</span> </span>
</div> </div>
</div> </div>
@ -57,23 +57,17 @@
<script setup lang="ts"> <script setup lang="ts">
const route = useRoute(); const route = useRoute();
const { findOne } = useStrapi();
const { getStrapiLocale } = useLocalizations();
const strapiLocale = getStrapiLocale();
// 获取路由参数(documentId) // 获取路由参数(documentId)
const documentId = computed(() => route.params.slug as string); const id = computed(() => route.params.slug as string);
const { data, pending, error } = useAsyncData( const { data, pending, error } = await useSolution(id.value);
() => `solution-${documentId.value}`,
() =>
findOne<Solution>('solutions', documentId.value, {
populate: '*',
locale: strapiLocale,
})
);
const solution = computed(() => data.value?.data ?? null); console.log('RawData: ', data.value);
const process = toSolutionView(data.value);
console.log('Processed Solution: ', process);
const solution = computed(() => toSolutionView(data.value));
watch(error, (value) => { watch(error, (value) => {
if (value) { if (value) {

View File

@ -21,11 +21,11 @@
<div class="solution-list"> <div class="solution-list">
<solution-card <solution-card
v-for="solution in solutions" v-for="solution in solutions"
:key="solution.documentId" :key="solution.id"
:title="solution.title" :title="solution.title"
:summary="solution.summary || ''" :summary="solution.summary || ''"
:cover-url="useStrapiMedia(solution?.cover?.url || '')" :cover-url="getImageUrl(solution.cover || '')"
:document-id="solution.documentId" :document-id="solution.id.toString()"
/> />
</div> </div>
</el-tab-pane> </el-tab-pane>
@ -38,9 +38,9 @@
<div class="solution-list"> <div class="solution-list">
<solution-card <solution-card
v-for="solution in group" v-for="solution in group"
:key="solution.documentId" :key="solution.id"
:document-id="solution.documentId" :document-id="solution.id.toString()"
:cover-url="useStrapiMedia(solution?.cover?.url || '')" :cover-url="getImageUrl(solution.cover || '')"
:title="solution.title" :title="solution.title"
:summary="solution.summary || ''" :summary="solution.summary || ''"
/> />
@ -55,31 +55,22 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
const { find } = useStrapi(); const { getImageUrl } = useDirectusImage();
const { getStrapiLocale } = useLocalizations();
const strapiLocale = getStrapiLocale();
const { data, pending, error } = useAsyncData('solutions', () => const { data, pending, error } = await useSolutionList();
find<Solution>('solutions', {
populate: { const solutionsRaw = computed(() => data.value ?? []);
cover: { const solutions = computed(() =>
populate: '*', solutionsRaw.value.map((item) => toSolutionListView(item))
},
solution_type: {
populate: '*',
},
},
locale: strapiLocale,
})
); );
const activeName = ref<string>('all'); const activeName = ref<string>('all');
const solutions = computed(() => data.value?.data ?? []); console.log('Processed Data', solutions.value);
// 按类型分组 // 按类型分组
const groupedSolutions = computed(() => { const groupedSolutions = computed(() => {
const gourps: Record<string, Solution[]> = {}; const gourps: Record<string, SolutionListView[]> = {};
for (const sol of solutions.value) { for (const sol of solutions.value) {
let typeKey = ''; let typeKey = '';
if (typeof sol.solution_type === 'string') { if (typeof sol.solution_type === 'string') {
@ -89,7 +80,7 @@
typeof sol.solution_type === 'object' && typeof sol.solution_type === 'object' &&
'type' in sol.solution_type 'type' in sol.solution_type
) { ) {
typeKey = sol.solution_type.type || ''; typeKey = sol.solution_type || '';
} }
if (!gourps[typeKey]) gourps[typeKey] = []; if (!gourps[typeKey]) gourps[typeKey] = [];
gourps[typeKey]?.push(sol); gourps[typeKey]?.push(sol);