Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f37eef75f2 | |||
| 8fea6d2b5d | |||
| 1893c01f54 | |||
| 0b8037c725 | |||
| 1a893d8d96 |
@@ -1,48 +0,0 @@
|
||||
"""wechat_transfer_authorization 表(免确认收款授权)
|
||||
|
||||
商家转账「用户授权免确认收款模式」:用户授权一次后,后续提现免逐笔确认直接到账。
|
||||
一个用户一条(user_id 主键)。out_authorization_no 我方生成,authorization_id 微信 active 后返回。
|
||||
|
||||
Revision ID: wx_transfer_auth
|
||||
Revises: withdraw_review_ad_watch
|
||||
Create Date: 2026-06-06 12:00:00.000000
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'wx_transfer_auth'
|
||||
down_revision: Union[str, Sequence[str], None] = 'withdraw_review_ad_watch'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
'wechat_transfer_authorization',
|
||||
sa.Column('user_id', sa.Integer(), nullable=False),
|
||||
sa.Column('openid', sa.String(length=64), nullable=False),
|
||||
sa.Column('out_authorization_no', sa.String(length=64), nullable=False),
|
||||
sa.Column('authorization_id', sa.String(length=64), nullable=True),
|
||||
sa.Column('state', sa.String(length=16), server_default='pending', nullable=False),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
|
||||
sa.PrimaryKeyConstraint('user_id'),
|
||||
)
|
||||
with op.batch_alter_table('wechat_transfer_authorization', schema=None) as batch_op:
|
||||
batch_op.create_index(
|
||||
batch_op.f('ix_wechat_transfer_authorization_out_authorization_no'),
|
||||
['out_authorization_no'],
|
||||
unique=True,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table('wechat_transfer_authorization', schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f('ix_wechat_transfer_authorization_out_authorization_no'))
|
||||
op.drop_table('wechat_transfer_authorization')
|
||||
@@ -42,6 +42,7 @@ def battle(user: CurrentUser, db: DbSession) -> SavingsBattleOut:
|
||||
week_saved_cents=b.week_saved_cents,
|
||||
beat_percent=b.beat_percent,
|
||||
streak_days=b.streak_days,
|
||||
compare_count=b.compare_count,
|
||||
)
|
||||
|
||||
|
||||
|
||||
+2
-65
@@ -33,8 +33,6 @@ from app.schemas.welfare import (
|
||||
ExchangeInfoOut,
|
||||
ExchangeRequest,
|
||||
ExchangeResultOut,
|
||||
TransferAuthResultOut,
|
||||
TransferAuthStatusOut,
|
||||
UnbindWechatResultOut,
|
||||
WithdrawInfoOut,
|
||||
WithdrawOrderOut,
|
||||
@@ -157,18 +155,15 @@ def unbind_wechat(user: CurrentUser, db: DbSession) -> UnbindWechatResultOut:
|
||||
return UnbindWechatResultOut(bound=False)
|
||||
|
||||
|
||||
@router.get("/withdraw-info", response_model=WithdrawInfoOut, summary="提现额度/绑定状态/免确认开关")
|
||||
@router.get("/withdraw-info", response_model=WithdrawInfoOut, summary="提现额度/绑定状态")
|
||||
def withdraw_info(user: CurrentUser, db: DbSession) -> WithdrawInfoOut:
|
||||
u = db.get(User, user.id)
|
||||
# 顺带同步免确认授权状态(捕获首单确认后已生效的授权 pending→active),让开关展示实时
|
||||
auth = crud_wallet.sync_transfer_auth(db, user.id)
|
||||
return WithdrawInfoOut(
|
||||
min_cents=WITHDRAW_MIN_CENTS,
|
||||
max_cents=WITHDRAW_MAX_CENTS,
|
||||
wechat_bound=bool(u and u.wechat_openid),
|
||||
wechat_nickname=u.wechat_nickname if u else None,
|
||||
wechat_avatar_url=u.wechat_avatar_url if u else None,
|
||||
transfer_auth_enabled=bool(auth and auth.state == "active"),
|
||||
)
|
||||
|
||||
|
||||
@@ -196,19 +191,9 @@ def withdraw(req: WithdrawRequest, user: CurrentUser, db: DbSession) -> Withdraw
|
||||
except crud_wallet.InsufficientCashError as e:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="insufficient cash balance") from e
|
||||
|
||||
# 调试直发(非生产):skip_review=true 时跳过人工审核,立即发起微信转账(等价 admin approve)。
|
||||
# 双闸保护——客户端仅 debug 包下发此 flag,服务端仅非 prod 才认;任一道闸拦住即恢复正常审核,
|
||||
# 生产绝不会被客户端 flag 绕过审核。用于本地联调"提现→微信转账/免确认到账"全链路。
|
||||
if req.skip_review and not settings.is_prod and order.status == "reviewing":
|
||||
logger.warning(
|
||||
"withdraw skip_review(非prod调试直发,跳过人工审核立即打款) user_id=%d bill=%s",
|
||||
user.id, order.out_bill_no,
|
||||
)
|
||||
order = crud_wallet.execute_withdraw_transfer(db, order)
|
||||
|
||||
acc = crud_wallet.get_or_create_account(db, user.id)
|
||||
logger.info(
|
||||
"withdraw submitted user_id=%d cents=%d bill=%s status=%s",
|
||||
"withdraw submitted user_id=%d cents=%d bill=%s status=%s(待审核)",
|
||||
user.id, req.amount_cents, order.out_bill_no, order.status,
|
||||
)
|
||||
# 此刻 status=reviewing,尚未打款 → 无 package_info;App 据 status 提示"已提交,等待审核"。
|
||||
@@ -261,51 +246,3 @@ def withdraw_orders(
|
||||
items=[WithdrawOrderOut.model_validate(it) for it in items],
|
||||
next_cursor=next_cursor,
|
||||
)
|
||||
|
||||
|
||||
# ===== 免确认收款授权(用户授权免确认模式)=====
|
||||
# 开启一次后,后续提现走免确认转账直接到账,不再跳微信确认。绑定 openid 是前提。
|
||||
|
||||
|
||||
@router.post(
|
||||
"/transfer-auth",
|
||||
response_model=TransferAuthResultOut,
|
||||
summary="开启免确认到账(申请授权,返回拉起微信授权页的 package)",
|
||||
dependencies=[Depends(rate_limit(10, 60, "transfer-auth"))],
|
||||
)
|
||||
def open_transfer_auth(user: CurrentUser, db: DbSession) -> TransferAuthResultOut:
|
||||
if not settings.wxpay_auth_configured:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="wechat transfer auth not configured",
|
||||
)
|
||||
try:
|
||||
info = crud_wallet.apply_transfer_auth(db, user.id)
|
||||
except crud_wallet.WechatNotBoundError as e:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="wechat not bound") from e
|
||||
except crud_wallet.WithdrawTransferError as e:
|
||||
raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY, detail=str(e)) from e
|
||||
logger.info("open transfer-auth user_id=%d already_active=%s", user.id, info["already_active"])
|
||||
return TransferAuthResultOut(**info)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/transfer-auth/status",
|
||||
response_model=TransferAuthStatusOut,
|
||||
summary="查免确认授权状态(从微信授权页返回后轮询)",
|
||||
)
|
||||
def transfer_auth_status(user: CurrentUser, db: DbSession) -> TransferAuthStatusOut:
|
||||
auth = crud_wallet.sync_transfer_auth(db, user.id)
|
||||
state = auth.state if auth else "none"
|
||||
return TransferAuthStatusOut(state=state, enabled=(state == "active"))
|
||||
|
||||
|
||||
@router.post(
|
||||
"/transfer-auth/close",
|
||||
response_model=TransferAuthStatusOut,
|
||||
summary="关闭免确认到账(解除授权)",
|
||||
)
|
||||
def close_transfer_auth_endpoint(user: CurrentUser, db: DbSession) -> TransferAuthStatusOut:
|
||||
crud_wallet.close_transfer_auth(db, user.id)
|
||||
logger.info("close transfer-auth user_id=%d", user.id)
|
||||
return TransferAuthStatusOut(state="closed", enabled=False)
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
"""微信支付相关回调(一期:免确认收款授权结果通知 stub)。
|
||||
|
||||
⚠️ 一期不处理回调内容、不验签:授权状态以主动查询(query_transfer_authorization)为准。
|
||||
本端点仅向微信回 200 避免重试风暴,**绝不依据回调内容改账**。二期接入时必须先补
|
||||
V3 平台证书/公钥验签(Wechatpay-Signature) + APIv3 密钥 AEAD 解密,验签通过后方可信任并处理。
|
||||
|
||||
授权回调地址通过 settings.WXPAY_AUTH_NOTIFY_URL 配置,需指向本端点的公网地址。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
|
||||
logger = logging.getLogger("shagua.wxpay")
|
||||
|
||||
router = APIRouter(prefix="/api/v1/wxpay", tags=["wxpay"])
|
||||
|
||||
|
||||
@router.post("/transfer-auth-notify", summary="免确认收款授权结果通知(一期 stub:仅应答,不处理)")
|
||||
async def transfer_auth_notify(request: Request) -> dict:
|
||||
# 一期:不验签、不解密、不改账。仅记录 + 应答成功;真实授权状态靠 /transfer-auth/status 查询兜底。
|
||||
try:
|
||||
body = await request.json()
|
||||
logger.info(
|
||||
"transfer-auth notify id=%s type=%s", body.get("id"), body.get("event_type")
|
||||
)
|
||||
except Exception: # noqa: BLE001 — body 解析失败也照常应答 200,避免微信重试
|
||||
logger.info("transfer-auth notify (unparseable body)")
|
||||
return {"code": "SUCCESS", "message": "OK"}
|
||||
@@ -99,10 +99,6 @@ class Settings(BaseSettings):
|
||||
WXPAY_PUBLIC_KEY_PATH: str = "./secrets/pub_key.pem" # 微信支付平台公钥
|
||||
WXPAY_TRANSFER_SCENE_ID: str = "1000" # 转账场景 ID(1000=现金营销)
|
||||
WXPAY_REQUEST_TIMEOUT_SEC: int = 10
|
||||
# 免确认收款授权(用户授权免确认模式)的授权结果回调地址,必须公网可访问 HTTPS、不带参数。
|
||||
# 发起授权 / 首单顺带授权时作为 authorization_notify_url 传给微信。一期不处理回调内容
|
||||
# (授权状态靠 query 查询兜底),但微信要求该字段非空,故启用免确认前必须配置;留空时免确认相关接口返回未配置。
|
||||
WXPAY_AUTH_NOTIFY_URL: str = ""
|
||||
|
||||
@property
|
||||
def wxpay_configured(self) -> bool:
|
||||
@@ -114,11 +110,6 @@ class Settings(BaseSettings):
|
||||
and self.WXPAY_PUBLIC_KEY_ID
|
||||
)
|
||||
|
||||
@property
|
||||
def wxpay_auth_configured(self) -> bool:
|
||||
"""免确认收款授权可用 = 微信支付凭证齐全 + 授权回调地址已配。"""
|
||||
return bool(self.wxpay_configured and self.WXPAY_AUTH_NOTIFY_URL)
|
||||
|
||||
# ===== 穿山甲激励视频(服务端发奖回调)=====
|
||||
# 看完激励视频后穿山甲服务器回调本服务发金币(S2S,客户端被破解也刷不到)。
|
||||
# PANGLE_REWARD_SECRET 是穿山甲后台配置的"奖励校验密钥",验签用,从后台取到后填 .env。
|
||||
|
||||
+4
-3
@@ -34,8 +34,9 @@ COIN_PER_YUAN: int = 10000
|
||||
CENTS_PER_YUAN: int = 100
|
||||
# 1 分对应多少金币 = 100。兑换金币数必须是它的整数倍,否则会出现不足 1 分的零头。
|
||||
COIN_PER_CENT: int = COIN_PER_YUAN // CENTS_PER_YUAN
|
||||
# 单次兑换最少 1 元(避免大量 1 分级碎兑)
|
||||
MIN_EXCHANGE_COIN: int = COIN_PER_YUAN
|
||||
# 单次兑换最少 1 分(=COIN_PER_CENT;产品定:可兑 1 分起,与 step 同粒度)。早先为 1 元(=COIN_PER_YUAN),
|
||||
# 用户 2026-06 改为 1 分:让"换现金"小额即可用(也方便联调验证资产卡飞金币动画)。
|
||||
MIN_EXCHANGE_COIN: int = COIN_PER_CENT
|
||||
|
||||
|
||||
def coins_to_cents(coin_amount: int) -> int:
|
||||
@@ -54,7 +55,7 @@ TASK_ENABLE_NOTIFICATION = "enable_notification"
|
||||
|
||||
# task_key -> 奖励金币
|
||||
# 打开消息提醒: 1000 金币(=¥0.1, 客户端原型展示口径; 量级与签到/里程碑相称)。
|
||||
# 注意: 已不再 = 兑换下限(MIN_EXCHANGE_COIN=10000), test_exchange_flow 改走 grant_coins 直接供款。
|
||||
# 注意: 不再 = 兑换下限(下限已降到 MIN_EXCHANGE_COIN=COIN_PER_CENT=100), test_exchange_flow 改走 grant_coins 直接供款。
|
||||
TASK_REWARDS: dict[str, int] = {
|
||||
TASK_ENABLE_NOTIFICATION: 1000,
|
||||
}
|
||||
|
||||
@@ -24,10 +24,6 @@ from app.core.config import settings
|
||||
|
||||
_API_HOST = "https://api.mch.weixin.qq.com"
|
||||
_TRANSFER_PATH = "/v3/fund-app/mch-transfer/transfer-bills"
|
||||
# 免确认收款授权(用户授权免确认模式)
|
||||
_AUTH_PATH = "/v3/fund-app/mch-transfer/user-confirm-authorization"
|
||||
_PRE_TRANSFER_AUTH_PATH = "/v3/fund-app/mch-transfer/transfer-bills/pre-transfer-with-authorization"
|
||||
_TRANSFER_WITH_AUTH_PATH = "/v3/fund-app/mch-transfer/transfer-bills/transfer"
|
||||
|
||||
# 懒加载缓存
|
||||
_private_key: RSAPrivateKey | None = None
|
||||
@@ -203,164 +199,3 @@ def code_to_userinfo(code: str) -> dict:
|
||||
pass
|
||||
|
||||
return {"openid": openid, "nickname": nickname, "avatar_url": avatar_url, "raw": raw}
|
||||
|
||||
|
||||
# ===== 免确认收款授权(用户授权免确认模式)=====
|
||||
# 用户授权一次后,后续转账走 transfer_with_authorization 免逐笔确认直接到账。
|
||||
# 复用上面同一套 V3 签名(_build_authorization)、敏感字段加密(encrypt_sensitive)、懒加载密钥。
|
||||
|
||||
|
||||
def _transfer_report_infos() -> list[dict]:
|
||||
"""现金营销(scene 1000)转账场景报备信息,与 create_transfer 同口径。"""
|
||||
return [
|
||||
{"info_type": "活动名称", "info_content": "比价返现"},
|
||||
{"info_type": "奖励说明", "info_content": "现金提现到微信零钱"},
|
||||
]
|
||||
|
||||
|
||||
def apply_transfer_authorization(
|
||||
out_authorization_no: str,
|
||||
openid: str,
|
||||
user_display_name: str,
|
||||
notify_url: str,
|
||||
*,
|
||||
scene_info: dict | None = None,
|
||||
) -> dict:
|
||||
"""发起免确认收款授权(方式二:不转账,仅申请授权)。返回 {status_code, data}。
|
||||
成功(200 + state=WAIT_USER_CONFIRM)时 data 带 package_info,供 App 拉起微信授权页。"""
|
||||
body: dict = {
|
||||
"out_authorization_no": out_authorization_no,
|
||||
"appid": settings.WECHAT_APP_ID,
|
||||
"openid": openid,
|
||||
"transfer_scene_id": settings.WXPAY_TRANSFER_SCENE_ID,
|
||||
"user_display_name": user_display_name,
|
||||
"authorization_notify_url": notify_url,
|
||||
}
|
||||
if scene_info:
|
||||
body["scene_info"] = scene_info
|
||||
body_str = json.dumps(body, ensure_ascii=False)
|
||||
headers = {
|
||||
"Authorization": _build_authorization("POST", _AUTH_PATH, body_str),
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
with httpx.Client() as client:
|
||||
resp = client.post(
|
||||
f"{_API_HOST}{_AUTH_PATH}",
|
||||
content=body_str,
|
||||
headers=headers,
|
||||
timeout=settings.WXPAY_REQUEST_TIMEOUT_SEC,
|
||||
)
|
||||
return {"status_code": resp.status_code, "data": resp.json()}
|
||||
|
||||
|
||||
def query_transfer_authorization(out_authorization_no: str) -> dict:
|
||||
"""按商户授权单号查授权结果。返回 {status_code, data}。
|
||||
data.state: WAIT_USER_CONFIRM / TAKING_EFFECT(已生效) / CLOSED;TAKING_EFFECT 时带 authorization_id。"""
|
||||
path = f"{_AUTH_PATH}/out-authorization-no/{out_authorization_no}"
|
||||
headers = {
|
||||
"Authorization": _build_authorization("GET", path, ""),
|
||||
"Accept": "application/json",
|
||||
}
|
||||
with httpx.Client() as client:
|
||||
resp = client.get(
|
||||
f"{_API_HOST}{path}", headers=headers, timeout=settings.WXPAY_REQUEST_TIMEOUT_SEC
|
||||
)
|
||||
return {"status_code": resp.status_code, "data": resp.json()}
|
||||
|
||||
|
||||
def close_transfer_authorization(out_authorization_no: str) -> dict:
|
||||
"""解除免确认收款授权。返回 {status_code, data}(成功 data.state=CLOSED)。"""
|
||||
path = f"{_AUTH_PATH}/out-authorization-no/{out_authorization_no}/close"
|
||||
headers = {
|
||||
"Authorization": _build_authorization("POST", path, ""),
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
with httpx.Client() as client:
|
||||
resp = client.post(
|
||||
f"{_API_HOST}{path}", headers=headers, timeout=settings.WXPAY_REQUEST_TIMEOUT_SEC
|
||||
)
|
||||
return {"status_code": resp.status_code, "data": resp.json()}
|
||||
|
||||
|
||||
def pre_transfer_with_authorization(
|
||||
openid: str,
|
||||
amount_fen: int,
|
||||
out_bill_no: str,
|
||||
out_authorization_no: str,
|
||||
user_display_name: str,
|
||||
notify_url: str,
|
||||
user_name: str | None = None,
|
||||
) -> dict:
|
||||
"""方式一:发起转账并同时申请免确认收款授权(用户在确认这笔收款时一并完成授权)。
|
||||
返回 {status_code, data};成功 state=WAIT_USER_CONFIRM 时带 package_info(拉确认+授权页)。"""
|
||||
body: dict = {
|
||||
"appid": settings.WECHAT_APP_ID,
|
||||
"out_bill_no": out_bill_no,
|
||||
"transfer_scene_id": settings.WXPAY_TRANSFER_SCENE_ID,
|
||||
"openid": openid,
|
||||
"transfer_amount": amount_fen,
|
||||
"transfer_remark": "金币提现",
|
||||
"transfer_scene_report_infos": _transfer_report_infos(),
|
||||
"authorization_info": {
|
||||
"user_display_name": user_display_name,
|
||||
"out_authorization_no": out_authorization_no,
|
||||
"authorization_notify_url": notify_url,
|
||||
},
|
||||
}
|
||||
if user_name and amount_fen >= 30:
|
||||
body["user_name"] = encrypt_sensitive(user_name)
|
||||
|
||||
body_str = json.dumps(body, ensure_ascii=False)
|
||||
headers = {
|
||||
"Authorization": _build_authorization("POST", _PRE_TRANSFER_AUTH_PATH, body_str),
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"Wechatpay-Serial": settings.WXPAY_PUBLIC_KEY_ID,
|
||||
}
|
||||
with httpx.Client() as client:
|
||||
resp = client.post(
|
||||
f"{_API_HOST}{_PRE_TRANSFER_AUTH_PATH}",
|
||||
content=body_str,
|
||||
headers=headers,
|
||||
timeout=settings.WXPAY_REQUEST_TIMEOUT_SEC,
|
||||
)
|
||||
return {"status_code": resp.status_code, "data": resp.json()}
|
||||
|
||||
|
||||
def transfer_with_authorization(
|
||||
authorization_id: str,
|
||||
amount_fen: int,
|
||||
out_bill_no: str,
|
||||
user_name: str | None = None,
|
||||
) -> dict:
|
||||
"""方式二 / 二次起:用户已授权后免确认转账(无需用户逐笔确认,直接到账)。返回 {status_code, data}。
|
||||
成功 state ∈ ACCEPTED/PROCESSING/TRANSFERING/SUCCESS(无 WAIT_USER_CONFIRM、无 package_info)。"""
|
||||
body: dict = {
|
||||
"appid": settings.WECHAT_APP_ID,
|
||||
"out_bill_no": out_bill_no,
|
||||
"transfer_scene_id": settings.WXPAY_TRANSFER_SCENE_ID,
|
||||
"transfer_amount": amount_fen,
|
||||
"transfer_remark": "金币提现",
|
||||
"transfer_scene_report_infos": _transfer_report_infos(),
|
||||
"authorization_id": authorization_id,
|
||||
}
|
||||
if user_name and amount_fen >= 30:
|
||||
body["user_name"] = encrypt_sensitive(user_name)
|
||||
|
||||
body_str = json.dumps(body, ensure_ascii=False)
|
||||
headers = {
|
||||
"Authorization": _build_authorization("POST", _TRANSFER_WITH_AUTH_PATH, body_str),
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json",
|
||||
"Wechatpay-Serial": settings.WXPAY_PUBLIC_KEY_ID,
|
||||
}
|
||||
with httpx.Client() as client:
|
||||
resp = client.post(
|
||||
f"{_API_HOST}{_TRANSFER_WITH_AUTH_PATH}",
|
||||
content=body_str,
|
||||
headers=headers,
|
||||
timeout=settings.WXPAY_REQUEST_TIMEOUT_SEC,
|
||||
)
|
||||
return {"status_code": resp.status_code, "data": resp.json()}
|
||||
|
||||
@@ -29,7 +29,6 @@ from app.api.v1.signin import router as signin_router
|
||||
from app.api.v1.tasks import router as tasks_router
|
||||
from app.api.v1.user import router as user_router
|
||||
from app.api.v1.wallet import router as wallet_router
|
||||
from app.api.v1.wxpay import router as wxpay_router
|
||||
from app.core.config import settings
|
||||
from app.core.logging import setup_logging
|
||||
|
||||
@@ -83,7 +82,6 @@ app.include_router(compare_record_router)
|
||||
app.include_router(compare_milestone_router)
|
||||
app.include_router(meituan_router)
|
||||
app.include_router(wallet_router)
|
||||
app.include_router(wxpay_router)
|
||||
app.include_router(signin_router)
|
||||
app.include_router(tasks_router)
|
||||
app.include_router(savings_router)
|
||||
|
||||
@@ -86,7 +86,7 @@ class ComparisonRecord(Base):
|
||||
# ===== 明细(JSON,越详细越好)=====
|
||||
# 下单菜品 [{name, qty, specs?}]
|
||||
items: Mapped[list] = mapped_column(_JSON, nullable=False, default=list)
|
||||
# 逐平台对比 [{platform_id, platform_name, package, price, is_source, rank, coupon_saved}](price/coupon_saved 单位:元,原样存)
|
||||
# 逐平台对比 [{platform_id, platform_name, package, price, is_source, rank, coupon_saved, coupon_name}](price/coupon_saved 单位:元,原样存;coupon_name=优惠来源名)
|
||||
comparison_results: Mapped[list] = mapped_column(_JSON, nullable=False, default=list)
|
||||
# 目标平台未找到、跳过的菜名
|
||||
skipped_dish_names: Mapped[list] = mapped_column(_JSON, nullable=False, default=list)
|
||||
|
||||
@@ -107,43 +107,6 @@ class WithdrawOrder(Base):
|
||||
return f"<WithdrawOrder id={self.id} user_id={self.user_id} cents={self.amount_cents} {self.status}>"
|
||||
|
||||
|
||||
class WechatTransferAuthorization(Base):
|
||||
"""微信商家转账「免确认收款授权」(用户授权免确认模式)。一个用户一条(user_id 主键)。
|
||||
|
||||
状态机:
|
||||
(无) →(申请 / 首单转账顺带申请)→ pending(微信 WAIT_USER_CONFIRM,待用户在微信确认授权)
|
||||
pending →(用户确认授权)→ active(微信 TAKING_EFFECT,此后转账免用户逐笔确认)
|
||||
pending / active →(用户在微信关 / 商户解除 / 风控)→ closed(终态,需重新开启)
|
||||
out_authorization_no 我方生成(查授权 / 发起授权用,一个用户一条稳定值,重开时换新);
|
||||
authorization_id 微信在 active 后返回,免确认转账(transfer_with_authorization)时必传。
|
||||
"""
|
||||
|
||||
__tablename__ = "wechat_transfer_authorization"
|
||||
|
||||
user_id: Mapped[int] = mapped_column(
|
||||
Integer, ForeignKey("user.id"), primary_key=True
|
||||
)
|
||||
openid: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
# 商户侧授权单号(我方生成),唯一
|
||||
out_authorization_no: Mapped[str] = mapped_column(
|
||||
String(64), unique=True, index=True, nullable=False
|
||||
)
|
||||
# 微信侧授权单号(active 后返回,免确认转账要用)
|
||||
authorization_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
# pending(待用户确认) / active(已生效可免确认) / closed(已关闭需重开)
|
||||
state: Mapped[str] = mapped_column(String(16), nullable=False, default="pending")
|
||||
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), nullable=False
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
|
||||
)
|
||||
|
||||
def __repr__(self) -> str: # pragma: no cover
|
||||
return f"<WechatTransferAuthorization user_id={self.user_id} state={self.state}>"
|
||||
|
||||
|
||||
class CashTransaction(Base):
|
||||
"""现金流水(单位:分)。金币兑现金、提现都记在这里。"""
|
||||
|
||||
|
||||
@@ -52,6 +52,7 @@ class SavingsBattle:
|
||||
week_saved_cents: int # 本周(周一起)已省
|
||||
beat_percent: int # 超过百分之多少用户
|
||||
streak_days: int # 连续省钱天数
|
||||
compare_count: int # 累计完成比价次数(= 有效记录数:真实用 compare 记录、否则 demo 兜底)
|
||||
|
||||
|
||||
def _local_date(dt: datetime):
|
||||
@@ -163,7 +164,10 @@ def get_battle(db: Session, user_id: int) -> SavingsBattle:
|
||||
beat_percent = _compute_beat_percent(db, user_id)
|
||||
|
||||
return SavingsBattle(
|
||||
week_saved_cents=week_saved, beat_percent=beat_percent, streak_days=streak
|
||||
week_saved_cents=week_saved,
|
||||
beat_percent=beat_percent,
|
||||
streak_days=streak,
|
||||
compare_count=len(records),
|
||||
)
|
||||
|
||||
|
||||
|
||||
+20
-217
@@ -7,7 +7,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import unicodedata
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
@@ -15,25 +14,15 @@ from sqlalchemy import select, update
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core import rewards
|
||||
from app.core.config import settings
|
||||
from app.core.rewards import COIN_PER_CENT, coins_to_cents
|
||||
from app.integrations import wxpay
|
||||
from app.models.user import User
|
||||
from app.models.wallet import (
|
||||
CashTransaction,
|
||||
CoinAccount,
|
||||
CoinTransaction,
|
||||
WechatTransferAuthorization,
|
||||
WithdrawOrder,
|
||||
)
|
||||
from app.models.wallet import CashTransaction, CoinAccount, CoinTransaction, WithdrawOrder
|
||||
|
||||
# 微信转账终态:成功 / 失败(失败/取消/关闭都退款)
|
||||
_WX_STATE_SUCCESS = "SUCCESS"
|
||||
_WX_STATE_FAILED = {"FAIL", "CANCELLED", "CLOSED"}
|
||||
_WX_STATE_WAIT_CONFIRM = "WAIT_USER_CONFIRM" # 用户还没在微信确认页确认
|
||||
# 免确认收款授权状态
|
||||
_WX_AUTH_ACTIVE = "TAKING_EFFECT" # 已生效,可免确认转账
|
||||
_WX_AUTH_CLOSED = "CLOSED" # 已关闭(用户/商户/风控),需重新开启
|
||||
|
||||
|
||||
class InvalidExchangeAmountError(Exception):
|
||||
@@ -416,175 +405,13 @@ def create_withdraw(
|
||||
return order # 待管理员审核;**不在此处打款**
|
||||
|
||||
|
||||
# ===== 免确认收款授权(用户授权免确认模式)=====
|
||||
# 用户授权一次后,后续提现走 transfer_with_authorization 免逐笔确认直接到账。
|
||||
# WechatTransferAuthorization 一个用户一条;out_authorization_no 我方生成,authorization_id 微信生效后返回。
|
||||
|
||||
|
||||
def _auth_display_name(user: User | None) -> str:
|
||||
"""微信授权页展示的"开通账号"昵称(≤32,utf8)。
|
||||
|
||||
微信对 user_display_name 校验极严:**连空格和标点(. 等)都算"控制字符"拒收**——实测
|
||||
'wonderable ai'(带空格)被 400 拒、'wonderableai'/'周周'/'傻瓜比价用户8888' 才过。
|
||||
故只保留 文字(Unicode L*,中英文/CJK)与数字(N*),其余(emoji/符号/空格/标点/控制符/
|
||||
零宽连接符/变体选择符/星形面字符)一律剔除;清空则兜底手机号尾号。
|
||||
"""
|
||||
raw = ((user.nickname if user else None) or (user.wechat_nickname if user else None) or "").strip()
|
||||
kept = [
|
||||
ch for ch in raw
|
||||
if ord(ch) <= 0xFFFF and unicodedata.category(ch)[0] in ("L", "N")
|
||||
]
|
||||
name = "".join(kept).strip()
|
||||
if not name and user and user.phone:
|
||||
tail = user.phone[-4:] if len(user.phone) >= 4 else user.phone
|
||||
name = f"傻瓜比价用户{tail}"
|
||||
return (name or "傻瓜比价用户")[:32]
|
||||
|
||||
|
||||
def _refresh_active_auth(db: Session, user_id: int) -> None:
|
||||
"""免确认转账失败后回查授权有效性(权威判定,不靠猜错误码):
|
||||
微信侧明确非生效(用户在微信关闭 / 风控 / 无此单)→ 标 closed,下次提现自动回退方式一重新授权;
|
||||
仍生效(失败实为商户余额不足等与授权无关的原因)→ 保持 active,下次免确认重试。
|
||||
仅对本地 active 记录查询;查询本身失败(5xx/网络)不改状态,下次再核,避免误关有效授权。"""
|
||||
auth = db.get(WechatTransferAuthorization, user_id)
|
||||
if auth is None or auth.state != "active" or not settings.wxpay_configured:
|
||||
return
|
||||
try:
|
||||
res = wxpay.query_transfer_authorization(auth.out_authorization_no)
|
||||
except Exception: # noqa: BLE001 — 查询失败保持 active,下次再核
|
||||
return
|
||||
if res["status_code"] == 200:
|
||||
if res["data"].get("state") != _WX_AUTH_ACTIVE: # CLOSED / 其他非生效 → 失效
|
||||
auth.state = "closed"
|
||||
db.commit()
|
||||
elif _wx_not_found(res): # 明确无此授权单 → 失效
|
||||
auth.state = "closed"
|
||||
db.commit()
|
||||
# 其他查询错误(5xx 等):不改状态,保持 active,下次再核
|
||||
|
||||
|
||||
def sync_transfer_auth(db: Session, user_id: int) -> WechatTransferAuthorization | None:
|
||||
"""同步待确认授权最新状态(仅 pending 时查微信):
|
||||
TAKING_EFFECT → active + 落 authorization_id;CLOSED/超期无此单 → closed。返回最新记录(或 None)。
|
||||
非 pending(active/closed)直接返回不查;查询异常或缺凭证则保持原状。"""
|
||||
auth = db.get(WechatTransferAuthorization, user_id)
|
||||
if auth is None or auth.state != "pending" or not settings.wxpay_configured:
|
||||
return auth
|
||||
try:
|
||||
res = wxpay.query_transfer_authorization(auth.out_authorization_no)
|
||||
except Exception: # noqa: BLE001 — 查询失败保持 pending,下次再同步
|
||||
return auth
|
||||
if res["status_code"] != 200:
|
||||
if _wx_not_found(res): # 超期未确认被关闭 → closed;其他错误保持 pending
|
||||
auth.state = "closed"
|
||||
db.commit()
|
||||
return auth
|
||||
state = res["data"].get("state", "")
|
||||
if state == _WX_AUTH_ACTIVE:
|
||||
auth.state = "active"
|
||||
auth.authorization_id = res["data"].get("authorization_id") or auth.authorization_id
|
||||
db.commit()
|
||||
elif state == _WX_AUTH_CLOSED:
|
||||
auth.state = "closed"
|
||||
db.commit()
|
||||
# WAIT_USER_CONFIRM:保持 pending,等用户确认
|
||||
return auth
|
||||
|
||||
|
||||
def _ensure_pending_auth_no(db: Session, user_id: int, openid: str) -> str:
|
||||
"""为"方式一(转账顺带授权)"准备待确认授权单号:
|
||||
已有 pending → 复用其 out_authorization_no(避免重复申请刷满"同场景≤5"上限);
|
||||
无 / 已 closed → 生成新单号并 upsert 成 pending。返回 out_authorization_no。"""
|
||||
auth = db.get(WechatTransferAuthorization, user_id)
|
||||
if auth is not None and auth.state == "pending":
|
||||
if auth.openid != openid: # 换绑过 → 刷新快照
|
||||
auth.openid = openid
|
||||
db.commit()
|
||||
return auth.out_authorization_no
|
||||
new_no = uuid.uuid4().hex # 32 位,符合 [0-9A-Za-z_-]{8,32}
|
||||
if auth is None:
|
||||
db.add(
|
||||
WechatTransferAuthorization(
|
||||
user_id=user_id, openid=openid, out_authorization_no=new_no,
|
||||
authorization_id=None, state="pending",
|
||||
)
|
||||
)
|
||||
else: # closed → 重新开启,换新单号
|
||||
auth.openid = openid
|
||||
auth.out_authorization_no = new_no
|
||||
auth.authorization_id = None
|
||||
auth.state = "pending"
|
||||
db.commit()
|
||||
return new_no
|
||||
|
||||
|
||||
def apply_transfer_auth(db: Session, user_id: int) -> dict:
|
||||
"""方式二:显式开启免确认收款(申请授权,不转账)。
|
||||
返回 {already_active, package_info, mch_id, app_id};already_active=True 表示已开启无需再授权。
|
||||
未绑微信抛 WechatNotBoundError;微信返回非 200 抛 WithdrawTransferError。"""
|
||||
user = db.get(User, user_id)
|
||||
openid = user.wechat_openid if user else None
|
||||
if not openid:
|
||||
raise WechatNotBoundError
|
||||
|
||||
auth = db.get(WechatTransferAuthorization, user_id)
|
||||
if auth is not None and auth.state == "active" and auth.authorization_id:
|
||||
return {
|
||||
"already_active": True, "package_info": None,
|
||||
"mch_id": settings.WXPAY_MCH_ID, "app_id": settings.WECHAT_APP_ID,
|
||||
}
|
||||
|
||||
out_auth_no = _ensure_pending_auth_no(db, user_id, openid)
|
||||
result = wxpay.apply_transfer_authorization(
|
||||
out_auth_no, openid, _auth_display_name(user), settings.WXPAY_AUTH_NOTIFY_URL
|
||||
)
|
||||
if result["status_code"] != 200:
|
||||
raise WithdrawTransferError(str(result["data"].get("message") or result["data"]))
|
||||
return {
|
||||
"already_active": False,
|
||||
"package_info": result["data"].get("package_info"),
|
||||
"mch_id": settings.WXPAY_MCH_ID,
|
||||
"app_id": settings.WECHAT_APP_ID,
|
||||
}
|
||||
|
||||
|
||||
def close_transfer_auth(db: Session, user_id: int) -> None:
|
||||
"""关闭免确认收款(解除授权)。best-effort 调微信解除 + 本地置 closed。"""
|
||||
auth = db.get(WechatTransferAuthorization, user_id)
|
||||
if auth is None:
|
||||
return
|
||||
if auth.state != "closed" and settings.wxpay_configured:
|
||||
try:
|
||||
wxpay.close_transfer_authorization(auth.out_authorization_no)
|
||||
except Exception: # noqa: BLE001 — 微信解除失败不阻塞本地置 closed(用户也可在微信侧自行关闭)
|
||||
pass
|
||||
auth.state = "closed"
|
||||
db.commit()
|
||||
|
||||
|
||||
def _apply_transfer_result(db: Session, order: WithdrawOrder, data: dict) -> WithdrawOrder:
|
||||
"""落微信转账应答到提现单:记 state/转账单号/package_info,SUCCESS 即 success。"""
|
||||
order.wechat_state = data.get("state")
|
||||
order.transfer_bill_no = data.get("transfer_bill_no")
|
||||
order.package_info = data.get("package_info") # 免确认转账无此字段(None);确认模式带它供拉确认页
|
||||
if data.get("state") == _WX_STATE_SUCCESS:
|
||||
order.status = "success"
|
||||
db.commit()
|
||||
db.refresh(order)
|
||||
return order
|
||||
|
||||
|
||||
def execute_withdraw_transfer(db: Session, order: WithdrawOrder) -> WithdrawOrder:
|
||||
"""审核通过后真正发起微信转账(复用原"转账 + 模糊失败查单"逻辑,绝不盲目退款)。**不抛异常**。
|
||||
"""审核通过后真正发起微信转账(复用原"转账 + 模糊失败查单"逻辑,绝不盲目退款)。
|
||||
|
||||
免确认收款(用户授权免确认模式)分叉:
|
||||
① 已有生效授权(active)→ transfer_with_authorization 免确认转账,直接到账,无 package_info。
|
||||
② 无生效授权:
|
||||
- 已配 WXPAY_AUTH_NOTIFY_URL → pre_transfer_with_authorization(方式一):转账 + 顺带申请授权,
|
||||
返回 WAIT_USER_CONFIRM + package_info,用户在确认这笔收款时一并授权,下次起免确认。
|
||||
- 未配回调地址(未启用免确认)→ 退化为原 create_transfer 确认模式,行为同改造前。
|
||||
免确认转账失败 → 先 _settle_after_ambiguous 保金额安全,再 _refresh_active_auth 回查授权,失效则标 closed(下次回退方式一)。
|
||||
用户审核期间解绑微信 → 退款 failed。结果不明(超时/非200)→ 先查单再决定,绝不盲目退款。
|
||||
流程:reviewing → 置 pending → 调微信转账 → SUCCESS=success / 失败/取消=退款 failed /
|
||||
结果不明=先查单再决定。**不抛异常**(admin 调用方按 order.status 判断结果)。
|
||||
用户在审核期间解绑微信 → 退款 failed(打不了款)。
|
||||
可能返回 package_info(WAIT_USER_CONFIRM 场景:需用户在 App 端确认页确认后才真正到账)。
|
||||
"""
|
||||
user = db.get(User, order.user_id)
|
||||
openid = user.wechat_openid if user else None
|
||||
@@ -593,57 +420,33 @@ def execute_withdraw_transfer(db: Session, order: WithdrawOrder) -> WithdrawOrde
|
||||
db.refresh(order)
|
||||
return order
|
||||
|
||||
# 先同步待确认授权:捕获首单确认后微信已生效的 authorization_id(pending→active),或失效(→closed)
|
||||
auth = sync_transfer_auth(db, order.user_id)
|
||||
|
||||
order.status = "pending" # 进入打款在途(转账调用前崩溃留 pending,交对账兜底)
|
||||
db.commit()
|
||||
|
||||
# ① 有生效授权 → 免确认转账(无需用户确认,直接到账)
|
||||
if auth is not None and auth.state == "active" and auth.authorization_id:
|
||||
try:
|
||||
result = wxpay.transfer_with_authorization(
|
||||
auth.authorization_id, order.amount_cents, order.out_bill_no, user_name=order.user_name
|
||||
)
|
||||
except Exception as e: # noqa: BLE001 — 超时/网络异常≠失败,查单确认
|
||||
_settle_after_ambiguous(db, order, reason=f"免确认转账调用异常: {e}")
|
||||
db.refresh(order)
|
||||
return order
|
||||
if result["status_code"] != 200:
|
||||
# 金额安全:查转账单后定夺,绝不盲退(未创建→退款,已创建→按真实状态)
|
||||
_settle_after_ambiguous(db, order, reason=str(result["data"].get("message") or result["data"]))
|
||||
# 授权有效性:回查授权单,微信侧已失效(用户关闭/风控)→标 closed,下次提现自动回退方式一重新授权
|
||||
_refresh_active_auth(db, order.user_id)
|
||||
db.refresh(order)
|
||||
return order
|
||||
return _apply_transfer_result(db, order, result["data"])
|
||||
|
||||
# ② 无生效授权 → 启用免确认则转账+顺带授权(方式一),否则退化为原确认模式
|
||||
try:
|
||||
if settings.WXPAY_AUTH_NOTIFY_URL:
|
||||
out_auth_no = _ensure_pending_auth_no(db, order.user_id, openid)
|
||||
result = wxpay.pre_transfer_with_authorization(
|
||||
openid, order.amount_cents, order.out_bill_no,
|
||||
out_authorization_no=out_auth_no,
|
||||
user_display_name=_auth_display_name(user),
|
||||
notify_url=settings.WXPAY_AUTH_NOTIFY_URL,
|
||||
user_name=order.user_name,
|
||||
)
|
||||
else:
|
||||
result = wxpay.create_transfer(
|
||||
openid, order.amount_cents, order.out_bill_no, user_name=order.user_name
|
||||
)
|
||||
result = wxpay.create_transfer(
|
||||
openid, order.amount_cents, order.out_bill_no, user_name=order.user_name
|
||||
)
|
||||
except Exception as e: # noqa: BLE001 — 超时/网络异常≠失败,查单确认
|
||||
_settle_after_ambiguous(db, order, reason=f"转账调用异常: {e}")
|
||||
db.refresh(order)
|
||||
return order
|
||||
|
||||
if result["status_code"] != 200:
|
||||
_settle_after_ambiguous(db, order, reason=str(result["data"].get("message") or result["data"]))
|
||||
msg = str(result["data"].get("message") or result["data"])
|
||||
_settle_after_ambiguous(db, order, reason=msg)
|
||||
db.refresh(order)
|
||||
return order
|
||||
|
||||
return _apply_transfer_result(db, order, result["data"])
|
||||
data = result["data"]
|
||||
order.wechat_state = data.get("state")
|
||||
order.transfer_bill_no = data.get("transfer_bill_no")
|
||||
order.package_info = data.get("package_info")
|
||||
if data.get("state") == _WX_STATE_SUCCESS:
|
||||
order.status = "success"
|
||||
db.commit()
|
||||
db.refresh(order)
|
||||
return order
|
||||
|
||||
|
||||
def _get_withdraw_or_raise(db: Session, out_bill_no: str) -> WithdrawOrder:
|
||||
|
||||
@@ -37,6 +37,9 @@ class ComparisonResultIn(BaseModel):
|
||||
# 必须显式声明: 落库走 model_dump(), pydantic 默认丢未知字段, 不声明这行会被悄悄吞掉。
|
||||
# 各平台抠到红包即带值(2026-06 起源平台 Phase1 意图识别也抠, 当前仅淘宝源)。见 pricebot 侧 比价红包额留痕-实现方案.md。
|
||||
coupon_saved: float | None = None
|
||||
# 优惠**来源名**(展示用, best-effort): 美团"外卖大额神券"/京东"百亿补贴"/淘宝"平台红包"。
|
||||
# None=没抠到 → 前端走通用"红包"。同样必须显式声明否则上报边界被 pydantic 静默丢弃(pricebot#38 引入)。
|
||||
coupon_name: str | None = None
|
||||
|
||||
|
||||
class ComparisonRecordIn(BaseModel):
|
||||
|
||||
+1
-24
@@ -80,23 +80,6 @@ class WithdrawInfoOut(BaseModel):
|
||||
wechat_bound: bool = Field(..., description="当前用户是否已绑定微信")
|
||||
wechat_nickname: str | None = Field(None, description="微信昵称(可能为空/脱敏)")
|
||||
wechat_avatar_url: str | None = Field(None, description="微信头像 URL(可能为空)")
|
||||
transfer_auth_enabled: bool = Field(
|
||||
False, description="是否已开启免确认到账(开启后提现免跳微信确认,直接到账)"
|
||||
)
|
||||
|
||||
|
||||
# ===== 免确认收款授权(用户授权免确认模式)=====
|
||||
|
||||
class TransferAuthResultOut(BaseModel):
|
||||
already_active: bool = Field(False, description="是否已是开启状态(无需再授权)")
|
||||
package_info: str | None = Field(None, description="拉起微信授权页的 package(已开启时为空)")
|
||||
mch_id: str | None = None
|
||||
app_id: str | None = None
|
||||
|
||||
|
||||
class TransferAuthStatusOut(BaseModel):
|
||||
state: str = Field(..., description="none(未开启)/pending(待确认)/active(已开启)/closed(已关闭)")
|
||||
enabled: bool = Field(..., description="是否已开启免确认到账")
|
||||
|
||||
|
||||
class BindWechatRequest(BaseModel):
|
||||
@@ -119,13 +102,6 @@ class WithdrawRequest(BaseModel):
|
||||
out_bill_no: str | None = Field(
|
||||
None, description="客户端幂等键(商户单号):同号重试不重复转账。不传则服务端生成"
|
||||
)
|
||||
skip_review: bool = Field(
|
||||
False,
|
||||
description=(
|
||||
"调试直发:跳过人工审核立即打款。仅非生产(APP_ENV!=prod)生效,"
|
||||
"客户端仅 debug 包下发(开发设置开关)。生产恒走人工审核。"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class WithdrawResultOut(BaseModel):
|
||||
@@ -223,6 +199,7 @@ class SavingsBattleOut(BaseModel):
|
||||
week_saved_cents: int = Field(..., description="本周已省(分)")
|
||||
beat_percent: int = Field(..., description="超过百分之多少用户")
|
||||
streak_days: int = Field(..., description="连续省钱天数")
|
||||
compare_count: int = Field(..., description="累计完成比价次数(= 比价上报记录数)")
|
||||
|
||||
|
||||
class SavingsRecordOut(BaseModel):
|
||||
|
||||
+21
-1
@@ -3,7 +3,7 @@
|
||||
> Base URL:生产 `https://app-api.shaguabijia.com`;本地联调 `http://<开发机>:8770`
|
||||
> 协议:HTTP / JSON,请求与响应体均 `application/json`,字段统一 **snake_case**
|
||||
> 鉴权:需鉴权的接口在请求头带 `Authorization: Bearer <access_token>`
|
||||
> 最后更新:2026-05-31(+ 比价战绩里程碑 12d/12e)
|
||||
> 最后更新:2026-06-04(+ 运营后台 Admin 子应用 A1–A18,见下方「运营后台 Admin」组)
|
||||
> 架构:`app/api/v1/` 只放很轻的接口层;穿山甲/微信支付/极光/短信/美团等 SDK 集成的重逻辑在 `app/integrations/`,实现细节见 [docs/integrations/](../integrations/README.md)。
|
||||
|
||||
---
|
||||
@@ -69,6 +69,26 @@
|
||||
| **静态资源**(StaticFiles 挂载,见下方 `/media` 静态服务) |||
|
||||
| - | `GET /media/avatars/<file>` | 无 | 用户头像;返回二进制图片 |
|
||||
| - | `GET /media/feedback/<file>` | 无 | 反馈截图;返回二进制图片 |
|
||||
| **运营后台 Admin**(独立子应用 `app/admin/`,前缀 `/admin/api`,独立进程 + 独立 admin JWT。鉴权列:`admin`=任意已登录管理员,`operator`/`finance`/`super_admin`=需对应角色(`super_admin` 恒通过)) |||
|
||||
| A1 | `POST /admin/api/auth/login` | 无 | [详情](./admin-auth-login.md) |
|
||||
| A2 | `GET /admin/api/auth/me` | admin | [详情](./admin-auth-me.md) |
|
||||
| A3 | `GET /admin/api/stats/overview` | admin | [详情](./admin-stats-overview.md) |
|
||||
| A4 | `GET /admin/api/users` | admin | [详情](./admin-users-list.md) |
|
||||
| A5 | `GET /admin/api/users/{user_id}` | admin | [详情](./admin-user-detail.md) |
|
||||
| A6 | `POST /admin/api/users/{user_id}/status` | operator | [详情](./admin-user-status.md) |
|
||||
| A7 | `POST /admin/api/users/{user_id}/coins` | finance | [详情](./admin-user-coins.md) |
|
||||
| A8 | `GET /admin/api/wallet/coin-transactions` | admin | [详情](./admin-wallet-coin-transactions.md) |
|
||||
| A9 | `GET /admin/api/wallet/cash-transactions` | admin | [详情](./admin-wallet-cash-transactions.md) |
|
||||
| A10 | `GET /admin/api/withdraws` | admin | [详情](./admin-withdraws-list.md) |
|
||||
| A11 | `POST /admin/api/withdraws/reconcile` | finance | [详情](./admin-withdraw-reconcile.md) |
|
||||
| A12 | `POST /admin/api/withdraws/{out_bill_no}/refresh` | finance | [详情](./admin-withdraw-refresh.md) |
|
||||
| A13 | `GET /admin/api/feedbacks` | admin | [详情](./admin-feedbacks-list.md) |
|
||||
| A14 | `POST /admin/api/feedbacks/{feedback_id}/handle` | operator | [详情](./admin-feedback-handle.md) |
|
||||
| A15 | `GET /admin/api/admins` | super_admin | [详情](./admin-admins-list.md) |
|
||||
| A16 | `POST /admin/api/admins` | super_admin | [详情](./admin-admin-create.md) |
|
||||
| A17 | `PATCH /admin/api/admins/{admin_id}` | super_admin | [详情](./admin-admin-update.md) |
|
||||
| A18 | `GET /admin/api/audit-logs` | admin | [详情](./admin-audit-logs.md) |
|
||||
| - | `GET /admin/api/health` | 无 | admin 健康检查(无单独文档) |
|
||||
|
||||
> ⚠️ 美团三个接口当前**无鉴权**,且 `referral-link` 的 `sid` 允许客户端传值覆盖默认渠道——见各接口"备注"。
|
||||
> `coupon/step` 及外卖比价的 `intent/recognize` / `price/step` 都透传到 pricebot-backend,**MVP 阶段均不鉴权**(device_id 透传,待补 JWT——见各接口详情)。
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# POST /admin/api/admins — 创建管理员
|
||||
|
||||
> 所属:Admin·Accounts 组(前缀 `/admin/api/admins`) | 鉴权:Bearer admin_token(角色:super_admin) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
**application/json**:
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `username` | string | ✓ | — | 账号,3–64 字 |
|
||||
| `password` | string | ✓ | — | 初始密码,8–72 字(bcrypt ≤72 字节) |
|
||||
| `role` | string | ✗ | `operator` | 角色,枚举:`super_admin` / `finance` / `operator` |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`AdminOut`(新建的管理员)
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 管理员 id |
|
||||
| `username` | string | 账号 |
|
||||
| `role` | string | 角色 |
|
||||
| `status` | string | 状态(新建默认 `active`) |
|
||||
| `created_at` | datetime | 创建时间(UTC) |
|
||||
| `last_login_at` | datetime \| null | 上次登录时间(新建为 null) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带 admin token / token 无效或过期 / 管理员被禁用
|
||||
- `403` 角色不足(仅 super_admin)
|
||||
- `409` 用户名已存在
|
||||
- `422` 缺字段 / `username` 长度不在 3–64 / `password` 长度不在 8–72 / `role` 非法枚举
|
||||
|
||||
## 说明
|
||||
- 创建成功后写一条审计:`action=admin.create`、`target_type=admin`、`target_id=新管理员 id`、`detail={username, role}`。见 [admin_audit_log](../database/admin_audit_log.md)。
|
||||
- 数据表见 [admin_user](../database/admin_user.md)。
|
||||
@@ -0,0 +1,39 @@
|
||||
# PATCH /admin/api/admins/{admin_id} — 改角色/启停/重置密码
|
||||
|
||||
> 所属:Admin·Accounts 组(前缀 `/admin/api/admins`) | 鉴权:Bearer admin_token(角色:super_admin) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
**路径参数**:
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `admin_id` | int | ✓ | 目标管理员 id |
|
||||
|
||||
**application/json**(三字段都可选,只改传了的;至少传一个):
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `role` | string | ✗ | 改角色,枚举:`super_admin` / `finance` / `operator` |
|
||||
| `status` | string | ✗ | 启停,枚举:`active`(启用)/ `disabled`(禁用) |
|
||||
| `password` | string | ✗ | 重置密码,8–72 字(传则覆盖原密码) |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`AdminOut`(更新后的管理员)
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 管理员 id |
|
||||
| `username` | string | 账号 |
|
||||
| `role` | string | 角色 |
|
||||
| `status` | string | 状态 |
|
||||
| `created_at` | datetime | 创建时间(UTC) |
|
||||
| `last_login_at` | datetime \| null | 上次登录时间 |
|
||||
|
||||
## 错误码
|
||||
- `400` 不能禁用自己(`admin_id == 当前 admin.id` 且 `status=disabled`) / 无任何变更字段(三字段全空)
|
||||
- `401` 未带 admin token / token 无效或过期 / 管理员被禁用
|
||||
- `403` 角色不足(仅 super_admin)
|
||||
- `404` 管理员不存在
|
||||
- `422` `role`/`status` 非法枚举 / `password` 长度不在 8–72
|
||||
|
||||
## 说明
|
||||
- 更新成功后写一条审计:`action=admin.update`、`target_type=admin`、`target_id=admin_id`、`detail` 为本次实际变更字段(如 `{"role": "...", "status": "...", "password": "reset"}`,密码只记 `reset` 不记明文)。见 [admin_audit_log](../database/admin_audit_log.md)。
|
||||
- 数据表见 [admin_user](../database/admin_user.md)。
|
||||
@@ -0,0 +1,26 @@
|
||||
# GET /admin/api/admins — 管理员列表
|
||||
|
||||
> 所属:Admin·Accounts 组(前缀 `/admin/api/admins`) | 鉴权:Bearer admin_token(角色:super_admin) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
无(按 `id` 升序返回全部,无分页)
|
||||
|
||||
## 出参
|
||||
响应 `200`:`AdminOut[]`
|
||||
|
||||
**AdminOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 管理员 id |
|
||||
| `username` | string | 账号 |
|
||||
| `role` | string | 角色:`super_admin` / `finance` / `operator` |
|
||||
| `status` | string | 状态:`active` / `disabled` |
|
||||
| `created_at` | datetime | 创建时间(UTC) |
|
||||
| `last_login_at` | datetime \| null | 上次登录时间,从未登录为 null |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带 admin token / token 无效或过期 / 管理员被禁用
|
||||
- `403` 角色不足(仅 super_admin 可访问,detail 形如 `role 'operator' not allowed (need one of [...])`)
|
||||
|
||||
## 说明
|
||||
账号管理整组(`/admin/api/admins`)的角色守卫为 `require_role()` 无参,即仅 `super_admin` 通过。数据表见 [admin_user](../database/admin_user.md)。
|
||||
@@ -0,0 +1,35 @@
|
||||
# GET /admin/api/audit-logs — 审计日志(谁改了什么,游标分页)
|
||||
|
||||
> 所属:Admin·Audit 组(前缀 `/admin/api/audit-logs`) | 鉴权:Bearer admin_token(角色:任意已登录 admin) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参(query)
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `action` | string | ❌ | null | 按操作类型过滤,如 `admin.create` / `admin.update` |
|
||||
| `target_type` | string | ❌ | null | 按目标对象类型过滤,如 `admin` |
|
||||
| `admin_id` | int | ❌ | null | 按操作人(管理员 id)过滤 |
|
||||
| `limit` | int | ❌ | 50 | 1–100 |
|
||||
| `cursor` | int | ❌ | null | 上一页末条 `id`,首页不传 |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`{ items: AdminAuditLogOut[], next_cursor: int|null }`(按 `id` 倒序;分页见 [索引#游标分页约定](./README.md#游标分页约定))
|
||||
|
||||
**AdminAuditLogOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 审计 id(也是游标) |
|
||||
| `admin_id` | int | 操作人管理员 id |
|
||||
| `admin_username` | string | 操作人账号(写入时快照) |
|
||||
| `action` | string | 操作类型,如 `admin.create` / `admin.update` |
|
||||
| `target_type` | string | 目标对象类型,如 `admin` |
|
||||
| `target_id` | string \| null | 目标对象 id(字符串) |
|
||||
| `detail` | object \| null | 操作详情(JSON,如变更字段) |
|
||||
| `ip` | string \| null | 操作来源 IP(取 XFF 首段或直连 IP,仅记录不鉴权) |
|
||||
| `created_at` | datetime | 操作时间(UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带 admin token / token 无效或过期 / 管理员被禁用
|
||||
|
||||
## 说明
|
||||
- 整组(`/admin/api/audit-logs`)守卫为 `get_current_admin`,任意已登录 admin 均可查看,无角色限制。
|
||||
- 审计日志只增不改不删,任何写操作经 `write_audit` 落一条。数据表见 [admin_audit_log](../database/admin_audit_log.md)。
|
||||
@@ -0,0 +1,40 @@
|
||||
# POST /admin/api/auth/login — 管理员登录
|
||||
|
||||
> 所属:Admin·Auth 组(前缀 `/admin/api/auth`) | 鉴权:无 | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
**application/json**:
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `username` | string | ✓ | 管理员账号,1–64 字 |
|
||||
| `password` | string | ✓ | 密码,1–128 字 |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`AdminLoginResponse`
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `access_token` | string | admin JWT(独立 `ADMIN_JWT_SECRET`、payload `typ=admin`、默认 12h、无 refresh) |
|
||||
| `token_type` | string | 固定 `Bearer` |
|
||||
| `expires_in` | int | access_token 剩余秒数(过期需重新登录) |
|
||||
| `admin` | AdminOut | 当前管理员信息,字段见下 |
|
||||
|
||||
**AdminOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 管理员 id |
|
||||
| `username` | string | 账号 |
|
||||
| `role` | string | 角色:`super_admin` / `finance` / `operator` |
|
||||
| `status` | string | 状态:`active` / `disabled` |
|
||||
| `created_at` | datetime | 创建时间(UTC) |
|
||||
| `last_login_at` | datetime \| null | 上次登录时间,从未登录为 null |
|
||||
|
||||
## 错误码
|
||||
- `401` 用户名或密码错误(用户名不存在与密码错误统一同文案,防账号枚举)
|
||||
- `403` 账号已禁用(`status != active`)
|
||||
- `422` 缺 `username` 或 `password` / 长度超限
|
||||
- `429` 同 IP 每分钟登录超过 10 次(限流防爆破)
|
||||
|
||||
## 说明
|
||||
- 登录成功后回写 `last_login_at` 为当前时间。
|
||||
- 后续所有 admin 接口须带 `Authorization: Bearer <access_token>`;admin token 与 App 用户 token 完全隔离。
|
||||
@@ -0,0 +1,24 @@
|
||||
# GET /admin/api/auth/me — 当前管理员
|
||||
|
||||
> 所属:Admin·Auth 组(前缀 `/admin/api/auth`) | 鉴权:Bearer admin_token(角色:任意已登录 admin) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
无(身份取自 Header token)
|
||||
|
||||
## 出参
|
||||
响应 `200`:`AdminOut`
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 管理员 id |
|
||||
| `username` | string | 账号 |
|
||||
| `role` | string | 角色:`super_admin` / `finance` / `operator` |
|
||||
| `status` | string | 状态:`active` / `disabled` |
|
||||
| `created_at` | datetime | 创建时间(UTC) |
|
||||
| `last_login_at` | datetime \| null | 上次登录时间,从未登录为 null |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带 admin token / token 无效或过期 / `typ` 非 admin / 管理员不存在或被禁用(响应头带 `WWW-Authenticate: Bearer`)
|
||||
|
||||
## 说明
|
||||
无
|
||||
@@ -0,0 +1,23 @@
|
||||
# POST /admin/api/feedbacks/{feedback_id}/handle — 标记反馈已处理
|
||||
|
||||
> 所属:Admin·反馈 组(前缀 `/admin/api/feedbacks`) | 鉴权:Bearer admin_token(角色:`operator`,`super_admin` 恒通过,`require_role("operator")`) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
- 路径:`feedback_id`(int)
|
||||
- body:无
|
||||
|
||||
## 出参
|
||||
响应 `200`:`OkResponse` = `{ "ok": true }`
|
||||
|
||||
幂等说明:将该反馈 `status` 置为 `handled`(不校验原状态,重复调用结果一致)。
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(头带 `WWW-Authenticate: Bearer`)
|
||||
- `403` 角色不足(需 `operator` 或 `super_admin`)
|
||||
- `404` 反馈不存在(`detail: "反馈不存在"`)
|
||||
- `422` `feedback_id` 非合法 int
|
||||
|
||||
## 说明
|
||||
- 写操作记审计 [admin_audit_log](../database/admin_audit_log.md):`action="feedback.handle"`、`target_type="feedback"`、`target_id=<feedback_id>`、`detail={"before": <原 status>, "after": "handled"}`、`ip=<客户端 IP>`。
|
||||
- 状态变更与审计写入在同一事务(`commit=False` 后统一 `db.commit()`)。
|
||||
- 关联表 [feedback](../database/feedback.md)。
|
||||
@@ -0,0 +1,34 @@
|
||||
# GET /admin/api/feedbacks — 反馈工单列表(游标分页)
|
||||
|
||||
> 所属:Admin·反馈 组(前缀 `/admin/api/feedbacks`) | 鉴权:Bearer admin_token(角色:任意已登录管理员,无 `require_role`,仅 `get_current_admin`) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参(query)
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `status` | string | ❌ | null | 反馈状态,精确匹配:`new`(待处理) / `handled`(已处理);传空/不传则不筛 |
|
||||
| `user_id` | int | ❌ | null | 按提交用户 id 精确筛 |
|
||||
| `limit` | int | ❌ | 20 | 1–100 |
|
||||
| `cursor` | int | ❌ | null | 上一页 next_cursor(按 feedback id 倒序,查 `id < cursor`) |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`{ items: FeedbackOut[], next_cursor: int|null }`(`next_cursor=null` 表示末页)
|
||||
|
||||
**FeedbackOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 反馈 id |
|
||||
| `user_id` | int | 提交用户 id |
|
||||
| `content` | string | 反馈内容 |
|
||||
| `contact` | string | 联系方式(微信/QQ/手机,便于回访) |
|
||||
| `images` | string[] \| null | 截图相对路径列表(如 `["/media/feedback/u1_ab12.jpg"]`),无图为 null |
|
||||
| `status` | string | 反馈状态:`new`(待处理) / `handled`(已处理) |
|
||||
| `created_at` | datetime | 提交时间(UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(头带 `WWW-Authenticate: Bearer`)
|
||||
- `422` `limit` 超出 1–100 范围 / 字段类型不合法
|
||||
|
||||
## 说明
|
||||
- 游标分页约定:结果按 feedback `id` 倒序;`cursor` 传上一页返回的 `next_cursor`;`next_cursor=null` 即末页。
|
||||
- `status` / `user_id` 均为精确匹配,可叠加。
|
||||
- 关联表 [feedback](../database/feedback.md);截图为相对路径,经 `GET /media/feedback/<file>` 静态读。
|
||||
@@ -0,0 +1,69 @@
|
||||
# GET /admin/api/stats/overview — 大盘核心指标
|
||||
|
||||
> 所属:Admin·数据大盘 组(前缀 `/admin/api/stats`) | 鉴权:Bearer admin_token(角色:任意已登录管理员,无 require_role) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
无
|
||||
|
||||
## 出参
|
||||
响应 `200`:`DashboardOverview`(全局只读聚合,六大块嵌套)
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `users` | DashboardUsers | 用户指标 |
|
||||
| `coins` | DashboardCoins | 金币指标 |
|
||||
| `cash` | DashboardCash | 现金/提现指标 |
|
||||
| `comparison` | DashboardComparison | 比价指标 |
|
||||
| `feedback` | DashboardFeedback | 反馈指标 |
|
||||
| `cps` | DashboardCps | CPS 收入(P2 未接入) |
|
||||
|
||||
**DashboardUsers**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `total` | int | 用户总数 |
|
||||
| `active` | int | 状态 `active` 数 |
|
||||
| `disabled` | int | 状态 `disabled` 数 |
|
||||
| `deleted` | int | 状态 `deleted` 数 |
|
||||
| `new_today` | int | 今日新增(按北京时区切天的 `created_at`) |
|
||||
| `dau` | int | 日活(今日有 `last_login_at`,北京时区切天) |
|
||||
|
||||
**DashboardCoins**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `granted_total` | int | 累计发放金币(coin_transaction 中所有 `amount > 0` 之和;负数兑换/扣减不计) |
|
||||
|
||||
**DashboardCash**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `withdraw_success_cents` | int | 提现成功累计金额(分,`status=success` 之和) |
|
||||
| `withdraw_pending_count` | int | 提现 `pending` 单数 |
|
||||
| `withdraw_success_count` | int | 提现 `success` 单数 |
|
||||
| `withdraw_failed_count` | int | 提现 `failed` 单数 |
|
||||
|
||||
**DashboardComparison**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `total` | int | 比价记录总数 |
|
||||
| `success` | int | 比价成功数(`status=success`) |
|
||||
| `success_rate` | float | 成功率 = success/total,保留 4 位小数;total 为 0 时返回 `0.0` |
|
||||
|
||||
**DashboardFeedback**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `new` | int | 待处理反馈数(`status=new`) |
|
||||
|
||||
**DashboardCps**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `available` | bool | CPS 数据是否可用(当前固定 `false`) |
|
||||
| `note` | string | 说明文案(当前固定 `"CPS 转化数据未接入(P2)"`) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(头带 `WWW-Authenticate: Bearer`)
|
||||
|
||||
## 说明
|
||||
- 全部为全局只读聚合(count / sum / DAU / 成功率),不改任何数据。
|
||||
- `new_today` / `dau` 按**北京时区**(UTC+8)切天,其余金额/计数无时区概念。
|
||||
- 金额单位:`*_cents` 为分;金币(`granted_total`)为个数。
|
||||
- CPS 收入数据源未接入(referral-link 只换链接,转化/佣金未回收),`cps` 恒为 `{available:false, note:...}`,前端显示"待接入"。
|
||||
- 关联表:[user](../database/user.md) / [coin_transaction](../database/coin_transaction.md) / [withdraw_order](../database/withdraw_order.md) / [comparison_record](../database/comparison_record.md) / [feedback](../database/feedback.md)。
|
||||
@@ -0,0 +1,29 @@
|
||||
# POST /admin/api/users/{user_id}/coins — 手动增减金币(带审计)
|
||||
|
||||
> 所属:Admin·用户 组(前缀 `/admin/api/users`) | 鉴权:Bearer admin_token(角色:`finance`,`super_admin` 恒通过) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
- 路径:`user_id`(int)
|
||||
- **application/json**:
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `amount` | int | ✓ | 金币变动(个数):正=增加,负=扣减;不可为 0 |
|
||||
| `reason` | string | ✓ | 操作原因,1–128 字(必填,入审计与流水备注) |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`OkResponse` = `{ "ok": true }`
|
||||
|
||||
## 错误码
|
||||
- `400` `amount == 0`(`detail: "amount 不能为 0"`);或负数扣减后余额会变负(`detail: "扣减后金币为负(当前余额 N)"`)
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(头带 `WWW-Authenticate: Bearer`)
|
||||
- `403` 角色不足(需 `finance` 或 `super_admin`)
|
||||
- `404` 用户不存在(`detail: "用户不存在"`)
|
||||
- `422` 缺 `amount`/`reason`、`reason` 长度不在 1–128、字段类型不合法 / `user_id` 非整数
|
||||
|
||||
## 说明
|
||||
- 金币 = 个数(非现金);本接口只动金币余额,不涉及现金(`*_cents`)。
|
||||
- 扣减保护:`amount < 0` 时若扣减后金币余额 < 0 直接拒绝(运营误操作保护)。
|
||||
- 金币变动写流水 [coin_transaction](../database/coin_transaction.md):`biz_type` 增加为 `admin_grant`、扣减为 `admin_deduct`,`remark = admin:<reason>`(截断至 128 字)。
|
||||
- 写操作记审计 [admin_audit_log](../database/admin_audit_log.md):`action = user.coins.grant`,`target_type = user`,`target_id = user_id`,`detail = {amount, balance_after, reason}`,并记录操作 IP。
|
||||
- 金币变动 + 审计在同一事务原子提交(改钱必留痕)。
|
||||
- 关联用户表 [user](../database/user.md);金币账户 [coin_account](../database/coin_account.md);金币流水 [coin_transaction](../database/coin_transaction.md)。
|
||||
@@ -0,0 +1,43 @@
|
||||
# GET /admin/api/users/{user_id} — 用户 360 详情
|
||||
|
||||
> 所属:Admin·用户 组(前缀 `/admin/api/users`) | 鉴权:Bearer admin_token(角色:任意已登录管理员,无 require_role) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
- 路径:`user_id`(int)
|
||||
|
||||
## 出参
|
||||
响应 `200`:`AdminUserOverview`(基础资料 + 钱包余额 + 各项 count;历史明细走各自分页接口)
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `user` | AdminUserListItem | 用户基础资料,字段见下 |
|
||||
| `coin_balance` | int | 当前金币余额(个数);无金币账户时为 0 |
|
||||
| `cash_balance_cents` | int | 当前现金余额(分);无账户时为 0 |
|
||||
| `total_coin_earned` | int | 累计获得金币(个数);无账户时为 0 |
|
||||
| `comparison_total` | int | 比价记录总数 |
|
||||
| `comparison_success` | int | 比价成功数(`status=success`) |
|
||||
| `withdraw_total` | int | 提现单总数 |
|
||||
| `withdraw_success_cents` | int | 提现成功累计金额(分,`status=success` 之和) |
|
||||
| `feedback_total` | int | 反馈总数 |
|
||||
|
||||
**AdminUserListItem**(`user` 字段)
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 用户 id |
|
||||
| `phone` | string | 手机号 |
|
||||
| `nickname` | string \| null | 昵称,可空 |
|
||||
| `register_channel` | string | 注册渠道 |
|
||||
| `status` | string | 用户状态:`active` / `disabled` / `deleted` |
|
||||
| `wechat_openid` | string \| null | 微信 openid,可空 |
|
||||
| `created_at` | datetime | 注册时间(UTC) |
|
||||
| `last_login_at` | datetime | 上次登录时间(UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(头带 `WWW-Authenticate: Bearer`)
|
||||
- `404` 用户不存在(`detail: "用户不存在"`)
|
||||
- `422` `user_id` 非整数
|
||||
|
||||
## 说明
|
||||
- 金币三项(`coin_balance` / `cash_balance_cents` / `total_coin_earned`)读 [coin_account](../database/coin_account.md);从未发生金币动作(账户不存在)时统一返回 0。
|
||||
- 各 count 为聚合数,明细历史走带 `user_id` 过滤的分页接口(金币流水 / 现金流水 / 提现 / 比价 / 反馈)。
|
||||
- 关联用户表 [user](../database/user.md);金币账户 [coin_account](../database/coin_account.md);提现单 [withdraw_order](../database/withdraw_order.md);比价记录 [comparison_record](../database/comparison_record.md);反馈 [feedback](../database/feedback.md)。
|
||||
@@ -0,0 +1,25 @@
|
||||
# POST /admin/api/users/{user_id}/status — 封禁/解封用户
|
||||
|
||||
> 所属:Admin·用户 组(前缀 `/admin/api/users`) | 鉴权:Bearer admin_token(角色:`operator`,`super_admin` 恒通过) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参
|
||||
- 路径:`user_id`(int)
|
||||
- **application/json**:
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `status` | string | ✓ | 目标状态,枚举 `active`(解封)/ `disabled`(封禁);注销 `deleted` 不走此接口 |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`OkResponse` = `{ "ok": true }`
|
||||
|
||||
## 错误码
|
||||
- `400` 已注销账号不可改状态(目标用户当前 `status == deleted`,`detail: "已注销账号不可改状态"`)
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(头带 `WWW-Authenticate: Bearer`)
|
||||
- `403` 角色不足(需 `operator` 或 `super_admin`)
|
||||
- `404` 用户不存在(`detail: "用户不存在"`)
|
||||
- `422` `status` 非 `active`/`disabled` 枚举 / 缺字段 / `user_id` 非整数
|
||||
|
||||
## 说明
|
||||
- 业务写(改用户状态)与审计写在同一事务原子提交:改了就有痕、有痕就真改了。
|
||||
- 写操作记审计 [admin_audit_log](../database/admin_audit_log.md):`action = user.status.set`,`target_type = user`,`target_id = user_id`,`detail = {before, after}`,并记录操作 IP。
|
||||
- 关联用户表 [user](../database/user.md)。
|
||||
@@ -0,0 +1,37 @@
|
||||
# GET /admin/api/users — 用户列表(筛选+分页)
|
||||
|
||||
> 所属:Admin·用户 组(前缀 `/admin/api/users`) | 鉴权:Bearer admin_token(角色:任意已登录管理员,无 require_role) | [← 返回 API 索引](./README.md)
|
||||
|
||||
## 入参(query)
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `phone` | string | ❌ | null | 手机号**前缀**匹配(`phone LIKE '<值>%'`) |
|
||||
| `register_channel` | string | ❌ | null | 注册渠道,精确匹配 |
|
||||
| `status` | string | ❌ | null | 用户状态,精确匹配:`active` / `disabled` / `deleted` |
|
||||
| `limit` | int | ❌ | 20 | 1–100 |
|
||||
| `cursor` | int | ❌ | null | 上一页 next_cursor(按 user id 倒序,查 `id < cursor`) |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`{ items: AdminUserListItem[], next_cursor: int|null }`(`next_cursor=null` 表示末页)
|
||||
|
||||
**AdminUserListItem**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 用户 id |
|
||||
| `phone` | string | 手机号 |
|
||||
| `nickname` | string \| null | 昵称,可空 |
|
||||
| `register_channel` | string | 注册渠道 |
|
||||
| `status` | string | 用户状态:`active` / `disabled` / `deleted` |
|
||||
| `wechat_openid` | string \| null | 微信 openid,可空 |
|
||||
| `created_at` | datetime | 注册时间(UTC) |
|
||||
| `last_login_at` | datetime | 上次登录时间(UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(头带 `WWW-Authenticate: Bearer`)
|
||||
- `422` `limit` 超出 1–100 范围 / 字段类型不合法
|
||||
|
||||
## 说明
|
||||
- 游标分页约定:结果按 user `id` 倒序;`cursor` 传上一页返回的 `next_cursor`;`next_cursor=null` 即末页。
|
||||
- `phone` 为前缀匹配(`LIKE '<值>%'`),`register_channel` / `status` 为精确匹配;三者可叠加。
|
||||
- 关联用户表 [user](../database/user.md)。
|
||||
- 历史明细(金币流水、提现、比价、反馈等)不在本列表,走各自带 `user_id` 过滤的分页接口。
|
||||
@@ -0,0 +1,35 @@
|
||||
# GET /admin/api/wallet/cash-transactions — 现金流水(游标分页)
|
||||
|
||||
> 所属:Admin·钱包 组(前缀 `/admin/api/wallet`) | 鉴权:Bearer admin_token(角色:任意已登录管理员,仅需 `get_current_admin`,无 `require_role`) | [← 返回 API 索引](./README.md)
|
||||
|
||||
跨用户查询全量现金流水,可按 `user_id` / `biz_type` 过滤。游标分页(`id` 倒序)。金额单位一律为**分**。
|
||||
|
||||
## 入参(query)
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `user_id` | int | ❌ | null | 按用户过滤;不传则查全量 |
|
||||
| `biz_type` | string | ❌ | null | 按业务类型过滤 |
|
||||
| `limit` | int | ❌ | 20 | 1–100 |
|
||||
| `cursor` | int | ❌ | null | 上一页 next_cursor(查 `id < cursor`) |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`{ items: CashTxnOut[], next_cursor: int|null }`(`next_cursor=null` 表示末页)
|
||||
|
||||
**CashTxnOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 流水主键 |
|
||||
| `user_id` | int | 所属用户 |
|
||||
| `amount_cents` | int | 现金变动量,单位:**分**(正/负) |
|
||||
| `balance_after_cents` | int | 本次变动后现金余额,单位:**分** |
|
||||
| `biz_type` | string | 业务类型 |
|
||||
| `ref_id` | string \| null | 关联业务 ID |
|
||||
| `remark` | string \| null | 备注 |
|
||||
| `created_at` | datetime | 创建时间(ISO 8601 UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(响应头带 `WWW-Authenticate: Bearer`)
|
||||
- `422` query 参数校验失败(如 `limit` 越界、类型错误)
|
||||
|
||||
## 说明
|
||||
关联 [cash_transaction](../database/cash_transaction.md)。所有金额字段单位为分。
|
||||
@@ -0,0 +1,35 @@
|
||||
# GET /admin/api/wallet/coin-transactions — 金币流水(游标分页)
|
||||
|
||||
> 所属:Admin·钱包 组(前缀 `/admin/api/wallet`) | 鉴权:Bearer admin_token(角色:任意已登录管理员,仅需 `get_current_admin`,无 `require_role`) | [← 返回 API 索引](./README.md)
|
||||
|
||||
跨用户查询全量金币流水,可按 `user_id` / `biz_type` 过滤。游标分页(`id` 倒序)。
|
||||
|
||||
## 入参(query)
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `user_id` | int | ❌ | null | 按用户过滤;不传则查全量 |
|
||||
| `biz_type` | string | ❌ | null | 按业务类型过滤 |
|
||||
| `limit` | int | ❌ | 20 | 1–100 |
|
||||
| `cursor` | int | ❌ | null | 上一页 next_cursor(查 `id < cursor`) |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`{ items: CoinTxnOut[], next_cursor: int|null }`(`next_cursor=null` 表示末页)
|
||||
|
||||
**CoinTxnOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 流水主键 |
|
||||
| `user_id` | int | 所属用户 |
|
||||
| `amount` | int | 金币变动量(正/负,单位:金币个数,非分) |
|
||||
| `balance_after` | int | 本次变动后金币余额 |
|
||||
| `biz_type` | string | 业务类型 |
|
||||
| `ref_id` | string \| null | 关联业务 ID |
|
||||
| `remark` | string \| null | 备注 |
|
||||
| `created_at` | datetime | 创建时间(ISO 8601 UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(响应头带 `WWW-Authenticate: Bearer`)
|
||||
- `422` query 参数校验失败(如 `limit` 越界、类型错误)
|
||||
|
||||
## 说明
|
||||
关联 [coin_transaction](../database/coin_transaction.md)。金币为个数计量,非分。
|
||||
@@ -0,0 +1,37 @@
|
||||
# POST /admin/api/withdraws/reconcile — 批量对账(扫超时 pending 单)
|
||||
|
||||
> 所属:Admin·提现 组(前缀 `/admin/api/withdraws`) | 鉴权:Bearer admin_token(角色:`finance`,`super_admin` 恒通过) | [← 返回 API 索引](./README.md)
|
||||
|
||||
扫描创建时间超过 `older_than_minutes` 分钟、仍为 `pending` 的提现单,逐单调微信查单并归一化(成功落 `success`;失败/已撤销则退款落 `failed`;查到 `WAIT_USER_CONFIRM` 视为用户放弃,撤单+退款)。用于解开"扣了款但转账没发起/没确认"的孤儿单。单笔失败不影响其余(内部 rollback 后继续,下轮再试)。
|
||||
|
||||
> 路由注册顺序上,静态路径 `/reconcile` 必须在路径参数 `/{out_bill_no}/refresh` 之前声明。
|
||||
|
||||
## 入参
|
||||
- 路径:无
|
||||
- query:
|
||||
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `older_than_minutes` | int | ❌ | 15 | 只扫创建时间早于「现在 − N 分钟」的 pending 单;`ge=0` |
|
||||
|
||||
- body:无
|
||||
|
||||
## 出参
|
||||
响应 `200`:`ReconcileResult`
|
||||
|
||||
**ReconcileResult**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `checked` | int | 本次扫到的超时 pending 单数量 |
|
||||
| `resolved` | int | 其中被归一化为终态(success/failed)的数量 |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(响应头带 `WWW-Authenticate: Bearer`)
|
||||
- `403` 角色不足(需 `finance` 或 `super_admin`)
|
||||
- `422` query 参数校验失败(如 `older_than_minutes < 0`)
|
||||
- `503` 微信支付未配置(捕获 `WxPayNotConfiguredError`,`detail="微信支付未配置"`)
|
||||
|
||||
## 说明
|
||||
- 调用底层 `app.repositories.wallet.reconcile_pending_withdraws`(内部逐单调微信查单/撤单/退款,各自 commit)。
|
||||
- 写操作记审计 [admin_audit_log](../database/admin_audit_log.md)(`action="withdraw.reconcile"`,`detail` 为 `{checked, resolved}`,含触发管理员与客户端 IP)。
|
||||
- 关联 [withdraw_order](../database/withdraw_order.md)。
|
||||
@@ -0,0 +1,39 @@
|
||||
# POST /admin/api/withdraws/{out_bill_no}/refresh — 单笔提现重试查单
|
||||
|
||||
> 所属:Admin·提现 组(前缀 `/admin/api/withdraws`) | 鉴权:Bearer admin_token(角色:`finance`,`super_admin` 恒通过) | [← 返回 API 索引](./README.md)
|
||||
|
||||
对单笔提现单调微信查单并归一化:`SUCCESS`→`success`;`FAIL`/`CANCELLED`/`CLOSED`→退款+`failed`;查到 `WAIT_USER_CONFIRM` 视为用户放弃(`cancel_if_unconfirmed=True`),撤单+退款;`ACCEPTED`/`PROCESSING` 等仍在途则保持 `pending`。已是终态的单直接返回、不再查。
|
||||
|
||||
## 入参
|
||||
- 路径:`out_bill_no`(string) — 商户提现单号
|
||||
- body:无
|
||||
|
||||
## 出参
|
||||
响应 `200`:`WithdrawOrderOut`(归一化后的最新提现单)
|
||||
|
||||
**WithdrawOrderOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 提现单主键 |
|
||||
| `user_id` | int | 所属用户 |
|
||||
| `out_bill_no` | string | 商户提现单号 |
|
||||
| `amount_cents` | int | 提现金额,单位:**分** |
|
||||
| `status` | string | 单状态(`pending` / `success` / `failed`) |
|
||||
| `wechat_state` | string \| null | 微信侧转账状态 |
|
||||
| `transfer_bill_no` | string \| null | 微信转账单号 |
|
||||
| `fail_reason` | string \| null | 失败/退款原因 |
|
||||
| `created_at` | datetime | 创建时间(ISO 8601 UTC) |
|
||||
| `updated_at` | datetime | 更新时间(ISO 8601 UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(响应头带 `WWW-Authenticate: Bearer`)
|
||||
- `403` 角色不足(需 `finance` 或 `super_admin`)
|
||||
- `404` 提现单不存在(`detail="提现单不存在"`)
|
||||
- `503` 微信支付未配置(捕获 `WxPayNotConfiguredError`,`detail="微信支付未配置"`)
|
||||
|
||||
> 注:微信查单本身失败(非未配置)时不抛错、不改状态,单保持 `pending` 等下次重试,故无 `502`。
|
||||
|
||||
## 说明
|
||||
- 调用底层 `app.repositories.wallet.refresh_withdraw_status(..., cancel_if_unconfirmed=True)`(内部调微信查单/撤单/退款并 commit)。
|
||||
- 写操作记审计 [admin_audit_log](../database/admin_audit_log.md)(`action="withdraw.refresh"`,`target_id=out_bill_no`,`detail` 为 `{status, wechat_state}`,含触发管理员与客户端 IP)。
|
||||
- 关联 [withdraw_order](../database/withdraw_order.md)。金额单位为分。
|
||||
@@ -0,0 +1,37 @@
|
||||
# GET /admin/api/withdraws — 提现单列表(游标分页)
|
||||
|
||||
> 所属:Admin·提现 组(前缀 `/admin/api/withdraws`) | 鉴权:Bearer admin_token(角色:任意已登录管理员,列表为只读,仅需 `get_current_admin`,无 `require_role`) | [← 返回 API 索引](./README.md)
|
||||
|
||||
跨用户查询全量提现单,可按 `user_id` / `status` 过滤。游标分页(`id` 倒序)。
|
||||
|
||||
## 入参(query)
|
||||
| 字段 | 类型 | 必填 | 默认 | 说明 |
|
||||
|---|---|---|---|---|
|
||||
| `user_id` | int | ❌ | null | 按用户过滤;不传则查全量 |
|
||||
| `status` | string | ❌ | null | 按状态过滤(如 `pending` / `success` / `failed`) |
|
||||
| `limit` | int | ❌ | 20 | 1–100 |
|
||||
| `cursor` | int | ❌ | null | 上一页 next_cursor(查 `id < cursor`) |
|
||||
|
||||
## 出参
|
||||
响应 `200`:`{ items: WithdrawOrderOut[], next_cursor: int|null }`(`next_cursor=null` 表示末页)
|
||||
|
||||
**WithdrawOrderOut**
|
||||
| 字段 | 类型 | 说明 |
|
||||
|---|---|---|
|
||||
| `id` | int | 提现单主键 |
|
||||
| `user_id` | int | 所属用户 |
|
||||
| `out_bill_no` | string | 商户提现单号(业务主键,重试/对账以此为准) |
|
||||
| `amount_cents` | int | 提现金额,单位:**分** |
|
||||
| `status` | string | 单状态(`pending` / `success` / `failed`) |
|
||||
| `wechat_state` | string \| null | 微信侧转账状态(如 `ACCEPTED` / `PROCESSING` / `WAIT_USER_CONFIRM` / `SUCCESS`) |
|
||||
| `transfer_bill_no` | string \| null | 微信转账单号 |
|
||||
| `fail_reason` | string \| null | 失败/退款原因 |
|
||||
| `created_at` | datetime | 创建时间(ISO 8601 UTC) |
|
||||
| `updated_at` | datetime | 更新时间(ISO 8601 UTC) |
|
||||
|
||||
## 错误码
|
||||
- `401` 未带/无效/过期 admin token、管理员被禁用(响应头带 `WWW-Authenticate: Bearer`)
|
||||
- `422` query 参数校验失败(如 `limit` 越界、类型错误)
|
||||
|
||||
## 说明
|
||||
关联 [withdraw_order](../database/withdraw_order.md)。金额单位为分。
|
||||
@@ -31,9 +31,10 @@
|
||||
|
||||
**Item**:`{ name: string, qty: int=1, specs: string[]\|null }`
|
||||
|
||||
**Result(comparison_results 元素)**:`{ platform_id, platform_name, package, price(元,float\|null), is_source(bool), rank(int\|null), coupon_saved(元,float\|null) }`
|
||||
**Result(comparison_results 元素)**:`{ platform_id, platform_name, package, price(元,float\|null), is_source(bool), rank(int\|null), coupon_saved(元,float\|null), coupon_name(string\|null) }`
|
||||
|
||||
- `coupon_saved`:该平台本单**平台主优惠额**(元)——美团红包 / 淘宝平台红包 / 京东优惠券·百亿补贴,**只取那一笔**,不含配送费减免/共减总额。仅外卖目标平台带值,源平台/没用为 `null`,前端记录页据此展示**「已优惠 ¥X」**(null 不展示)。
|
||||
- `coupon_name`:优惠**来源名**(展示用,best-effort)——美团「外卖大额神券」/ 京东「百亿补贴」/ 淘宝「平台红包」。仅在该行有 `coupon_saved` 时带;`null` 时前端走通用「红包」。非用户选中的具体券名(选中态不可读)。
|
||||
|
||||
## 服务端派生(客户端不用算)
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
| `week_saved_cents` | int | 本周已省(分) |
|
||||
| `beat_percent` | int | 超过百分之多少用户 |
|
||||
| `streak_days` | int | 连续省钱天数 |
|
||||
| `compare_count` | int | 累计完成比价次数(= 比价上报记录数;有真实 `compare` 记录用真实数,否则 demo 兜底) |
|
||||
|
||||
## 说明
|
||||
「我的」页「省钱战绩」卡数据源。接口已通但无真实业务写入,实际多为 0。
|
||||
「我的」页「省钱战绩」卡数据源(三列:本周已省 / 完成比价 / 连续比价)。`compare_count` 与 `/summary` 的 `order_count` 同源(均 = 有效记录数),对应原型「完成比价(次)」列。接口已通但无真实业务写入时走 demo 兜底(seed 约 23 条)。
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
> 数据库:SQLite 起步(`data/app.db`),生产可切 PostgreSQL(改 `DATABASE_URL`)。
|
||||
> ORM:SQLAlchemy 2.0(`app/models/`),迁移:Alembic(`alembic/versions/`,`render_as_batch` 兼容 SQLite)。
|
||||
> 金额字段一律存**整数**:金币=个数,现金=**分**(`*_cents`)。时间列 `DateTime(timezone=True)`。
|
||||
> 最后更新:2026-05-31
|
||||
> 最后更新:2026-06-04(+ admin_user / admin_audit_log)
|
||||
|
||||
---
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
| `feedback` | 用户帮助与反馈 | `models/feedback.py` | 反馈 | [详情](./feedback.md) |
|
||||
| `comparison_record` | 比价记录(每次比价完整明细) | `models/comparison.py` | 比价记录 | [详情](./comparison_record.md) |
|
||||
| `comparison_milestone_claim` | 比价战绩里程碑领取记录 | `models/comparison_milestone.py` | 比价记录/福利 | [详情](./comparison_milestone_claim.md) |
|
||||
| `admin_user` | 运营后台管理员账号(独立鉴权) | `models/admin.py` | Admin 后台 | [详情](./admin_user.md) |
|
||||
| `admin_audit_log` | 运营后台操作审计日志(只追加) | `models/admin.py` | Admin 后台 | [详情](./admin_audit_log.md) |
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
# admin_audit_log — 运营后台操作审计日志
|
||||
|
||||
> 模型 `app/models/admin.py` | 关联接口 [admin-audit-logs](../api/admin-audit-logs.md) | [← 表索引](./README.md)
|
||||
|
||||
每个**写操作**(改钱/改状态/处理反馈等)落一条,记录"谁在何时、对谁、做了什么、前后值"。仅追加、不可删,用于追溯。`admin_username` / `target_id` 冗余存字符串,即使关联对象被删/改名也能追溯。
|
||||
|
||||
## 字段
|
||||
| 列 | 类型 | 约束 / 默认 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `id` | Integer | PK, autoincrement | |
|
||||
| `admin_id` | Integer | FK→admin_user.id, index, NOT NULL | 操作者 |
|
||||
| `admin_username` | String(64) | NOT NULL | 冗余操作者用户名(改名/禁用后仍可追溯) |
|
||||
| `action` | String(64) | index, NOT NULL | 操作类型,如 `user.coins.grant` / `user.status.set` / `withdraw.refresh` / `feedback.handle` |
|
||||
| `target_type` | String(32) | NOT NULL | 被操作对象类型,如 `user` / `withdraw` / `feedback` |
|
||||
| `target_id` | String(64) | nullable | 被操作对象 id(用字符串以兼容 `out_bill_no` 等非整型主键) |
|
||||
| `detail` | JSON | nullable | 上下文 + 前后值,如 `{"amount":1000,"reason":"...","before":{...},"after":{...}}` |
|
||||
| `ip` | String(64) | nullable | 操作者客户端 IP(取自 `X-Forwarded-For` 首段,仅记录不鉴权) |
|
||||
| `created_at` | DateTime(tz) | server_default now(), index, NOT NULL | 操作时间 |
|
||||
|
||||
## 索引与约束
|
||||
- PK: `id`
|
||||
- index: `ix_admin_audit_log_admin_id`(`admin_id`)、`ix_admin_audit_log_action`(`action`)、`ix_admin_audit_log_created_at`(`created_at`)
|
||||
|
||||
## 关系
|
||||
- `admin_id` → [`admin_user`](./admin_user.md).`id`(多对一)
|
||||
|
||||
## 说明
|
||||
- **JSON 列**:`detail` 用 `JSON().with_variant(JSONB(), "postgresql")` —— PG 上 JSONB,SQLite 退化为通用 JSON(同 `comparison_record.raw_payload`)。
|
||||
- **只追加**:无更新/删除接口,审计不可篡改。
|
||||
- **IP 可伪造**:`X-Forwarded-For` 可被客户端伪造,nginx 必须用 `proxy_set_header X-Forwarded-For $remote_addr` 覆盖;审计 IP 仅作记录、不参与鉴权。
|
||||
@@ -0,0 +1,28 @@
|
||||
# admin_user — 运营后台管理员账号
|
||||
|
||||
> 模型 `app/models/admin.py` | 关联接口 [admin-auth-login](../api/admin-auth-login.md) / [admin-admins-list](../api/admin-admins-list.md) | [← 表索引](./README.md)
|
||||
|
||||
运营后台的管理员账号,与 App 用户(`user` 表)**完全隔离**:走独立 JWT secret、独立鉴权链(见 `app/admin/`)。密码 bcrypt 存哈希,带角色做权限分级。
|
||||
|
||||
## 字段
|
||||
| 列 | 类型 | 约束 / 默认 | 说明 |
|
||||
|---|---|---|---|
|
||||
| `id` | Integer | PK, autoincrement | |
|
||||
| `username` | String(64) | unique, index, NOT NULL | 登录名 |
|
||||
| `password_hash` | String(255) | NOT NULL | bcrypt 哈希(明文不落库) |
|
||||
| `role` | String(20) | NOT NULL, default `operator` | `super_admin`(全权+管账号)/ `finance`(钱:提现+金币)/ `operator`(用户+反馈+大盘) |
|
||||
| `status` | String(20) | NOT NULL, default `active` | `active` / `disabled`(禁用后 token 立即失效) |
|
||||
| `created_at` | DateTime(tz) | server_default now(), NOT NULL | 创建时间 |
|
||||
| `last_login_at` | DateTime(tz) | nullable | 最近登录时间(登录成功时更新) |
|
||||
|
||||
## 索引与约束
|
||||
- PK: `id`
|
||||
- unique index: `ix_admin_user_username`(`username` 唯一)
|
||||
|
||||
## 关系
|
||||
- 被 [`admin_audit_log`](./admin_audit_log.md).`admin_id` 引用(一管理员多条审计)。
|
||||
|
||||
## 说明
|
||||
- **角色权限**:`super_admin` 恒通过所有角色守卫(`require_role`);`finance` 管钱(提现/金币),`operator` 管用户/反馈/大盘。具体守卫见各接口文档。
|
||||
- **鉴权隔离**:admin token `typ=admin` + 独立 `ADMIN_JWT_SECRET`(≠ App 的 `JWT_SECRET_KEY`),App 用户 token 无法当 admin 用。admin 无 refresh,过期(默认 12h)重新登录。
|
||||
- **初始化**:首个管理员用 `scripts/create_admin.py` 命令行创建(无自助注册接口)。
|
||||
@@ -29,7 +29,7 @@
|
||||
| `status` | String(16) | NOT NULL, default `success` | `success`(拿到有效对比)/ `failed`(出错/没采到目标价) |
|
||||
| `information` | String(256) | nullable | done 帧文案;成功=摘要,失败=具体原因(前端失败时当原因展示) |
|
||||
| `items` | JSON(PG: JSONB) | NOT NULL, default [] | 下单菜品 `[{name, qty, specs?}]` |
|
||||
| `comparison_results` | JSON(PG: JSONB) | NOT NULL, default [] | 逐平台对比 `[{platform_id,platform_name,package,price(元),is_source,rank,coupon_saved}]`;`coupon_saved`=该平台主优惠额(元,美团红包/淘宝平台红包/京东优惠券·百亿补贴,只取那一笔,不含配送费/共减总额),各平台抠到红包即带值(2026-06 起源平台 Phase1 意图识别也抠,当前仅淘宝源),没用/没抠到为 null,前端展示「已优惠 ¥X」 |
|
||||
| `comparison_results` | JSON(PG: JSONB) | NOT NULL, default [] | 逐平台对比 `[{platform_id,platform_name,package,price(元),is_source,rank,coupon_saved,coupon_name}]`;`coupon_saved`=该平台主优惠额(元,美团红包/淘宝平台红包/京东优惠券·百亿补贴,只取那一笔,不含配送费/共减总额),各平台抠到红包即带值(2026-06 起源平台 Phase1 意图识别也抠,当前仅淘宝源),没用/没抠到为 null,前端展示「已优惠 ¥X」;`coupon_name`=优惠来源名(展示用 best-effort,美团"外卖大额神券"/京东"百亿补贴"/淘宝"平台红包"),null 走前端通用"红包" |
|
||||
| `skipped_dish_names` | JSON(PG: JSONB) | NOT NULL, default [] | 被跳过的菜名 |
|
||||
| `raw_payload` | JSON(PG: JSONB) | nullable | 客户端原始上报(calibration + done.params 全量) |
|
||||
| `created_at` | DateTime(tz) | server_default now(), index | 时间 |
|
||||
|
||||
@@ -33,7 +33,8 @@ def _food_payload(trace_id: str) -> dict:
|
||||
],
|
||||
"comparison_results": [
|
||||
{"platform_id": "meituan", "platform_name": "美团", "package": "com.sankuai.meituan",
|
||||
"price": 123.50, "is_source": False, "rank": 1, "coupon_saved": 7.0},
|
||||
"price": 123.50, "is_source": False, "rank": 1,
|
||||
"coupon_saved": 7.0, "coupon_name": "外卖大额神券"},
|
||||
{"platform_id": "taobao_flash", "platform_name": "淘宝闪购", "package": "com.taobao.taobao",
|
||||
"price": 128.50, "is_source": True, "rank": 2},
|
||||
{"platform_id": "jd_waimai", "platform_name": "京东外卖", "package": "com.jingdong.app.mall",
|
||||
@@ -79,7 +80,7 @@ def test_report_and_derive(client) -> None:
|
||||
|
||||
|
||||
def test_coupon_saved_passthrough(client) -> None:
|
||||
"""红包优惠额 coupon_saved 上报→落库→读出;仅目标平台带值,源平台不带(null)。"""
|
||||
"""红包优惠额 coupon_saved + 来源名 coupon_name 上报→落库→读出;仅目标平台带值,源平台不带(null)。"""
|
||||
token = _login(client, "13800002010")
|
||||
rid = client.post(
|
||||
"/api/v1/compare/record", json=_food_payload("trace-coupon"), headers=_auth(token)
|
||||
@@ -87,7 +88,9 @@ def test_coupon_saved_passthrough(client) -> None:
|
||||
d = client.get(f"/api/v1/compare/records/{rid}", headers=_auth(token)).json()
|
||||
by_pid = {x["platform_id"]: x for x in d["comparison_results"]}
|
||||
assert by_pid["meituan"]["coupon_saved"] == 7.0 # 目标平台带值
|
||||
assert by_pid["meituan"]["coupon_name"] == "外卖大额神券" # 来源名透传(pricebot#38)
|
||||
assert by_pid["taobao_flash"].get("coupon_saved") is None # 源平台不带/为 null
|
||||
assert by_pid["taobao_flash"].get("coupon_name") is None # 源平台无来源名
|
||||
|
||||
|
||||
def test_source_is_cheapest_no_saving(client) -> None:
|
||||
|
||||
+53
-6
@@ -133,10 +133,10 @@ def test_exchange_info(client) -> None:
|
||||
|
||||
|
||||
def test_exchange_flow(client) -> None:
|
||||
"""先供款够兑换下限的金币 → 兑换 1 元 → 金币扣、现金加 → 现金流水有记录。
|
||||
"""先供款够兑 1 元的金币 → 兑换 1 元 → 金币扣、现金加 → 现金流水有记录。
|
||||
|
||||
打开消息提醒任务已降到 1000 金币(不再 = 兑换下限), 不能再靠领任务供款;
|
||||
直接 grant_coins 注入 MIN_EXCHANGE_COIN(= COIN_PER_YUAN = 10000)当种子。
|
||||
兑换下限已降到 1 分(MIN_EXCHANGE_COIN = COIN_PER_CENT = 100), 但本用例验证兑换 1 元,
|
||||
直接 grant_coins 注入 COIN_PER_YUAN(= 10000)当种子(够兑 1 元)。
|
||||
"""
|
||||
phone = "13800001005"
|
||||
token = _login(client, phone)
|
||||
@@ -144,7 +144,7 @@ def test_exchange_flow(client) -> None:
|
||||
with SessionLocal() as db:
|
||||
user = get_user_by_phone(db, phone)
|
||||
assert user is not None
|
||||
crud_wallet.grant_coins(db, user.id, MIN_EXCHANGE_COIN, biz_type="test_seed", remark="测试供款")
|
||||
crud_wallet.grant_coins(db, user.id, COIN_PER_YUAN, biz_type="test_seed", remark="测试供款")
|
||||
db.commit()
|
||||
|
||||
# 兑换 10000 金币 → 100 分
|
||||
@@ -180,6 +180,51 @@ def test_exchange_flow(client) -> None:
|
||||
assert page["items"][0]["biz_type"] == "exchange_in"
|
||||
|
||||
|
||||
def test_exchange_min_floor_one_cent(client) -> None:
|
||||
"""锁定本次下调的兑换下限:正好兑下限额(MIN_EXCHANGE_COIN=100 金币=1 分)应成功。
|
||||
|
||||
这是本 PR 的核心新能力(下限 10000→100,可兑 1 分起)。test_exchange_flow 兑的是
|
||||
1 元(10000),在旧下限下也通过,证明不了新下限;本用例供款并兑换正好等于下限的
|
||||
100 金币,断言到账 1 分。若有人把 MIN_EXCHANGE_COIN 改回 10000,100 会因低于下限被
|
||||
判 400,本用例随之失败,从而把新下限值钉死。
|
||||
"""
|
||||
phone = "13800001010"
|
||||
token = _login(client, phone)
|
||||
# 供款: 正好注入一个下限额度的金币(=COIN_PER_CENT=100)
|
||||
with SessionLocal() as db:
|
||||
user = get_user_by_phone(db, phone)
|
||||
assert user is not None
|
||||
crud_wallet.grant_coins(
|
||||
db, user.id, MIN_EXCHANGE_COIN, biz_type="test_seed", remark="测试供款"
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# 兑换下限额 100 金币 → 1 分
|
||||
r = client.post(
|
||||
"/api/v1/wallet/exchange",
|
||||
json={"coin_amount": MIN_EXCHANGE_COIN},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert r.status_code == 200, r.text
|
||||
res = r.json()
|
||||
assert res["coin_amount"] == MIN_EXCHANGE_COIN
|
||||
assert res["cash_added_cents"] == 1 # coins_to_cents(100) == 1
|
||||
assert res["coin_balance"] == 0
|
||||
assert res["cash_balance_cents"] == 1
|
||||
|
||||
# 金币流水有一笔 exchange_out(=-100)
|
||||
r = client.get("/api/v1/wallet/coin-transactions", headers=_auth(token))
|
||||
out_txn = next(t for t in r.json()["items"] if t["biz_type"] == "exchange_out")
|
||||
assert out_txn["amount"] == -MIN_EXCHANGE_COIN
|
||||
|
||||
# 现金流水有且仅有一笔 exchange_in(=+1 分)
|
||||
r = client.get("/api/v1/wallet/cash-transactions", headers=_auth(token))
|
||||
cash = r.json()["items"]
|
||||
assert len(cash) == 1
|
||||
assert cash[0]["amount_cents"] == 1
|
||||
assert cash[0]["biz_type"] == "exchange_in"
|
||||
|
||||
|
||||
def test_exchange_insufficient_and_invalid(client) -> None:
|
||||
token = _login(client, "13800001006")
|
||||
|
||||
@@ -191,10 +236,10 @@ def test_exchange_insufficient_and_invalid(client) -> None:
|
||||
)
|
||||
assert r.status_code == 409
|
||||
|
||||
# 低于最小额 → 400
|
||||
# 低于最小额(下限现为 1 分=100;取 MIN_EXCHANGE_COIN-1=99,既低于下限又非整分)→ 400
|
||||
r = client.post(
|
||||
"/api/v1/wallet/exchange",
|
||||
json={"coin_amount": MIN_EXCHANGE_COIN - COIN_PER_CENT},
|
||||
json={"coin_amount": MIN_EXCHANGE_COIN - 1},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert r.status_code == 400
|
||||
@@ -226,6 +271,8 @@ def test_savings_summary_and_battle(client) -> None:
|
||||
assert b["streak_days"] >= 1
|
||||
assert b["week_saved_cents"] >= 0
|
||||
assert 0 <= b["beat_percent"] <= 100
|
||||
# 完成比价次数与 summary 的 order_count 同源(均 = 有效记录数)
|
||||
assert b["compare_count"] == s["order_count"]
|
||||
|
||||
|
||||
def test_savings_seeder_idempotent(client) -> None:
|
||||
|
||||
Reference in New Issue
Block a user