feat: 产品页的组合式API
All checks were successful
deploy to server / build-and-deploy (push) Successful in 5m11s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 5m11s
- 为product和product-list添加组合式API
This commit is contained in:
@ -1,2 +1,4 @@
|
||||
export * from './useDirectusImage';
|
||||
export * from './useDirectusFiles';
|
||||
export * from './useProductList';
|
||||
export * from './useProduct';
|
||||
|
||||
111
app/composables/directus/useProduct.ts
Normal file
111
app/composables/directus/useProduct.ts
Normal file
@ -0,0 +1,111 @@
|
||||
import { readItem } from '@directus/sdk';
|
||||
|
||||
export const useProduct = (id: string) => {
|
||||
const { $directus } = useNuxtApp();
|
||||
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
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 },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
};
|
||||
42
app/composables/directus/useProductList.ts
Normal file
42
app/composables/directus/useProductList.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { readItems } from '@directus/sdk';
|
||||
|
||||
export const useProductList = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
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'],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
deep: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
product_type: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: locale },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
});
|
||||
};
|
||||
@ -106,124 +106,14 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { readItem } from '@directus/sdk';
|
||||
const route = useRoute();
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const directusLocale = computed(() => getDirectusLocale());
|
||||
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getImageUrl } = useDirectusImage();
|
||||
|
||||
// 获取路由参数(slug 或 id)
|
||||
const documentId = computed(() => route.params.slug as string);
|
||||
// 获取路由参数
|
||||
const id = computed(() => route.params.slug as string);
|
||||
|
||||
const { data, pending, error } = await useAsyncData(
|
||||
() => `production-${documentId.value}`,
|
||||
() =>
|
||||
$directus.request(
|
||||
readItem('products', documentId.value, {
|
||||
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: directusLocale.value },
|
||||
},
|
||||
},
|
||||
images: {
|
||||
product_images_id: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: directusLocale.value },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
faqs: {
|
||||
questions_id: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: directusLocale.value },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
documents: {
|
||||
documents_id: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: directusLocale.value },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
),
|
||||
{
|
||||
watch: [documentId, directusLocale],
|
||||
}
|
||||
);
|
||||
const { data, pending, error } = await useProduct(id.value);
|
||||
|
||||
console.log('Raw Data: ', data.value);
|
||||
|
||||
|
||||
@ -45,52 +45,9 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { readItems } from '@directus/sdk';
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const directusLocale = computed(() => getDirectusLocale());
|
||||
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getImageUrl } = useDirectusImage();
|
||||
|
||||
const { data, pending, error } = useAsyncData(
|
||||
'products',
|
||||
() =>
|
||||
$directus.request(
|
||||
readItems('products', {
|
||||
fields: [
|
||||
'id',
|
||||
{ translations: ['id', 'name', 'summary'] },
|
||||
'cover',
|
||||
{
|
||||
product_type: [
|
||||
'id',
|
||||
{
|
||||
translations: ['id', 'name'],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
deep: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: directusLocale.value },
|
||||
},
|
||||
},
|
||||
product_type: {
|
||||
translations: {
|
||||
_filter: {
|
||||
languages_code: { _eq: directusLocale.value },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
),
|
||||
{
|
||||
watch: [directusLocale],
|
||||
lazy: true,
|
||||
}
|
||||
);
|
||||
const { data, pending, error } = useProductList();
|
||||
|
||||
const activeNames = ref<string[]>([]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user