fix: 令牌不再走 query(白名单除外)、订阅额度发成功才扣、测试临时目录移出仓库、kpt 外置 json、README 产品优先
This commit is contained in:
+13
-1
@@ -40,6 +40,18 @@ ROUTES: list[tuple[str, re.Pattern, object, str]] = []
|
||||
PROD = os.environ.get("QINGLING_ENV", "dev").strip().lower() in ("prod", "production")
|
||||
DEV_ONLY_PREFIXES = ("/api/dev/", "/api/auth/sms-")
|
||||
MAX_BODY = 16 * 1024 * 1024 # 与 Caddy 的 request_body max_size 对齐
|
||||
|
||||
# 只有「要贴到新标签页/发给他人的链接」才允许把令牌放在 query 里。
|
||||
# 其它接口一律走 Authorization 头——令牌进 URL 会被反代 access log 明文记下来。
|
||||
QUERY_TOKEN_PATHS = (
|
||||
re.compile(r"^/api/export/csv$"),
|
||||
re.compile(r"^/api/print/weekly$"),
|
||||
re.compile(r"^/api/papers/[^/]+/clean$"),
|
||||
)
|
||||
|
||||
|
||||
def query_token_ok(path: str) -> bool:
|
||||
return any(p.match(path) for p in QUERY_TOKEN_PATHS)
|
||||
CORS_ORIGINS = [o.strip() for o in
|
||||
(os.environ.get("QINGLING_CORS") or ("*" if not PROD else "")).split(",")
|
||||
if o.strip()]
|
||||
@@ -544,7 +556,7 @@ class App(BaseHTTPRequestHandler):
|
||||
return
|
||||
auth = self.headers.get("Authorization") or ""
|
||||
token = auth[7:].strip() if auth.lower().startswith("bearer ") else None
|
||||
if not token:
|
||||
if not token and query_token_ok(ctx.path):
|
||||
token = (ctx.query.get("token") or [None])[0]
|
||||
if not token:
|
||||
raise ApiError(401, "UNAUTHORIZED", "缺少 Authorization: Bearer <token>")
|
||||
|
||||
+28
-8
@@ -474,14 +474,29 @@ def subscribe_quota(conn, gid: str) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def _take_grant(conn, gid: str, template_id: str) -> int | None:
|
||||
"""扣一条额度,返回 grant id;没有额度返回 None。"""
|
||||
def _peek_grant(conn, gid: str, template_id: str) -> int | None:
|
||||
"""看一眼还有没有额度,**不扣**。
|
||||
|
||||
扣额度必须发生在「发送成功之后」:一次性订阅是用户点一次才给一条,
|
||||
先扣后发,发失败就成了「付了没拿到」——这个品类的差评里 32.9% 就是收费套路。
|
||||
"""
|
||||
row = conn.execute(
|
||||
"SELECT id FROM subscribe_grants WHERE guardian_id=? AND template_id=? AND used_at IS NULL "
|
||||
"ORDER BY id LIMIT 1", (gid, template_id)).fetchone()
|
||||
if row is None:
|
||||
return int(row["id"]) if row else None
|
||||
|
||||
|
||||
def _mark_grant_used(conn, grant_id: int) -> None:
|
||||
conn.execute("UPDATE subscribe_grants SET used_at=? WHERE id=?", (now(), grant_id))
|
||||
|
||||
|
||||
def _take_grant(conn, gid: str, template_id: str) -> int | None:
|
||||
"""扣一条额度(保留给「确定要发」的调用方);没有额度返回 None。"""
|
||||
gid_row = _peek_grant(conn, gid, template_id)
|
||||
if gid_row is None:
|
||||
return None
|
||||
conn.execute("UPDATE subscribe_grants SET used_at=? WHERE id=?", (now(), row["id"]))
|
||||
_mark_grant_used(conn, gid_row)
|
||||
return gid_row
|
||||
return int(row["id"])
|
||||
|
||||
|
||||
@@ -1150,7 +1165,7 @@ def notifications_tick(conn, on: str | None = None) -> dict:
|
||||
额度是「一次性订阅」的:发一条就扣一条,扣完就不再发。
|
||||
"""
|
||||
d = on or today()
|
||||
made, skipped, need = [], [], []
|
||||
made, skipped, need, fails = [], [], [], []
|
||||
tmpl = subscribe_template("daily")
|
||||
children = conn.execute("SELECT * FROM children WHERE deleted_at IS NULL").fetchall()
|
||||
for c in children:
|
||||
@@ -1201,7 +1216,7 @@ def notifications_tick(conn, on: str | None = None) -> dict:
|
||||
continue
|
||||
|
||||
gid = c["guardian_id"]
|
||||
grant = _take_grant(conn, gid, tmpl)
|
||||
grant = _peek_grant(conn, gid, tmpl)
|
||||
if grant is None:
|
||||
conn.execute(
|
||||
"INSERT INTO notifications (child_id, kind, payload, scheduled_at, skipped_reason)"
|
||||
@@ -1216,9 +1231,14 @@ def notifications_tick(conn, on: str | None = None) -> dict:
|
||||
g = conn.execute("SELECT openid FROM guardians WHERE id=?", (gid,)).fetchone()
|
||||
msg = wechat.daily_payload(c["nickname"], n, d, tmpl)
|
||||
res = wechat.send_subscribe(g["openid"] if g else "", msg)
|
||||
# 本地模式不算失败:额度照扣、记账照写,只是不真的发出去
|
||||
# 本地模式不算失败(演示要能看到流程走通)
|
||||
delivered = bool(res["sent"]) or bool(res.get("dev"))
|
||||
channel = "wx" if res["sent"] else ("dev" if res.get("dev") else "failed")
|
||||
if delivered:
|
||||
_mark_grant_used(conn, grant) # 只有真发出去了才扣额度
|
||||
else:
|
||||
# 发送失败:额度退回(本来就没扣),并记下原因,下次还能用
|
||||
fails.append({"child_id": c["id"], "reason": res.get("message") or "发送失败"})
|
||||
cur = conn.execute(
|
||||
"INSERT INTO notifications (child_id, kind, payload, scheduled_at, sent_at, skipped_reason)"
|
||||
" VALUES (?,?,?,?,?,?)",
|
||||
@@ -1233,7 +1253,7 @@ def notifications_tick(conn, on: str | None = None) -> dict:
|
||||
skipped.append({"child_id": c["id"], "reason": f"微信发送失败:{res['errmsg']}"})
|
||||
need.append({"child_id": c["id"], "guardian_id": gid, "why": res["errmsg"]})
|
||||
return {"date": d, "queued": made, "skipped": skipped, "need_subscribe": need,
|
||||
"wx_mode": wechat.mode()}
|
||||
"failed": fails, "wx_mode": wechat.mode()}
|
||||
|
||||
|
||||
def stats(conn, gid: str, cid: str) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user