Compare commits

..

10 Commits

Author SHA1 Message Date
8213eec217 refactor: 关于我们页的API重构
Some checks failed
deploy to server / build-and-deploy (push) Has been cancelled
2025-11-11 17:01:18 +08:00
ac9e7b4436 refactor: 联系信息页的API重构 2025-11-11 16:58:40 +08:00
5ad6133252 refactor: 文档资料页的API重构 2025-11-11 16:55:41 +08:00
a07d77dde7 refactor: 问题列表的API重构 2025-11-11 16:49:58 +08:00
3e7b195002 refactor: 解决方案页的API重构 2025-11-11 16:43:08 +08:00
706b754905 refactor: 解决方案列表的API重构 2025-11-11 16:35:20 +08:00
0d77e97ad5 test: 完善测试 2025-11-11 16:15:05 +08:00
ac658e01ae refactor: 调整首页数据获取API
- 将首页的数据获取由REST重构为GraphQL
2025-11-11 16:14:58 +08:00
a93f508e85 refactor: 调整GraphQL导入形式
- 以纯文本形式导入
2025-11-11 16:08:24 +08:00
1290189d84 style: 格式化graphql文件 2025-11-11 16:01:20 +08:00
26 changed files with 276 additions and 334 deletions

View File

@ -1,4 +1,4 @@
import { readSingleton } from '@directus/sdk'; import GetCompanyProfile from '@/graphql/companyProfile.graphql?raw';
export const useCompanyProfile = () => { export const useCompanyProfile = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -6,24 +6,11 @@ export const useCompanyProfile = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`company-profile-${locale}`, async () => { return useAsyncData(`company-profile-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ company_profile: CompanyProfile }>(
readSingleton('company_profile', { GetCompanyProfile,
fields: [
'id',
{ {
translations: ['id', 'content'], locale: locale,
}, }
],
deep: {
translations: {
_filter: {
languages_code: {
_eq: locale,
},
},
},
},
})
); );
}); });
}; };

View File

@ -1,4 +1,4 @@
import { readSingleton } from '@directus/sdk'; import GetContactInfo from '@/graphql/contactInfo.graphql?raw';
export const useContactInfo = () => { export const useContactInfo = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -6,24 +6,11 @@ export const useContactInfo = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`contact-info-${locale}`, async () => { return useAsyncData(`contact-info-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ contact_info: ContactInfo }>(
readSingleton('contact_info', { GetContactInfo,
fields: [
'id',
{ {
translations: ['id', 'content'], locale: locale,
}, }
],
deep: {
translations: {
_filter: {
languages_code: {
_eq: locale,
},
},
},
},
})
); );
}); });
}; };

View File

@ -1,4 +1,4 @@
import { readItems } from '@directus/sdk'; import GetDocumentList from '@/graphql/documentList.graphql?raw';
export const useDocumentList = () => { export const useDocumentList = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -6,62 +6,11 @@ export const useDocumentList = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`document-list-${locale}`, async () => { return useAsyncData(`document-list-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ product_documents: ProductDocument[] }>(
readItems('product_documents', { GetDocumentList,
fields: [
'id',
{ {
file: ['id', 'filesize', 'filename_download'], locale: locale,
}, }
{
translations: ['id', 'title'],
},
{
products: [
'id',
{
products_id: [
'id',
{
translations: ['id', 'name'],
},
{
product_type: [
'id',
{
translations: ['id', 'name'],
},
],
},
],
},
],
},
],
deep: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
products: {
products_id: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
product_type: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
},
},
},
})
); );
}); });
}; };

View File

@ -1,4 +1,4 @@
import { readSingleton } from '@directus/sdk'; import GetHomepage from '@/graphql/homepage.graphql?raw';
export const useHomepage = () => { export const useHomepage = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -6,49 +6,8 @@ export const useHomepage = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`homepage-${locale}`, async () => { return useAsyncData(`homepage-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ homepage: Homepage }>(GetHomepage, {
readSingleton('homepage', { locale: locale,
fields: [ });
'id',
{
carousel: ['id', 'directus_files_id'],
},
{
recommend_products: [
'id',
{
translations: ['id', 'name', 'summary'],
},
'cover',
],
},
{
recommend_solutions: [
'id',
{
translations: ['id', 'title', 'summary'],
},
'cover',
],
},
],
deep: {
recommend_products: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
recommend_solutions: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
},
})
);
}); });
}; };

View File

