访客首页推荐、产品列表、产品详情此前读的是前端手写的 `app/static/portal/common/mock-data.js`:只有 8 只,且**其中 6 只根本不在 `fin_product` 里**(159915 / 512100 / 513100 / 511360 / 159645 / 159925), 还把海富通的 `511360` 标成"南方短融ETF"、把 `510500` 净值写成 6.742(真实 7.6027)。 `README.md` 把它记为"公开产品 HTTP 接口尚未实现"的临时方案。 ## 接口 新增 `GET /api/v1/products`(编号 **P001**,已在 `docs/05` §19 总目录与 §19 说明中登记): - **要求有效令牌但不校验权限码**:访客令牌的角色是 `visitor`、不带任何权限, 这与 `/api/v1/agent-runs`、`/api/v1/conversations` 面向访客的口径一致; 产品信息本身是公开信息。数据面只暴露 `fin_product`(`status='上市'`)与 `fin_market_price` 的最新一行,**不含任何账户/客户字段**(由 integration 测试守着)。 - 字段与 `mock-data.js` 对齐,因此前端渲染与筛选逻辑**一行未改**。 - `change_pct` **可能是 `null`**:当日涨跌需要两个交易日的收盘价,行情只同步过一天时 算不出来。前端对 `null` 显示"暂无" —— `formatPercent(null)` 会渲染成 `+0.00%`, 那等于对客户说"今天平盘",是编出来的结论。 ## 前端 - 新增 `common/visitor-token.js`:访客令牌的**唯一实现**(这段逻辑原先只写在客服浮窗里, 现在四处要用;复制四份的话存储 key 与过期判断迟早不一致);`widget.js` 改为复用它。 - 新增 `common/product-notes.js`:产品级披露文案。510300"非本公司发行"那条是**合规披露**, 不能随 mock 一起删掉。 - 三个访客页改读接口,并显式区分 loading / error 状态。 - **删除 `common/mock-data.js`**。 - 产品详情页**不再画走势图**:`fin_nav_history` 目前 0 行,此前那条曲线是 mock 里 12 个编造点位 —— 走势图最容易被当成真数据,宁可不画,并显式说明"尚未接入"。 ## 顺带修正 `min_amount` 在库里全是 0.00(那本是场外"最小认购金额"的概念,对场内按手交易的 ETF 不适用),页面不再显示"¥0.00"(会被读成零元起购),改为"1 手(100 份)起"。 验证:接口实测 `count=20`;ruff 通过;mypy 251 文件 0 错;unit+contract 1387 passed; integration 106 passed;e2e 冒烟 40/40。
137 lines
5.5 KiB
Python
137 lines
5.5 KiB
Python
from typing import Any, Literal
|
||
|
||
from fastapi import APIRouter, Depends, Header
|
||
from pydantic import Field
|
||
|
||
from app.api.dependencies.auth import build_request_context
|
||
from app.api.dependencies.rate_limit import enforce_rate_limit
|
||
from app.api.schemas.admin import StrictPayload
|
||
from app.api.schemas.conversations import HandoverRequest
|
||
from app.core.contracts import RequestContext
|
||
from app.service.public_platform_service import PublicPlatformService
|
||
from app.service.public_product_service import PublicProductService
|
||
|
||
router = APIRouter(prefix="/api/v1", tags=["public-platform"],
|
||
dependencies=[Depends(enforce_rate_limit)])
|
||
|
||
|
||
class SessionCreate(StrictPayload):
|
||
agent_type: str = Field(pattern=r"^[a-z][a-z0-9_]{1,31}$")
|
||
|
||
|
||
class Cancellation(StrictPayload):
|
||
reason: str = Field(default="user_cancelled", max_length=128)
|
||
|
||
|
||
class CandidateDecisionPayload(StrictPayload):
|
||
decision: Literal["confirmed", "rejected"]
|
||
|
||
|
||
@router.post("/conversations", status_code=201)
|
||
async def create_session(
|
||
payload: SessionCreate,
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("create", "", context, key, payload.model_dump())
|
||
|
||
|
||
@router.get("/conversations/{session_id}")
|
||
async def get_session(
|
||
session_id: str, context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().session(session_id, context)
|
||
|
||
|
||
@router.post("/conversations/{session_id}/closures")
|
||
async def close_session(
|
||
session_id: str, key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("close", session_id, context, key, {})
|
||
|
||
|
||
@router.post("/agent-runs/{run_id}/cancellations", status_code=202)
|
||
async def cancel_run(
|
||
run_id: str, payload: Cancellation,
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("cancel", run_id, context, key, payload.model_dump())
|
||
|
||
|
||
@router.get("/handover-requests/{handover_id}")
|
||
async def get_handover(
|
||
handover_id: str, context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().handover(handover_id, context)
|
||
|
||
|
||
# 转人工申请的唯一入口:必须走 PublicPlatformService.write("handover"),
|
||
# 它在同一事务内写工单 + Outbox 事件(conversation.transfer_requested)+ 审计。
|
||
# 历史上 conversations.py 另有一份只写审计的实现,因注册顺序覆盖了本入口,
|
||
# 导致转人工的异步链路(Outbox → Worker)永不触发,已合并删除。
|
||
@router.post("/conversations/{session_id}/handover-requests", status_code=202)
|
||
async def request_handover(
|
||
session_id: str,
|
||
payload: HandoverRequest,
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write(
|
||
"handover", session_id, context, key, payload.model_dump()
|
||
)
|
||
|
||
|
||
@router.get("/users/me/memory-profile")
|
||
async def my_memory(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().memory(int(context.user_id), context)
|
||
|
||
|
||
@router.get("/customers/{customer_id}/memory-profile")
|
||
async def customer_memory(
|
||
customer_id: int, context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().memory(customer_id, context)
|
||
|
||
|
||
@router.get("/users/me/memory-candidates")
|
||
async def my_memory_candidates(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""返回当前用户可确认的画像候选,不返回证据原文。"""
|
||
from app.service.customer_profile_candidate_service import CustomerProfileCandidateService
|
||
|
||
return await CustomerProfileCandidateService().list_for_customer(context)
|
||
|
||
|
||
@router.post("/users/me/memory-candidates/{candidate_id}/decisions")
|
||
async def decide_memory_candidate(
|
||
candidate_id: int,
|
||
payload: CandidateDecisionPayload,
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""用户确认或拒绝自己的候选;确认后仍需管理员审核才能激活。"""
|
||
from app.service.customer_profile_candidate_service import CustomerProfileCandidateService
|
||
|
||
return await CustomerProfileCandidateService().decide_by_customer(
|
||
candidate_id, payload.decision, context
|
||
)
|
||
|
||
|
||
@router.get("/products")
|
||
async def list_products(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""公开产品列表:在售场内基金 + 各自最新行情(访客令牌即可访问)。
|
||
|
||
这是访客三个页面(首页推荐 / 产品列表 / 产品详情)的数据源,
|
||
替代原先前端手写的 `common/mock-data.js`。
|
||
|
||
只要求**有效令牌**、不检查权限码:访客令牌的上下文只有 `roles=("visitor",)`
|
||
且不带权限,与 `/api/v1/conversations`、`/api/v1/agent-runs` 的访客口径一致。
|
||
"""
|
||
return await PublicProductService().list_products(context)
|