diff --git a/.graphqlrc.yaml b/.graphqlrc.yaml new file mode 100644 index 0000000..47690e9 --- /dev/null +++ b/.graphqlrc.yaml @@ -0,0 +1,5 @@ +schema: + - 'http://192.168.86.5:8055/graphql': + headers: + Authorization: 'Bearer ixSWeViHIqwj6_r7NM-uZVR3NNOyBa_W' +documents: 'app/graphql/**/*.{graphql,js,ts,jsx,tsx}' diff --git a/app/composables/directus/useCompanyProfile.ts b/app/composables/directus/useCompanyProfile.ts index 0b26039..af3954b 100644 --- a/app/composables/directus/useCompanyProfile.ts +++ b/app/composables/directus/useCompanyProfile.ts @@ -1,4 +1,4 @@ -import { readSingleton } from '@directus/sdk'; +import GetCompanyProfile from '@/graphql/companyProfile.graphql?raw'; export const useCompanyProfile = () => { const { $directus } = useNuxtApp(); @@ -6,24 +6,11 @@ export const useCompanyProfile = () => { const locale = getDirectusLocale(); return useAsyncData(`company-profile-${locale}`, async () => { - return await $directus.request( - readSingleton('company_profile', { - fields: [ - 'id', - { - translations: ['id', 'content'], - }, - ], - deep: { - translations: { - _filter: { - languages_code: { - _eq: locale, - }, - }, - }, - }, - }) + return await $directus.query<{ company_profile: CompanyProfile }>( + GetCompanyProfile, + { + locale: locale, + } ); }); }; diff --git a/app/composables/directus/useContactInfo.ts b/app/composables/directus/useContactInfo.ts index 8f0e7a4..3bfbc81 100644 --- a/app/composables/directus/useContactInfo.ts +++ b/app/composables/directus/useContactInfo.ts @@ -1,4 +1,4 @@ -import { readSingleton } from '@directus/sdk'; +import GetContactInfo from '@/graphql/contactInfo.graphql?raw'; export const useContactInfo = () => { const { $directus } = useNuxtApp(); @@ -6,24 +6,11 @@ export const useContactInfo = () => { const locale = getDirectusLocale(); return useAsyncData(`contact-info-${locale}`, async () => { - return await $directus.request( - readSingleton('contact_info', { - fields: [ - 'id', - { - translations: ['id', 'content'], - }, - ], - deep: { - translations: { - _filter: { - languages_code: { - _eq: locale, - }, - }, - }, - }, - }) + return await $directus.query<{ contact_info: ContactInfo }>( + GetContactInfo, + { + locale: locale, + } ); }); }; diff --git a/app/composables/directus/useDocumentList.ts b/app/composables/directus/useDocumentList.ts index fc21823..11b0978 100644 --- a/app/composables/directus/useDocumentList.ts +++ b/app/composables/directus/useDocumentList.ts @@ -1,4 +1,4 @@ -import { readItems } from '@directus/sdk'; +import GetDocumentList from '@/graphql/documentList.graphql?raw'; export const useDocumentList = () => { const { $directus } = useNuxtApp(); @@ -6,62 +6,11 @@ export const useDocumentList = () => { const locale = getDirectusLocale(); return useAsyncData(`document-list-${locale}`, async () => { - return await $directus.request( - readItems('product_documents', { - fields: [ - 'id', - { - file: ['id', 'filesize', 'filename_download'], - }, - { - 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 }, - }, - }, - }, - }, - }, - }, - }) + return await $directus.query<{ product_documents: ProductDocument[] }>( + GetDocumentList, + { + locale: locale, + } ); }); }; diff --git a/app/composables/directus/useHomepage.ts b/app/composables/directus/useHomepage.ts index 01da60c..af50bfe 100644 --- a/app/composables/directus/useHomepage.ts +++ b/app/composables/directus/useHomepage.ts @@ -1,4 +1,4 @@ -import { readSingleton } from '@directus/sdk'; +import GetHomepage from '@/graphql/homepage.graphql?raw'; export const useHomepage = () => { const { $directus } = useNuxtApp(); @@ -6,49 +6,8 @@ export const useHomepage = () => { const locale = getDirectusLocale(); return useAsyncData(`homepage-${locale}`, async () => { - return await $directus.request( - readSingleton('homepage', { - 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 }, - }, - }, - }, - }, - }) - ); + return await $directus.query<{ homepage: Homepage }>(GetHomepage, { + locale: locale, + }); }); }; diff --git a/app/composables/directus/useProduct.ts b/app/composables/directus/useProduct.ts index 67c18ed..e264cb0 100644 --- a/app/composables/directus/useProduct.ts +++ b/app/composables/directus/useProduct.ts @@ -1,4 +1,4 @@ -import { readItem } from '@directus/sdk'; +import GetProduct from '@/graphql/product.graphql?raw'; export const useProduct = (id: string) => { const { $directus } = useNuxtApp(); @@ -7,104 +7,9 @@ export const useProduct = (id: string) => { const locale = getDirectusLocale(); return useAsyncData(`product-${id}-${locale}`, async () => { - return await $directus.request( - readItem('products', id, { - fields: [ - 'id', - { translations: ['id', 'name', 'summary', 'description'] }, - { - images: [ - 'id', - { - product_images_id: [ - 'id', - 'image', - { translations: ['id', 'caption'] }, - ], - }, - ], - }, - { - specs: [ - 'id', - { - translations: ['*'], - }, - { - specs: [ - 'id', - { - translations: ['id', 'key', 'value'], - }, - ], - }, - ], - }, - { - faqs: [ - 'id', - { - questions_id: [ - 'id', - { - translations: ['id', 'title', 'content'], - }, - ], - }, - ], - }, - { - documents: [ - 'id', - { - product_documents_id: [ - 'id', - { - file: ['id', 'filesize', 'filename_download'], - }, - { - translations: ['id', 'title'], - }, - ], - }, - ], - }, - ], - deep: { - translations: { - _filter: { - languages_code: { _eq: locale }, - }, - }, - images: { - product_images_id: { - translations: { - _filter: { - languages_code: { _eq: locale }, - }, - }, - }, - }, - faqs: { - questions_id: { - translations: { - _filter: { - languages_code: { _eq: locale }, - }, - }, - }, - }, - documents: { - documents_id: { - translations: { - _filter: { - languages_code: { _eq: locale }, - }, - }, - }, - }, - }, - }) - ); + return await $directus.query<{ products_by_id: Product }>(GetProduct, { + id: id, + locale: locale, + }); }); }; diff --git a/app/composables/directus/useProductList.ts b/app/composables/directus/useProductList.ts index 989c88d..58bac21 100644 --- a/app/composables/directus/useProductList.ts +++ b/app/composables/directus/useProductList.ts @@ -1,4 +1,4 @@ -import { readItems } from '@directus/sdk'; +import GetProductList from '@/graphql/productList.graphql?raw'; export const useProductList = () => { const { $directus } = useNuxtApp(); @@ -7,37 +7,8 @@ export const useProductList = () => { const locale = getDirectusLocale(); return useAsyncData(`product-list-${locale}`, async () => { - return await $directus.request( - readItems('products', { - fields: [ - 'id', - { translations: ['id', 'name', 'summary'] }, - 'cover', - { - product_type: [ - 'id', - { - translations: ['id', 'name'], - }, - 'sort', - ], - }, - ], - deep: { - translations: { - _filter: { - languages_code: { _eq: locale }, - }, - }, - product_type: { - translations: { - _filter: { - languages_code: { _eq: locale }, - }, - }, - }, - }, - }) - ); + return await $directus.query<{ products: Product[] }>(GetProductList, { + locale: locale, + }); }); }; diff --git a/app/composables/directus/useQuestionList.ts b/app/composables/directus/useQuestionList.ts index 76e1350..d9c35b2 100644 --- a/app/composables/directus/useQuestionList.ts +++ b/app/composables/directus/useQuestionList.ts @@ -1,4 +1,4 @@ -import { readItems } from '@directus/sdk'; +import GetQuestionList from '@/graphql/questionList.graphql?raw'; export const useQuestionList = () => { const { $directus } = useNuxtApp(); @@ -7,57 +7,8 @@ export const useQuestionList = () => { const locale = getDirectusLocale(); return useAsyncData(`question-list-${locale}`, async () => { - return await $directus.request( - readItems('questions', { - 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 }, - }, - }, - }, - }, - }, - }) - ); + return await $directus.query<{ questions: Question[] }>(GetQuestionList, { + locale: locale, + }); }); }; diff --git a/app/composables/directus/useSolution.ts b/app/composables/directus/useSolution.ts index 78b77de..1ab8f55 100644 --- a/app/composables/directus/useSolution.ts +++ b/app/composables/directus/useSolution.ts @@ -1,4 +1,4 @@ -import { readItem } from '@directus/sdk'; +import GetSolution from '@/graphql/solution.graphql?raw'; export const useSolution = (id: string) => { const { $directus } = useNuxtApp(); @@ -6,23 +6,9 @@ export const useSolution = (id: string) => { const locale = getDirectusLocale(); return useAsyncData(`solution-${id}-${locale}`, async () => { - return await $directus.request( - readItem('solutions', id, { - fields: [ - 'id', - { - translations: ['*'], - }, - 'create_at', - ], - deep: { - translations: { - _filter: { - languages_code: { _eq: locale }, - }, - }, - }, - }) - ); + return await $directus.query<{ solutions_by_id: Solution }>(GetSolution, { + id: id, + locale: locale, + }); }); }; diff --git a/app/composables/directus/useSolutionList.ts b/app/composables/directus/useSolutionList.ts index 8fe659f..fcbd25d 100644 --- a/app/composables/directus/useSolutionList.ts +++ b/app/composables/directus/useSolutionList.ts @@ -1,4 +1,4 @@ -import { readItems } from '@directus/sdk'; +import GetSolutionList from '@/graphql/solutionList.graphql?raw'; export const useSolutionList = () => { const { $directus } = useNuxtApp(); @@ -6,33 +6,8 @@ export const useSolutionList = () => { const locale = getDirectusLocale(); return useAsyncData(`solution-list-${locale}`, async () => { - return await $directus.request( - readItems('solutions', { - 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 }, - }, - }, - }, - }) - ); + return await $directus.query<{ solutions: Solution[] }>(GetSolutionList, { + locale: locale, + }); }); }; diff --git a/app/graphql/companyProfile.graphql b/app/graphql/companyProfile.graphql new file mode 100644 index 0000000..97f362e --- /dev/null +++ b/app/graphql/companyProfile.graphql @@ -0,0 +1,9 @@ +query GetCompanyProfile($locale: String!) { + company_profile { + id + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + content + } + } +} diff --git a/app/graphql/contactInfo.graphql b/app/graphql/contactInfo.graphql new file mode 100644 index 0000000..5dfe360 --- /dev/null +++ b/app/graphql/contactInfo.graphql @@ -0,0 +1,9 @@ +query GetContactInfo($locale: String!) { + contact_info { + id + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + content + } + } +} diff --git a/app/graphql/documentList.graphql b/app/graphql/documentList.graphql new file mode 100644 index 0000000..e133dcd --- /dev/null +++ b/app/graphql/documentList.graphql @@ -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 + } + } + } + } + } +} diff --git a/app/graphql/homepage.graphql b/app/graphql/homepage.graphql new file mode 100644 index 0000000..b31229c --- /dev/null +++ b/app/graphql/homepage.graphql @@ -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 + } + } + } +} diff --git a/app/graphql/product.graphql b/app/graphql/product.graphql new file mode 100644 index 0000000..d4493c2 --- /dev/null +++ b/app/graphql/product.graphql @@ -0,0 +1,65 @@ +query GetProduct($id: ID!, $locale: String!) { + products_by_id(id: $id) { + id + status + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + name + summary + description + } + images { + id + product_images_id { + id + image { + id + } + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + caption + } + } + } + specs { + id + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + name + } + specs { + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + key + value + } + } + } + faqs { + id + questions_id { + id + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + title + content + } + } + } + documents { + id + product_documents_id { + id + file { + id + filesize + filename_download + } + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + title + } + } + } + } +} diff --git a/app/graphql/productList.graphql b/app/graphql/productList.graphql new file mode 100644 index 0000000..788d12a --- /dev/null +++ b/app/graphql/productList.graphql @@ -0,0 +1,22 @@ +query GetProductList($locale: String!) { + products(filter: { status: { _eq: "in-production" } }) { + id + status + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + name + summary + } + cover { + id + } + product_type { + id + translations(filter: { languages_code: { code: { _eq: $locale } } }) { + id + name + } + sort + } + } +} diff --git a/app/graphql/questionList.graphql b/app/graphql/questionList.graphql new file mode 100644 index 0000000..39085c6 --- /dev/null +++ b/app/graphql/questionList.graphql @@ -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 + } + } + } + } +} diff --git a/app/graphql/solution.graphql b/app/graphql/solution.graphql new file mode 100644 index 0000000..ae2f590 --- /dev/null +++ b/app/graphql/solution.graphql @@ -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 + } +} diff --git a/app/graphql/solutionList.graphql b/app/graphql/solutionList.graphql new file mode 100644 index 0000000..8422275 --- /dev/null +++ b/app/graphql/solutionList.graphql @@ -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 + } + } +} diff --git a/app/models/mappers/homepageMapper.test.ts b/app/models/mappers/homepageMapper.test.ts index de881c7..055cf09 100644 --- a/app/models/mappers/homepageMapper.test.ts +++ b/app/models/mappers/homepageMapper.test.ts @@ -8,8 +8,18 @@ describe('toHomepageView', () => { const rawData: Homepage = { id: 1, 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: [ { diff --git a/app/models/mappers/homepageMapper.ts b/app/models/mappers/homepageMapper.ts index cd95c0e..078890b 100644 --- a/app/models/mappers/homepageMapper.ts +++ b/app/models/mappers/homepageMapper.ts @@ -11,7 +11,8 @@ export function toHomepageView(raw: Homepage): HomepageView { const carousel = (raw.carousel ?? []) .filter(isObject) .map((item) => item.directus_files_id) - .filter((item) => typeof item === 'string'); + .filter(isObject) + .map((file) => file.id); const products = (raw.recommend_products ?? []) .filter(isObject) diff --git a/app/models/mappers/productMapper.test.ts b/app/models/mappers/productMapper.test.ts index 1e996f0..ad9a837 100644 --- a/app/models/mappers/productMapper.test.ts +++ b/app/models/mappers/productMapper.test.ts @@ -10,7 +10,9 @@ describe('toProductListView', () => { translations: [ { id: 10, name: 'Product Name', summary: 'Product Summary' }, ], - cover: 'cover-file-uuid-1234', + cover: { + id: 'cover-file-uuid-1234', + }, product_type: { id: 1, translations: [{ id: 20, name: 'Type Name' }], @@ -35,7 +37,9 @@ describe('toProductListView', () => { const rawData: Product = { id: 1, translations: [], - cover: 'cover-file-uuid-1234', + cover: { + id: 'cover-file-uuid-1234', + }, product_type: { id: 20, translations: [], diff --git a/app/models/mappers/productMapper.ts b/app/models/mappers/productMapper.ts index 724faff..9c61a5e 100644 --- a/app/models/mappers/productMapper.ts +++ b/app/models/mappers/productMapper.ts @@ -33,12 +33,14 @@ export function toProductListView(raw: Product): ProductListView { ? toProductTypeView(raw.product_type) : undefined; + const cover = isObject(raw.cover) ? raw.cover.id : ''; + return { id: raw.id, product_type: type, name: trans.name, summary: trans.summary, - cover: raw.cover.toString(), + cover: cover, }; } @@ -110,9 +112,10 @@ export function toProductView(raw: Product): ProductView { .map((item) => item.product_images_id) .filter(isObject) .map((item) => { + const image = isObject(item.image) ? item.image.id : ''; return { id: item.id, - image: item.image.toString(), + image: image, caption: item.translations?.[0]?.caption || '', }; }); diff --git a/app/pages/about/index.vue b/app/pages/about/index.vue index 64dd05f..09a775c 100644 --- a/app/pages/about/index.vue +++ b/app/pages/about/index.vue @@ -34,7 +34,9 @@ ]; const { data, pending, error } = await useCompanyProfile(); - const content = computed(() => toCompanyProfileView(data.value)); + const content = computed(() => + toCompanyProfileView(data.value.company_profile) + ); watch(error, (value) => { if (value) { diff --git a/app/pages/index.vue b/app/pages/index.vue index f2eb413..d5d6720 100644 --- a/app/pages/index.vue +++ b/app/pages/index.vue @@ -16,7 +16,7 @@ const { data, pending, error } = await useHomepage(); const homepageData = computed(() => { - return toHomepageView(data.value); + return toHomepageView(data.value.homepage); }); const pageTilte = $t('page-title.homepage'); diff --git a/app/pages/products/[...slug].vue b/app/pages/products/[slug].vue similarity index 86% rename from app/pages/products/[...slug].vue rename to app/pages/products/[slug].vue index cca08a3..6eee50a 100644 --- a/app/pages/products/[...slug].vue +++ b/app/pages/products/[slug].vue @@ -35,11 +35,12 @@ const localePath = useLocalePath(); // 获取路由参数 - const id = computed(() => route.params.slug as string); + const id = route.params.slug as string; - const { data, pending, error } = await useProduct(id.value); + const { data, pending, error } = await useProduct(id); + + const rawProduct = computed(() => data.value.products_by_id ?? null); - const rawProduct = computed(() => data.value ?? null); const product = computed(() => { if (rawProduct.value === null) { return null; @@ -60,10 +61,10 @@ }); // SEO - usePageSeo({ - title: product.value.name || $t('page-title.products'), - description: product.value.summary || '', - }); + // usePageSeo({ + // title: product.value.name || $t('page-title.products'), + // description: product.value.summary || '', + // });