@ -1,5 +1,4 @@
import GetProduct from '@/graphql/product.graphql'; import GetProduct from '@/graphql/product.graphql?raw';
import { print } from 'graphql';
export const useProduct = (id: string) => { export const useProduct = (id: string) => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -8,13 +7,10 @@ export const useProduct = (id: string) => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`product-${id}-${locale}`, async () => { return useAsyncData(`product-${id}-${locale}`, async () => {
return await $directus.query<{ products_by_id: Product }>( return await $directus.query<{ products_by_id: Product }>(GetProduct, {
print(GetProduct),
{
id: id, id: id,
locale: locale, locale: locale,
} });
);
// return await $directus.request( // return await $directus.request(
// readItem('products', id, { // readItem('products', id, {
// fields: [ // fields: [

View File

@ -1,5 +1,4 @@
import GetProductList from '@/graphql/productList.graphql'; import GetProductList from '@/graphql/productList.graphql?raw';
import { print } from 'graphql';
export const useProductList = () => { export const useProductList = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -8,11 +7,8 @@ export const useProductList = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`product-list-${locale}`, async () => { return useAsyncData(`product-list-${locale}`, async () => {
return await $directus.query<{ products: Product[] }>( return await $directus.query<{ products: Product[] }>(GetProductList, {
print(GetProductList),
{
locale: locale, locale: locale,
} });
);
}); });
}; };

View File

@ -1,4 +1,4 @@
import { readItems } from '@directus/sdk'; import GetQuestionList from '@/graphql/questionList.graphql?raw';
export const useQuestionList = () => { export const useQuestionList = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -7,57 +7,8 @@ export const useQuestionList = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`question-list-${locale}`, async () => { return useAsyncData(`question-list-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ questions: Question[] }>(GetQuestionList, {
readItems('questions', { locale: locale,
fields: [ });
'id',
{
translations: ['*'],
},
{
products: [
'id',
{
products_id: [
'id',
{
product_type: [
'id',
{
translations: ['id', 'name'],
},
],
},
{ translations: ['id', 'name'] },
],
},
],
},
],
deep: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
products: {
products_id: {
product_type: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
},
},
})
);
}); });
}; };

View File

@ -1,4 +1,4 @@
import { readItem } from '@directus/sdk'; import GetSolution from '@/graphql/solution.graphql?raw';
export const useSolution = (id: string) => { export const useSolution = (id: string) => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -6,23 +6,9 @@ export const useSolution = (id: string) => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`solution-${id}-${locale}`, async () => { return useAsyncData(`solution-${id}-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ solutions_by_id: Solution }>(GetSolution, {
readItem('solutions', id, { id: id,
fields: [ locale: locale,
'id', });
{
translations: ['*'],
},
'create_at',
],
deep: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
})
);
}); });
}; };

View File

@ -1,4 +1,4 @@
import { readItems } from '@directus/sdk'; import GetSolutionList from '@/graphql/solutionList.graphql?raw';
export const useSolutionList = () => { export const useSolutionList = () => {
const { $directus } = useNuxtApp(); const { $directus } = useNuxtApp();
@ -6,33 +6,8 @@ export const useSolutionList = () => {
const locale = getDirectusLocale(); const locale = getDirectusLocale();
return useAsyncData(`solution-list-${locale}`, async () => { return useAsyncData(`solution-list-${locale}`, async () => {
return await $directus.request( return await $directus.query<{ solutions: Solution[] }>(GetSolutionList, {
readItems('solutions', { locale: locale,
fields: [ });
'id',
'cover',
{
type: ['id', { translations: ['id', 'name'] }, 'sort'],
},
{
translations: ['id', 'title', 'summary'],
},
],
deep: {
type: {
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
translations: {
_filter: {
languages_code: { _eq: locale },
},
},
},
})
);
}); });
}; };

View File

@ -0,0 +1,9 @@
query GetCompanyProfile($locale: String!) {
company_profile {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
content
}
}
}

View File

@ -0,0 +1,9 @@
query GetContactInfo($locale: String!) {
contact_info {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
content
}
}
}

View File

@ -0,0 +1,31 @@
query GetDocumentList($locale: String!) {
product_documents {
id
file {
id
filesize
filename_download
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
}
products {
id
products_id {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
product_type {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
}
}
}
}
}

View File

@ -0,0 +1,33 @@
query GetHomepage($locale: String!) {
homepage {
id
carousel {
id
directus_files_id {
id
}
}
recommend_products {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
summary
}
cover {
id
}
}
recommend_solutions {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
summary
}
cover {
id
}
}
}
}

View File

@ -63,4 +63,3 @@ query GetProduct($id: ID!, $locale: String!) {
} }
} }
} }

View File

@ -0,0 +1,27 @@
query GetQuestionList($locale: String!) {
questions {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
content
}
products {
id
products_id {
id
product_type {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
}
}
}
}

