refactor: 将数据获取从app端移至server端

- 调整数据获取位置以提升安全性
- 对于后端状态为Archived的数据,通过server控制不进行获取
This commit is contained in:
2025-11-12 17:54:43 +08:00
parent 58223734a2
commit a5f3895794
37 changed files with 302 additions and 85 deletions

View File

@ -1,16 +1,17 @@
import GetCompanyProfile from '@/graphql/companyProfile.graphql?raw';
export const useCompanyProfile = () => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`company-profile-${locale}`, async () => {
return await $directus.query<{ company_profile: CompanyProfile }>(
GetCompanyProfile,
{
locale: locale,
}
);
try {
const data = await $fetch(`/api/cms/companyProfile`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching company profile: ', error);
throw error;
}
});
};

View File

@ -1,16 +1,17 @@
import GetContactInfo from '@/graphql/contactInfo.graphql?raw';
export const useContactInfo = () => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`contact-info-${locale}`, async () => {
return await $directus.query<{ contact_info: ContactInfo }>(
GetContactInfo,
{
locale: locale,
}
);
try {
const data = await $fetch('/api/cms/contactInfo', {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching contact info: ', error);
throw error;
}
});
};

View File

@ -1,16 +1,17 @@
import GetDocumentList from '@/graphql/documentList.graphql?raw';
export const useDocumentList = () => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`document-list-${locale}`, async () => {
return await $directus.query<{ product_documents: ProductDocument[] }>(
GetDocumentList,
{
locale: locale,
}
);
try {
const data = $fetch(`/api/cms/documentList`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching document list:', error);
throw error;
}
});
};

View File

@ -1,13 +1,17 @@
import GetHomepage from '@/graphql/homepage.graphql?raw';
export const useHomepage = () => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`homepage-${locale}`, async () => {
return await $directus.query<{ homepage: Homepage }>(GetHomepage, {
locale: locale,
});
try {
const data = $fetch(`/api/cms/homepage`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching homepage:', error);
throw error;
}
});
};

View File

@ -1,15 +1,17 @@
import GetProduct from '@/graphql/product.graphql?raw';
export const useProduct = (id: string) => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`product-${id}-${locale}`, async () => {
return await $directus.query<{ products_by_id: Product }>(GetProduct, {
id: id,
locale: locale,
});
try {
const data = await $fetch(`/api/cms/product/${id}`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching product: ', error);
throw error;
}
});
};

View File

@ -1,14 +1,17 @@
import GetProductList from '@/graphql/productList.graphql?raw';
export const useProductList = () => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`product-list-${locale}`, async () => {
return await $directus.query<{ products: Product[] }>(GetProductList, {
locale: locale,
});
try {
const data = await $fetch(`/api/cms/productList`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching product list: ', error);
throw error;
}
});
};

View File

@ -1,14 +1,17 @@
import GetQuestionList from '@/graphql/questionList.graphql?raw';
export const useQuestionList = () => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`question-list-${locale}`, async () => {
return await $directus.query<{ questions: Question[] }>(GetQuestionList, {
locale: locale,
});
try {
const data = $fetch(`/api/cms/questionList`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching question list:', error);
throw error;
}
});
};

View File

@ -1,14 +1,17 @@
import GetSolution from '@/graphql/solution.graphql?raw';
export const useSolution = (id: string) => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`solution-${id}-${locale}`, async () => {
return await $directus.query<{ solutions_by_id: Solution }>(GetSolution, {
id: id,
locale: locale,
});
try {
const data = await $fetch(`/api/cms/solution/${id}`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching solution: ', error);
throw error;
}
});
};

View File

@ -1,13 +1,17 @@
import GetSolutionList from '@/graphql/solutionList.graphql?raw';
export const useSolutionList = () => {
const { $directus } = useNuxtApp();
const { getDirectusLocale } = useLocalizations();
const locale = getDirectusLocale();
return useAsyncData(`solution-list-${locale}`, async () => {
return await $directus.query<{ solutions: Solution[] }>(GetSolutionList, {
locale: locale,
});
try {
const data = $fetch(`/api/cms/solutionList`, {
headers: { 'x-locale': locale },
});
return data;
} catch (error) {
logger.error('Error fetching solution list:', error);
throw error;
}
});
};

View File

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

View File

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

View File

@ -1,31 +0,0 @@
query GetDocumentList($locale: String!) {
product_documents(filter: { status: { _eq: "published" } }) {
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

@ -1,33 +0,0 @@
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

@ -1,65 +0,0 @@
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(filter: { status: { _eq: "published" } }) {
id
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
content
}
}
}
documents {
id
product_documents_id(filter: { status: { _eq: "published" } }) {
id
file {
id
filesize
filename_download
}
translations(filter: { languages_code: { code: { _eq: $locale } } }) {
id
title
}
}
}
}
}

View File

@ -1,22 +0,0 @@
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
}
}
}

View File

@ -1,27 +0,0 @@
query GetQuestionList($locale: String!) {
questions(filter: { status: { _eq: "published" } }) {
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

@ -1,12 +0,0 @@
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

@ -1,21 +0,0 @@
query GetSolutionList($locale: String!) {
solutions(filter: { status: { _eq: "published" } }) {
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

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

View File

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

View File

@ -39,7 +39,7 @@
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) {
@ -61,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 || '',
});
</script>
<style scoped>

View File

@ -46,7 +46,7 @@
{ label: $t('navigation.products') },
];
const productsRaw = computed(() => data.value.products ?? []);
const productsRaw = computed(() => data.value ?? []);
const products = computed(() =>
productsRaw.value.map((item) => toProductListView(item))

View File

@ -35,7 +35,7 @@
if (!data.value) {
return null;
}
return toSolutionView(data.value.solutions_by_id);
return toSolutionView(data.value);
});
const breadcrumbItems = computed(() => [

View File

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

View File

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

View File

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

View File

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