feat(app): 添加多模板关于我们页面及相关路由和404页面
- 新增404页面,优化未找到页面体验,避免被搜索引擎索引 - 增加文件代理接口,隐藏真实文件服务器地址,支持文件请求代理 - 实现/article、/case、/product及/page动态路由兼容列表与详情展示 - 添加动态CMS页面兼容入口处理旧式路径,统一路由与SEO设置 - 新增模板1、模板7、模板2、模板3关于我们页面,实现多模板支持 - 模板增强支持CMS单页内容加载及SEO信息动态设置 - 配置环境变量及Git忽略文件规则辅助开发和构建环境管理
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { $fetch } from 'ofetch'
|
||||
import { createError, defineEventHandler, getQuery } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
import { getTenantFromContext } from '../../utils/tenant'
|
||||
|
||||
/**
|
||||
* 产品详情
|
||||
* GET /api/product/detail?id=1
|
||||
* 代理到 SaaS 后端 CMS 产品详情接口
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const ctx = getTenantFromContext(event, config)
|
||||
const query = getQuery(event)
|
||||
const modulesApiBase = config.public.modulesApiBase as string
|
||||
|
||||
if (!query.id) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Missing id parameter' })
|
||||
}
|
||||
|
||||
let res: any
|
||||
try {
|
||||
// 上游 cms-website 服务没有 productDetail 这类路由(会被 /cms/cms-website/{id} 兜底捕获并因 id 非整数报错),
|
||||
// 正确的产品详情端点属于 cms-product 控制器,且 id 走路径参数:/cms/cms-product/{id}
|
||||
res = await $fetch(`/cms/cms-product/${query.id}`, {
|
||||
baseURL: modulesApiBase,
|
||||
headers: {
|
||||
TenantId: ctx.tenantId
|
||||
},
|
||||
// TenantId 同时放入 query 兜底(详情端点实测认 query 的 TenantId)
|
||||
query: { ...query, TenantId: ctx.tenantId }
|
||||
})
|
||||
} catch (error: any) {
|
||||
throw createError({
|
||||
statusCode: error?.statusCode || error?.response?.status || 502,
|
||||
statusMessage: error?.statusMessage || 'Failed to fetch product detail'
|
||||
})
|
||||
}
|
||||
|
||||
// C 端不展示草稿(1)/下架(2)/已删除产品:status 非 0(已发布) 或 deleted=1 视为不存在
|
||||
// 上游真实枚举:0=已发布 1=草稿 2=下架(与早期假设相反,已于 2026-07-29 校正)
|
||||
// 上游对不存在/未发布产品常返回 {code:1, message:"...不存在"}(无 data),
|
||||
// 此时 res.code !== 0,必须 404,否则扁平后的对象为 truthy,前端会误判为「存在」渲染空页。
|
||||
const detail = res?.data ?? res
|
||||
if (typeof res?.code === 'number' && res.code !== 0) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Product not found or unpublished' })
|
||||
}
|
||||
if (!detail || detail.status === undefined || detail.status !== 0 || detail.deleted === 1) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Product not found or unpublished' })
|
||||
}
|
||||
|
||||
// 字段兼容归一化:上游 CMS 不同版本字段名不一致,统一补齐,
|
||||
// 避免前端 10 套模板的 <RichText :content> / subtitle 取不到值而渲染空白。
|
||||
// - content(富文本正文):部分版本放在 description,兜底回退(content 优先)
|
||||
// - subtitle(副标题):上游多数版本返回 summary,兜底回退
|
||||
// - navigationId:上游用 categoryId,组件用它拼「返回列表」链接
|
||||
if (detail && typeof detail === 'object') {
|
||||
if (detail.content == null || detail.content === '') {
|
||||
detail.content = detail.description || ''
|
||||
}
|
||||
if (detail.subtitle == null || detail.subtitle === '') {
|
||||
detail.subtitle = detail.summary || ''
|
||||
}
|
||||
if (detail.navigationId == null && detail.categoryId != null) {
|
||||
detail.navigationId = detail.categoryId
|
||||
}
|
||||
}
|
||||
|
||||
// 返回扁平数据,去掉上游 {code, message, data} 包装层
|
||||
return detail
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
import { $fetch } from 'ofetch'
|
||||
import { createError, defineEventHandler, getQuery } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
import { getTenantFromContext } from '../../utils/tenant'
|
||||
|
||||
/**
|
||||
* 产品列表
|
||||
* GET /api/product/list?categoryId=1&page=1&limit=10
|
||||
* 代理到 SaaS 后端 CMS 产品列表接口
|
||||
* 注意:上游 cms-website 服务没有 productList 这类路由(会被 /cms/cms-website/{id} 兜底捕获并因 id 非整数报错),
|
||||
* 正确的产品列表端点属于 cms-product 控制器:/cms/cms-product/page
|
||||
*
|
||||
* 返回结构:与 case/list 对齐,去掉上游包装层,归一化为 { list, count },
|
||||
* 前端 ProductList 直接读 data.value?.list / data.value?.count,分页稳定可用。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const ctx = getTenantFromContext(event, config)
|
||||
const query = getQuery(event)
|
||||
const modulesApiBase = config.public.modulesApiBase as string
|
||||
|
||||
try {
|
||||
// 前端按导航模型约定传 navigationId;上游 cms-product 实际按 categoryId 过滤,
|
||||
// 二者在 CMS 中同值(产品分类 ID == 对应栏目 navigationId),故透传 categoryId。
|
||||
// 同时保留 navigationId 以兼容少数按 navigationId 过滤的租户。
|
||||
const categoryId = query.navigationId ?? query.categoryId
|
||||
// 聚合场景:前端递归收集父栏目下所有子栏目的 navigationId,以逗号串传入。
|
||||
// 有 categoryIds 时优先走 IN 查询,忽略单值 categoryId,避免 AND 冲突。
|
||||
const categoryIds = query.categoryIds as string | undefined
|
||||
|
||||
const res = await $fetch('/cms/cms-product/page', {
|
||||
baseURL: modulesApiBase,
|
||||
headers: {
|
||||
TenantId: ctx.tenantId
|
||||
},
|
||||
// TenantId 同时放入 query 兜底(page 端点实测认 query 的 TenantId)
|
||||
// status 0=已发布 1=草稿 2=下架(与早期假设相反,已于 2026-07-29 校正),C 端只展示已发布,放最后覆盖前端传入
|
||||
query: {
|
||||
...query,
|
||||
navigationId: categoryIds ? undefined : query.navigationId,
|
||||
categoryId: categoryIds ? undefined : categoryId,
|
||||
categoryIds: categoryIds || undefined,
|
||||
TenantId: ctx.tenantId,
|
||||
status: 0,
|
||||
// 同时排除软删除(deleted=1)的产品,兜底防止已删除内容出现在前台
|
||||
deleted: 0
|
||||
}
|
||||
})
|
||||
|
||||
// 与 case/list 对齐:去掉上游包装层,归一化为 { list, count }
|
||||
// 上游 /cms/cms-product/page 返回 { code, message, data: { list, count } } 或 { list, count },
|
||||
// 字段名可能为 count 或 total,统一兜底。
|
||||
const envelopeData = (res && res.data !== undefined ? res.data : res) || {}
|
||||
const rawList = Array.isArray(envelopeData.list) ? envelopeData.list : []
|
||||
const count = envelopeData.count ?? envelopeData.total ?? rawList.length
|
||||
|
||||
return { list: rawList, count }
|
||||
} catch (error: any) {
|
||||
throw createError({
|
||||
statusCode: error?.statusCode || error?.response?.status || 502,
|
||||
statusMessage: error?.statusMessage || 'Failed to fetch product list'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user