Files

56 lines
2.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { $fetch } from 'ofetch'
import { createError, defineEventHandler, getRouterParam, getHeader, getCookie } from 'h3'
import { useRuntimeConfig } from '#imports'
import { getTenantFromContext } from '../../utils/tenant'
/**
* 标书项目详情
* GET /api/tender/detail?id=1 (或 /api/tender/{id})
* 代理到后端 hjc 标书项目详情,返回扁平化项目对象;不存在则 404。
*
* 返回体**整体**返回后端 data(不逐字段挑选):后端新增的 `favorited`(是否已收藏)
* 正是靠这一点透传过来的,改成字段白名单就会把它丢掉。
*/
export default defineEventHandler(async (event) => {
const config = useRuntimeConfig()
const ctx = getTenantFromContext(event, config)
const id = getRouterParam(event, 'id') || (getQuery(event) as any)?.id
const modulesApiBase = config.public.modulesApiBase as string
/**
* 转发登录态。详情接口本身是**公开**的(匿名也返回 code=0),但它的返回体里内联了一个
* `favorited`:匿名调用时后端按契约返回 `false`,页面就会把已收藏的项目显示成「未收藏」。
*
* 未登录时没有 cookie → 不带这个头 → 行为与改动前**完全一致**(向后兼容)。
*/
const cookieToken = getCookie(event, 'hjc_token')
const auth = getHeader(event, 'authorization') || (cookieToken ? `Bearer ${cookieToken}` : null)
if (!id) {
throw createError({ statusCode: 400, statusMessage: '缺少 id' })
}
try {
const res = await $fetch(`/hjc/bid-project/${id}`, {
baseURL: modulesApiBase,
headers: {
TenantId: ctx.tenantId,
...(auth ? { Authorization: auth } : {})
}
}) as any
const data = res && res.data !== undefined ? res.data : res
if (!data) {
throw createError({ statusCode: 404, statusMessage: '标书项目不存在' })
}
return data
} catch (error: any) {
if (error?.statusCode === 404) {
throw error
}
throw createError({
statusCode: error?.statusCode || error?.response?.status || 502,
statusMessage: error?.statusMessage || 'Failed to fetch tender detail'
})
}
})