View File

@ -0,0 +1,12 @@
query GetSolution($id: ID!, $locale: String!) {
solutions_by_id(id: $id) {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
summary
content
}
create_at
}
}

View File

@ -0,0 +1,21 @@
query GetSolutionList($locale: String!) {
solutions {
id
cover {
id
}
type {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
name
}
sort
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
summary
}
}
}

View File

@ -8,8 +8,18 @@ describe('toHomepageView', () => {
const rawData: Homepage = { const rawData: Homepage = {
id: 1, id: 1,
carousel: [ carousel: [
{ id: 1, directus_files_id: 'file-uuid-1' }, {
{ id: 2, directus_files_id: 'file-uuid-2' }, id: 1,
directus_files_id: {
id: 'file-uuid-1',
},
},
{
id: 2,
directus_files_id: {
id: 'file-uuid-2',
},
},
], ],
recommend_products: [ recommend_products: [
{ {

View File

@ -11,7 +11,8 @@ export function toHomepageView(raw: Homepage): HomepageView {
const carousel = (raw.carousel ?? []) const carousel = (raw.carousel ?? [])
.filter(isObject<HomepageFile>) .filter(isObject<HomepageFile>)
.map((item) => item.directus_files_id) .map((item) => item.directus_files_id)
.filter((item) => typeof item === 'string'); .filter(isObject<DirectusFile>)
.map((file) => file.id);
const products = (raw.recommend_products ?? []) const products = (raw.recommend_products ?? [])
.filter(isObject<Product>) .filter(isObject<Product>)

View File

@ -34,7 +34,9 @@
]; ];
const { data, pending, error } = await useCompanyProfile(); const { data, pending, error } = await useCompanyProfile();
const content = computed(() => toCompanyProfileView(data.value)); const content = computed(() =>
toCompanyProfileView(data.value.company_profile)
);
watch(error, (value) => { watch(error, (value) => {
if (value) { if (value) {

View File

@ -16,7 +16,7 @@
const { data, pending, error } = await useHomepage(); const { data, pending, error } = await useHomepage();
const homepageData = computed(() => { const homepageData = computed(() => {
return toHomepageView(data.value); return toHomepageView(data.value.homepage);
}); });
const pageTilte = $t('page-title.homepage'); const pageTilte = $t('page-title.homepage');

View File

@ -26,16 +26,16 @@
const route = useRoute(); const route = useRoute();
const localePath = useLocalePath(); const localePath = useLocalePath();
// (documentId) //
const id = computed(() => route.params.slug as string); const id = route.params.slug as string;
const { data, pending, error } = await useSolution(id.value); const { data, pending, error } = await useSolution(id);
const solution = computed(() => { const solution = computed(() => {
if (!data.value) { if (!data.value) {
return null; return null;
} }
return toSolutionView(data.value); return toSolutionView(data.value.solutions_by_id);
}); });
const breadcrumbItems = computed(() => [ const breadcrumbItems = computed(() => [

View File

@ -54,7 +54,7 @@
const { data, pending, error } = await useSolutionList(); const { data, pending, error } = await useSolutionList();
const solutionsRaw = computed(() => data.value ?? []); const solutionsRaw = computed(() => data.value.solutions ?? []);
const solutions = computed(() => const solutions = computed(() =>
solutionsRaw.value.map((item) => toSolutionListView(item)) solutionsRaw.value.map((item) => toSolutionListView(item))
); );

View File

@ -24,7 +24,7 @@
]; ];
const { data, pending, error } = await useContactInfo(); const { data, pending, error } = await useContactInfo();
const content = computed(() => toContactInfoView(data.value)); const content = computed(() => toContactInfoView(data.value.contact_info));
watch(error, (value) => { watch(error, (value) => {
if (value) { if (value) {

View File

@ -38,7 +38,9 @@
const { data, pending, error } = await useDocumentList(); const { data, pending, error } = await useDocumentList();
const documents = computed( const documents = computed(
() => data?.value.map((item) => toDocumentListView(item)) ?? [] () =>
data?.value.product_documents.map((item) => toDocumentListView(item)) ??
[]
); );
const productTypeOptions = computed(() => { const productTypeOptions = computed(() => {

View File

@ -41,7 +41,7 @@
const { data, pending, error } = await useQuestionList(); const { data, pending, error } = await useQuestionList();
const questions = computed( const questions = computed(
() => data.value.map((item) => toQuestionListView(item)) ?? null () => data.value.questions.map((item) => toQuestionListView(item)) ?? null
); );
const productTypeOptions = computed(() => { const productTypeOptions = computed(() => {