nomadro/apps/api/services/ebook_svc/routers/shop.py
Eric 3f7528f824 Add VIP knowledge community and device shop with native weapp UX.
Document memo/shop admin routes, deploy scripts, and cache-first feed performance in README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-06 16:58:17 +08:00

64 lines
2.1 KiB
Python
Raw 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.

"""小程序电商公开 API:目录 / 下单查询。支付走 /pay/shop-order。"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
from ebook_svc.core.deps import get_current_openid
from ebook_svc.services.shop_store import shop_store
from ebook_svc.services.store import store as ebook_store
from ebook_svc.services.vip_config import read_vip_config
router = APIRouter(prefix="/shop", tags=["shop"])
VIP_BOOK_ID = "__vip__"
def _require_vip(openid: str) -> None:
cfg = read_vip_config()
if cfg.get("is_free") or int(cfg.get("vipPriceFen") or 0) <= 0:
return
if not ebook_store.is_unlocked(openid, VIP_BOOK_ID):
raise HTTPException(403, "开通 VIP 后可使用设备商城")
@router.get("/catalog")
async def catalog() -> dict[str, Any]:
"""商品目录(可匿名;下单仍需 VIP + 登录)。"""
return shop_store.catalog_public()
@router.get("/goods/{product_id}")
async def goods_detail(product_id: str) -> dict[str, Any]:
p = shop_store.get_product(product_id)
if not p:
raise HTTPException(404, "商品不存在")
# 不向前端暴露未配置的 wx 道具细节以外字段已安全
return {"ok": True, "product": p}
class MyOrdersQuery(BaseModel):
limit: int = Field(default=50, ge=1, le=100)
@router.get("/my-orders")
async def my_orders(openid: str = Depends(get_current_openid)) -> dict[str, Any]:
_require_vip(openid)
items = shop_store.list_orders(openid=openid, limit=50)
# 用户侧可看卡密
return {"ok": True, "items": items}
@router.get("/orders/{order_id}")
async def order_detail(order_id: str, openid: str = Depends(get_current_openid)) -> dict[str, Any]:
_require_vip(openid)
order = shop_store.get_order(order_id)
if not order or order["openid"] != openid:
# 也允许用 out_trade_no
order = shop_store.get_order_by_out_trade_no(order_id)
if not order or order["openid"] != openid:
raise HTTPException(404, "订单不存在")
return {"ok": True, "order": order}