feat: 将解决方案页迁移至directus
- 将/solutions与/solutions/[slug]页现在由Directus作为CMS - 添加solution页的composable API
This commit is contained in:
54
app/models/mappers/solutionMapper.ts
Normal file
54
app/models/mappers/solutionMapper.ts
Normal 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,
|
||||
};
|
||||
}
|
||||
20
app/models/views/SolutionListView.ts
Normal file
20
app/models/views/SolutionListView.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* 解决方案列表模型
|
||||
* 用于解决方案列表(/solutions)渲染的数据结构
|
||||
*/
|
||||
export interface SolutionListView {
|
||||
/** 唯一标识符 **/
|
||||
id: number;
|
||||
|
||||
/** 标题 **/
|
||||
title: string;
|
||||
|
||||
/** 摘要 **/
|
||||
summary: string;
|
||||
|
||||
/** 解决方案类型 **/
|
||||
solution_type: string;
|
||||
|
||||
/** 解决方案封面(图片id) **/
|
||||
cover: string;
|
||||
}
|
||||
20
app/models/views/SolutionView.ts
Normal file
20
app/models/views/SolutionView.ts
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* 解决方案模型
|
||||
* 用于解决方案页(/solutions/[slug])渲染的数据结构
|
||||
*/
|
||||
export interface SolutionView {
|
||||
/** 唯一标识符 **/
|
||||
id: number;
|
||||
|
||||
/** 标题 **/
|
||||
title: string;
|
||||
|
||||
/** 摘要 **/
|
||||
summary: string;
|
||||
|
||||
/** 内容 **/
|
||||
content: string;
|
||||
|
||||
/** 创建时间 **/
|
||||
createAt: string;
|
||||
}
|
||||
@ -25,7 +25,7 @@
|
||||
<div class="solution-meta">
|
||||
<span class="solution-date">
|
||||
CreatedAt:
|
||||
{{ new Date(solution.createdAt).toLocaleDateString() }}
|
||||
{{ new Date(solution.createAt).toLocaleDateString() }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
@ -57,23 +57,17 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
const route = useRoute();
|
||||
const { findOne } = useStrapi();
|
||||
const { getStrapiLocale } = useLocalizations();
|
||||
const strapiLocale = getStrapiLocale();
|
||||
|
||||
// 获取路由参数(documentId)
|
||||
const documentId = computed(() => route.params.slug as string);
|
||||
const id = computed(() => route.params.slug as string);
|
||||
|
||||
const { data, pending, error } = useAsyncData(
|
||||
() => `solution-${documentId.value}`,
|
||||
() =>
|
||||
findOne<Solution>('solutions', documentId.value, {
|
||||
populate: '*',
|
||||
locale: strapiLocale,
|
||||
})
|
||||
);
|
||||
const { data, pending, error } = await useSolution(id.value);
|
||||
|
||||
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) => {
|
||||
if (value) {
|
||||
|
||||
@ -21,11 +21,11 @@
|
||||
<div class="solution-list">
|
||||
<solution-card
|
||||
v-for="solution in solutions"
|
||||
:key="solution.documentId"
|
||||
:key="solution.id"
|
||||
:title="solution.title"
|
||||
:summary="solution.summary || ''"
|
||||
:cover-url="useStrapiMedia(solution?.cover?.url || '')"
|
||||
:document-id="solution.documentId"
|
||||
:cover-url="getImageUrl(solution.cover || '')"
|
||||
:document-id="solution.id.toString()"
|
||||
/>
|
||||
</div>
|
||||
</el-tab-pane>
|
||||
@ -38,9 +38,9 @@
|
||||
<div class="solution-list">
|
||||
<solution-card
|
||||
v-for="solution in group"
|
||||
:key="solution.documentId"
|
||||
:document-id="solution.documentId"
|
||||
:cover-url="useStrapiMedia(solution?.cover?.url || '')"
|
||||
:key="solution.id"
|
||||
:document-id="solution.id.toString()"
|
||||
:cover-url="getImageUrl(solution.cover || '')"
|
||||
:title="solution.title"
|
||||
:summary="solution.summary || ''"
|
||||
/>
|
||||
@ -55,31 +55,22 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
const { find } = useStrapi();
|
||||
const { getStrapiLocale } = useLocalizations();
|
||||
const strapiLocale = getStrapiLocale();
|
||||
const { getImageUrl } = useDirectusImage();
|
||||
|
||||
const { data, pending, error } = useAsyncData('solutions', () =>
|
||||
find<Solution>('solutions', {
|
||||
populate: {
|
||||
cover: {
|
||||
populate: '*',
|
||||
},
|
||||
solution_type: {
|
||||
populate: '*',
|
||||
},
|
||||
},
|
||||
locale: strapiLocale,
|
||||
})
|
||||
const { data, pending, error } = await useSolutionList();
|
||||
|
||||
const solutionsRaw = computed(() => data.value ?? []);
|
||||
const solutions = computed(() =>
|
||||
solutionsRaw.value.map((item) => toSolutionListView(item))
|
||||
);
|
||||
|
||||
const activeName = ref<string>('all');
|
||||
|
||||
const solutions = computed(() => data.value?.data ?? []);
|
||||
console.log('Processed Data', solutions.value);
|
||||
|
||||
// 按类型分组
|
||||
const groupedSolutions = computed(() => {
|
||||
const gourps: Record<string, Solution[]> = {};
|
||||
const gourps: Record<string, SolutionListView[]> = {};
|
||||
for (const sol of solutions.value) {
|
||||
let typeKey = '';
|
||||
if (typeof sol.solution_type === 'string') {
|
||||
@ -89,7 +80,7 @@
|
||||
typeof sol.solution_type === 'object' &&
|
||||
'type' in sol.solution_type
|
||||
) {
|
||||
typeKey = sol.solution_type.type || '';
|
||||
typeKey = sol.solution_type || '';
|
||||
}
|
||||
if (!gourps[typeKey]) gourps[typeKey] = [];
|
||||
gourps[typeKey]?.push(sol);
|
||||
|
||||
Reference in New Issue
Block a user