import { $fetch } from 'ofetch' import { createError, defineEventHandler, readBody, getHeader, getCookie } from 'h3' import { useRuntimeConfig } from '#imports' import { getTenantFromContext } from '../../utils/tenant' /** * 收藏一个标书项目(幂等) * POST /api/tender/favorites { projectId } * 代理到 mp-api /api/hjc/project-favorite;需登录态。 * * 约定:原样透传 ApiResult{code,message,data},由前端按 body.code 判定登录态与业务失败。 * 后端**恒返回 code=0**(重复收藏也是成功),结论在 data 里:`{ projectId, favorited }`。 * 收藏**不需要企业资质**,所以这里也没有资质相关的判定。 */ export default defineEventHandler(async (event) => { const config = useRuntimeConfig() const ctx = getTenantFromContext(event, config) const body = await readBody(event) const cookieToken = getCookie(event, 'hjc_token') const auth = getHeader(event, 'authorization') || (cookieToken ? `Bearer ${cookieToken}` : null) try { return await $fetch('/hjc/project-favorite', { baseURL: config.public.modulesApiBase, method: 'POST', headers: { TenantId: ctx.tenantId, ...(auth ? { Authorization: auth } : {}) }, query: { TenantId: ctx.tenantId }, body }) } catch (error: any) { throw createError({ statusCode: error?.statusCode || error?.response?.status || 502, statusMessage: error?.statusMessage || 'Failed to add favorite' }) } })