feat(wx-poc): 会话存档按小程序卡区分源平台,支持京东
原 poller 把所有 weapp 卡片写死成 source="meituan",分享京东也会让手机弹/开 美团。现按 weapp 结构判源: - poller 新增 _resolve_source(): 按 weapp username/appid/title 关键词判平台 (jingdong/京东 → jd, meituan/美团 → meituan);认不出默认 meituan + 打 WARNING 并日志全量 weapp 结构,便于照真实 username/appid 精确补规则。 - app-server _SOURCE_PACKAGES 加 jd → com.jingdong.app.mall。 客户端无需改动(只认 source_package 反查平台名 + launch),纯服务端可热部署。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -29,8 +29,12 @@ from app.schemas.device import (
|
||||
|
||||
logger = logging.getLogger("shagua.device")
|
||||
|
||||
# PoC:源平台代号 → Android 包名(前端 launch 用)。当前只用美团。
|
||||
_SOURCE_PACKAGES = {"meituan": "com.sankuai.meituan"}
|
||||
# PoC:源平台代号 → Android 包名(前端 launch 用)。poller 按分享的小程序卡区分平台。
|
||||
# 客户端只认 source_package:用它反查平台名(弹窗"原平台")+ launch 对应 App,故新增平台只需在此登记包名。
|
||||
_SOURCE_PACKAGES = {
|
||||
"meituan": "com.sankuai.meituan",
|
||||
"jd": "com.jingdong.app.mall", # 京东主 App(含京东秒送/外卖),后端 intent skill=jd_waimai
|
||||
}
|
||||
|
||||
router = APIRouter(prefix="/api/v1/device", tags=["device"])
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import signal
|
||||
import time
|
||||
@@ -62,6 +63,25 @@ def _notify(client: httpx.Client, source: str, kind: str) -> None:
|
||||
logger.exception("notify app-server 失败")
|
||||
|
||||
|
||||
def _resolve_source(msg: dict) -> str:
|
||||
"""从 weapp 小程序卡判源平台代号(= app-server _SOURCE_PACKAGES 的 key)。
|
||||
|
||||
会话存档 weapp 结构里 username(gh_id)/appid/pagepath/title 任一含平台特征即判定:
|
||||
先按关键词启发式(覆盖大多数);认不出 → 默认 meituan(过渡期不阻断)+ WARNING,
|
||||
并把整个 weapp 结构打进日志,便于照真实 username/appid 精确补规则。
|
||||
"""
|
||||
weapp = msg.get("weapp") or {}
|
||||
blob = json.dumps(weapp, ensure_ascii=False).lower()
|
||||
# 排障日志:打全 weapp 结构(顺带 msg 顶层 key,防结构不在 weapp 下),便于补/改判定规则
|
||||
logger.info("weapp 判源: msg_keys=%s weapp=%s", list(msg.keys()), blob[:1500])
|
||||
if any(k in blob for k in ("jingdong", "jd.com", "jddj", "京东")):
|
||||
return "jd"
|
||||
if any(k in blob for k in ("meituan", "sankuai", "美团")):
|
||||
return "meituan"
|
||||
logger.warning("weapp 源平台未识别, 暂默认 meituan;见上一行结构日志补 _resolve_source 规则")
|
||||
return "meituan"
|
||||
|
||||
|
||||
def _handle(client: httpx.Client, msg: dict) -> None:
|
||||
"""明文消息:外部用户(from≠接收成员)发来的 weapp/image → 通知 app-server 打信号。"""
|
||||
msgtype = msg.get("msgtype") or ""
|
||||
@@ -71,8 +91,10 @@ def _handle(client: httpx.Client, msg: dict) -> None:
|
||||
return # 成员自己发的, 跳过
|
||||
if msgtype not in ("weapp", "image"):
|
||||
return
|
||||
logger.info("命中触发 from=%s type=%s", frm, msgtype)
|
||||
_notify(client, "meituan", msgtype)
|
||||
# weapp 卡片按小程序判源平台;image(截图)判不出平台,沿用 meituan。
|
||||
source = _resolve_source(msg) if msgtype == "weapp" else "meituan"
|
||||
logger.info("命中触发 from=%s type=%s source=%s", frm, msgtype, source)
|
||||
_notify(client, source, msgtype)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user