refactor: 将Data到ViewModel的转换由App转移至Server端
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
- 将逻辑转移到Server端后,简化前端逻辑
This commit is contained in:
7
server/api/cms/companyProfile.get.ts
Normal file
7
server/api/cms/companyProfile.get.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { companyProfileService } from '~~/server/services/cms/companyProfileService';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return companyProfileService.getCompanyProfile(locale);
|
||||
});
|
||||
@ -1,24 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/companyProfile.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ company_profile: CompanyProfile }>(
|
||||
query,
|
||||
{
|
||||
locale: locale,
|
||||
}
|
||||
);
|
||||
|
||||
const companyProfile = data?.company_profile;
|
||||
|
||||
return companyProfile;
|
||||
});
|
||||
7
server/api/cms/contactInfo.get.ts
Normal file
7
server/api/cms/contactInfo.get.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { contactInfoService } from '~~/server/services/cms/contactInfoService';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return contactInfoService.getContactInfo(locale);
|
||||
});
|
||||
@ -1,21 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/contactInfo.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ contact_info: ContactInfo }>(query, {
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const contactInfo = data?.contact_info;
|
||||
|
||||
return contactInfo;
|
||||
});
|
||||
7
server/api/cms/documentList.get.ts
Normal file
7
server/api/cms/documentList.get.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { documentService } from '~~/server/services/cms/documentService';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return documentService.getDocumentList(locale);
|
||||
});
|
||||
@ -1,24 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/documentList.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ product_documents: ProductDocument[] }>(
|
||||
query,
|
||||
{
|
||||
locale: locale,
|
||||
}
|
||||
);
|
||||
|
||||
const documents = data?.product_documents;
|
||||
|
||||
return documents;
|
||||
});
|
||||
7
server/api/cms/homepage.get.ts
Normal file
7
server/api/cms/homepage.get.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { homepageService } from '~~/server/services/cms/homepageService';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return homepageService.getHomepage(locale);
|
||||
});
|
||||
@ -1,21 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/homepage.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ homepage: Homepage }>(query, {
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const homepage = data?.homepage;
|
||||
|
||||
return homepage;
|
||||
});
|
||||
13
server/api/cms/product/[id].get.ts
Normal file
13
server/api/cms/product/[id].get.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { productService } from '~~/server/services/cms/productService';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, 'id');
|
||||
if (!id)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Product ID is required',
|
||||
});
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return productService.getProductById(id, locale);
|
||||
});
|
||||
@ -1,33 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, 'id');
|
||||
if (!id)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Product ID is required',
|
||||
});
|
||||
|
||||
const query = await loadAssetAsString(
|
||||
'assets/server',
|
||||
'graphql/product.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ products_by_id: Product }>(query, {
|
||||
id: id,
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const product = data?.products_by_id;
|
||||
|
||||
if (!product || product.status === 'archived') {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Product not found' });
|
||||
}
|
||||
|
||||
return product;
|
||||
});
|
||||
6
server/api/cms/productList.get.ts
Normal file
6
server/api/cms/productList.get.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { productService } from '~~/server/services/cms/productService';
|
||||
export default defineEventHandler(async (event) => {
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return productService.getProductList(locale);
|
||||
});
|
||||
@ -1,21 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/productList.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ products: Product[] }>(query, {
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const products = data?.products || [];
|
||||
|
||||
return products;
|
||||
});
|
||||
6
server/api/cms/questionList.get.ts
Normal file
6
server/api/cms/questionList.get.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { questionService } from '~~/server/services/cms/questionService';
|
||||
export default defineEventHandler(async (event) => {
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return questionService.getQuestionList(locale);
|
||||
});
|
||||
@ -1,21 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/questionList.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ questions: Question[] }>(query, {
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const questions = data?.questions || [];
|
||||
|
||||
return questions;
|
||||
});
|
||||
14
server/api/cms/solution/[id].get.ts
Normal file
14
server/api/cms/solution/[id].get.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { solutionService } from '~~/server/services/cms/solutionService';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, 'id');
|
||||
if (!id)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Solution ID is required',
|
||||
});
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return solutionService.getSolutionById(id, locale);
|
||||
});
|
||||
@ -1,33 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const id = getRouterParam(event, 'id');
|
||||
if (!id)
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Solution ID is required',
|
||||
});
|
||||
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/solution.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ solutions_by_id: Solution }>(query, {
|
||||
id: id,
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const solution = data?.solutions_by_id;
|
||||
|
||||
if (!solution || solution.status === 'archived') {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Solution not found' });
|
||||
}
|
||||
|
||||
return solution;
|
||||
});
|
||||
7
server/api/cms/solutionList.get.ts
Normal file
7
server/api/cms/solutionList.get.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { solutionService } from '~~/server/services/cms/solutionService';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
return solutionService.getSolutionList(locale);
|
||||
});
|
||||
@ -1,21 +0,0 @@
|
||||
import { directus } from '~~/server/utils/directus';
|
||||
import { loadAssetAsString } from '~~/server/utils/serverAssets';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const query = await loadAssetAsString(
|
||||
'assets:server',
|
||||
'graphql/solutionList.graphql'
|
||||
);
|
||||
if (!query)
|
||||
throw createError({ statusCode: 500, message: 'GraphQL query not found' });
|
||||
|
||||
const locale = getHeader(event, 'x-locale') || 'zh-CN';
|
||||
|
||||
const data = await directus.query<{ solutions: Solution[] }>(query, {
|
||||
locale: locale,
|
||||
});
|
||||
|
||||
const solutions = data?.solutions || [];
|
||||
|
||||
return solutions;
|
||||
});
|
||||
Reference in New Issue
Block a user