feat(hjc-web): 标书详情加「收藏」按钮,「我的收藏」页与顶栏入口
后端接口是端无关的,本轮 **mp-java 零改动**,纯前端 + Nitro 代理。 - 新增 favorites.get/post/delete 三个站内代理,原样透传 ApiResult (HTTP 恒 200,按 body.code 判登录态与业务失败)。delete 用查询串传 projectId。 - detail.get.ts 补发 Authorization:详情接口内联了 favorited,而它此前是匿名调用, 已收藏的项目会显示成「未收藏」。未登录时不带这个头,行为与改动前完全一致 (返回体仍整体返回 data,favorited 正是靠这一点透传的)。 - 「我的收藏」做成**模板无关的公共路由** app/pages/tender/favorites.vue(10 套模板共用), 顶栏 HjcUserBar 在「我的订单」后加入口。列表按收藏时间倒序 + 加载更多; 只渲染后端算好的 saleState/purchased(前端不再判一遍);失效项目**保留条目**、 点击只提示不进详情、仍可取消;取消收藏弹二次确认(复用 HjcConfirmDialog)。 取数用 useRequestFetch —— 裸 $fetch 在 SSR 不转发 cookie,会被代理当成匿名调用, 拿到 401 后 handleAuthCode 会清凭据跳登录,等于刷新页面把自己登出。 - template-07 详情页加收藏按钮(heart SVG 两态,不引图标库),未登录点它跳登录、 回跳带 favorite=1 后自动补一次收藏(只在客户端 onMounted 做,不改写 URL—— 详情壳是 :key="route.fullPath",改 query 会重挂载并与收藏请求赛跑)。 取数同样改用 useRequestFetch,否则首屏 favorited 恒为 false。 - 只改 template-07:10 套模板里只有它有 TenderDetail.vue,其余按需再补。
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { $fetch } from 'ofetch'
|
||||
import { createError, defineEventHandler, getRouterParam } from 'h3'
|
||||
import { createError, defineEventHandler, getRouterParam, getHeader, getCookie } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
import { getTenantFromContext } from '../../utils/tenant'
|
||||
|
||||
@@ -7,12 +7,23 @@ import { getTenantFromContext } from '../../utils/tenant'
|
||||
* 标书项目详情
|
||||
* GET /api/tender/detail?id=1 (或 /api/tender/{id})
|
||||
* 代理到 mp-api(mp-java) 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' })
|
||||
@@ -21,7 +32,10 @@ export default defineEventHandler(async (event) => {
|
||||
try {
|
||||
const res = await $fetch(`/hjc/bid-project/${id}`, {
|
||||
baseURL: modulesApiBase,
|
||||
headers: { TenantId: ctx.tenantId }
|
||||
headers: {
|
||||
TenantId: ctx.tenantId,
|
||||
...(auth ? { Authorization: auth } : {})
|
||||
}
|
||||
}) as any
|
||||
|
||||
const data = res && res.data !== undefined ? res.data : res
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { $fetch } from 'ofetch'
|
||||
import { createError, defineEventHandler, getHeader, getCookie, getQuery } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
import { getTenantFromContext } from '../../utils/tenant'
|
||||
|
||||
/**
|
||||
* 取消收藏(幂等)
|
||||
* DELETE /api/tender/favorites?projectId=123
|
||||
* 代理到 mp-api /api/hjc/project-favorite/{projectId};需登录态。
|
||||
*
|
||||
* 约定:原样透传 ApiResult{code,message,data}。后端**恒返回 code=0**
|
||||
* (本来就没收藏也算成功),结论在 data 里:`{ projectId, favorited: false }`。
|
||||
*
|
||||
* projectId 走查询串而不是请求体:DELETE 带 body 在部分中间层会被丢掉,
|
||||
* 而它本来就是个标识符,放查询串更自然。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const ctx = getTenantFromContext(event, config)
|
||||
const projectId = (getQuery(event) as any)?.projectId
|
||||
const cookieToken = getCookie(event, 'hjc_token')
|
||||
const auth = getHeader(event, 'authorization') || (cookieToken ? `Bearer ${cookieToken}` : null)
|
||||
|
||||
if (!projectId) {
|
||||
throw createError({ statusCode: 400, statusMessage: '缺少 projectId' })
|
||||
}
|
||||
|
||||
try {
|
||||
return await $fetch(`/hjc/project-favorite/${projectId}`, {
|
||||
baseURL: config.public.modulesApiBase,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
TenantId: ctx.tenantId,
|
||||
...(auth ? { Authorization: auth } : {})
|
||||
},
|
||||
query: { TenantId: ctx.tenantId }
|
||||
})
|
||||
} catch (error: any) {
|
||||
throw createError({
|
||||
statusCode: error?.statusCode || error?.response?.status || 502,
|
||||
statusMessage: error?.statusMessage || 'Failed to remove favorite'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,44 @@
|
||||
import { $fetch } from 'ofetch'
|
||||
import { createError, defineEventHandler, getHeader, getCookie, getQuery } from 'h3'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
import { getTenantFromContext } from '../../utils/tenant'
|
||||
|
||||
/**
|
||||
* 我的收藏(标书项目)
|
||||
* GET /api/tender/favorites?page=1&limit=12
|
||||
* 代理到 mp-api /api/hjc/project-favorite/page;需登录态。
|
||||
*
|
||||
* 约定:原样透传 ApiResult{code,message,data}。HTTP 状态码恒为 200,
|
||||
* 「未登录/token 失效」是 code=401、「无权限」是 code=403,前端必须按 code 判定;
|
||||
* 故这里**不能**只取 data,否则登录态失败会被误当成「没有收藏」。
|
||||
*
|
||||
* 刻意**不转发**客户端的 sort/order:后端固定按收藏时间倒序,且会丢弃客户端排序
|
||||
* (这个查询是连表,放行排序有歧义列的风险)。少转发一个参数就少一处「看起来能排序」的错觉。
|
||||
*/
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const ctx = getTenantFromContext(event, config)
|
||||
const cookieToken = getCookie(event, 'hjc_token')
|
||||
const auth = getHeader(event, 'authorization') || (cookieToken ? `Bearer ${cookieToken}` : null)
|
||||
const q = getQuery(event)
|
||||
|
||||
try {
|
||||
return await $fetch('/hjc/project-favorite/page', {
|
||||
baseURL: config.public.modulesApiBase,
|
||||
headers: {
|
||||
TenantId: ctx.tenantId,
|
||||
...(auth ? { Authorization: auth } : {})
|
||||
},
|
||||
query: {
|
||||
TenantId: ctx.tenantId,
|
||||
page: q.page,
|
||||
limit: q.limit
|
||||
}
|
||||
})
|
||||
} catch (error: any) {
|
||||
throw createError({
|
||||
statusCode: error?.statusCode || error?.response?.status || 502,
|
||||
statusMessage: error?.statusMessage || 'Failed to fetch my favorites'
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,39 @@
|
||||
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'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user