From 39b240fc6437982ff96f5259f7c1505a28c352bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8A=E9=9F=AC?= <1075983130@qq.com> Date: Wed, 2 Dec 2026 22:32:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=93=E5=B1=9E=E6=B5=8B=E8=AF=95=E6=9C=BA?= =?UTF-8?q?=EF=BC=9A=E6=B5=8B=E8=AF=95=E4=BF=A1=E5=8F=B7=E5=8F=AA=E5=8F=91?= =?UTF-8?q?=E7=BB=99=E6=A0=87=E8=AE=B0=E8=AE=BE=E5=A4=87=EF=BC=8C=E6=97=A7?= =?UTF-8?q?=E7=89=88=E6=94=B6=E4=B8=8D=E5=88=B0(devices.is=5Ftester?= =?UTF-8?q?=E5=88=97=EF=BC=9B=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E6=B3=A8=E5=85=A5XL=5FTESTER=3D1=E6=89=8D=E4=B8=8A=E6=8A=A5?= =?UTF-8?q?=EF=BC=9B=E4=B8=8D=E6=8C=87=E5=AE=9A=E6=97=B6=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E5=8F=91=E7=BB=99=E5=85=A8=E9=83=A8=E8=AE=BE=E5=A4=87)?= =?UTF-8?q?=EF=BC=9B=E7=89=88=E6=9C=AC=E5=8D=87=E8=87=B31.4.6+17(=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=89=88=EF=BC=8C=E4=B8=8D=E4=B8=8A=E4=BC=A0=E7=94=9F?= =?UTF-8?q?=E4=BA=A7=E5=88=86=E5=8F=91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/core/push/push_service.dart | 4 +++ app/lib/features/misc/app_updater.dart | 2 +- app/pubspec.yaml | 2 +- server/app/db.py | 4 +++ server/app/routers/admin.py | 31 +++++++++++++++-------- server/app/routers/me.py | 12 ++++++--- server/tests/test_tester_device.py | 35 ++++++++++++++++++++++++++ 7 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 server/tests/test_tester_device.py diff --git a/app/lib/core/push/push_service.dart b/app/lib/core/push/push_service.dart index 4d9ec73..6f4108c 100644 --- a/app/lib/core/push/push_service.dart +++ b/app/lib/core/push/push_service.dart @@ -25,6 +25,9 @@ class PushService { String? _rid; static const _appKey = String.fromEnvironment('JPUSH_APP_KEY'); + /// 专属测试机标记:仅联调包经 --dart-define=XL_TESTER=1 注入。 + /// 正式包不注入 → false,服务端不会把测试信号发给它。 + static const isTester = String.fromEnvironment('XL_TESTER') == '1'; bool get _configured => _appKey.isNotEmpty; Future init() async { @@ -75,6 +78,7 @@ class PushService { 'device_id': _deviceId, 'brand': defaultTargetBrand(), 'jpush_rid': _rid, + 'is_tester': isTester, }); } catch (_) { // 静默;下次登录重试 diff --git a/app/lib/features/misc/app_updater.dart b/app/lib/features/misc/app_updater.dart index 3c22629..4169dd0 100644 --- a/app/lib/features/misc/app_updater.dart +++ b/app/lib/features/misc/app_updater.dart @@ -14,7 +14,7 @@ import 'package:path_provider/path_provider.dart'; import '../../core/network/api_client.dart'; import '../../core/theme/app_colors.dart'; -const kAppVersion = '1.4.5'; // 与 pubspec.yaml version 同步 +const kAppVersion = '1.4.6'; // 与 pubspec.yaml version 同步 /// 比较语义化版本:a < b 返回 -1,> 返回 1,相等 0 int compareVersion(String a, String b) { diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 6009df4..7d60629 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.4.5+16 +version: 1.4.6+17 environment: sdk: ^3.12.2 diff --git a/server/app/db.py b/server/app/db.py index e05b904..dce0864 100644 --- a/server/app/db.py +++ b/server/app/db.py @@ -176,4 +176,8 @@ def init_db() -> None: cols = {r["name"] for r in conn.execute("PRAGMA table_info(signals)")} if "kind" not in cols: conn.execute("ALTER TABLE signals ADD COLUMN kind TEXT NOT NULL DEFAULT 'divergence'") + # 存量库迁移:devices.is_tester(专属测试机,默认 0=正式设备) + cols = {r["name"] for r in conn.execute("PRAGMA table_info(devices)")} + if "is_tester" not in cols: + conn.execute("ALTER TABLE devices ADD COLUMN is_tester INTEGER NOT NULL DEFAULT 0") conn.commit() diff --git a/server/app/routers/admin.py b/server/app/routers/admin.py index ed08393..fb6f5ef 100644 --- a/server/app/routers/admin.py +++ b/server/app/routers/admin.py @@ -141,23 +141,32 @@ def list_signals(symbol: str = "", interval: str = "", date: str = "", limit: in # 自定义消息走真实闹钟链路(JPush message + signal_alarm extras)发到设备。 class TestSignalIn(BaseModel): - user_id: int | None = None # 不填则发给全部已注册推送的设备 + pass # 预留;测试信号只发给专属测试机,不再支持"发给全部设备" + + +def pick_test_devices(rows: list[dict]) -> list[str]: + """从设备行里挑出专属测试机的极光注册ID。is_tester 为 NULL/0(旧版)一律不发。""" + return [r["jpush_rid"] for r in rows if r.get("is_tester") == 1 and r.get("jpush_rid")] @router.post("/test-signal") def test_signal(body: TestSignalIn, _: None = Depends(verify_admin)): + """只发给标记为专属测试机的设备(2026-12-02)。 + + 此前发给全部已注册推送的设备,联调一次会打扰所有用户。旧版客户端不上报 + is_tester,该列为 0,永远收不到测试信号。 + """ from ..notify.push import push_test_signal - conds = "rid_invalid = 0 AND jpush_rid IS NOT NULL" - params: list = [] - if body.user_id: - conds += " AND user_id = ?" - params.append(body.user_id) - rows = query(f"SELECT user_id, jpush_rid FROM devices WHERE {conds}", tuple(params)) - if not rows: - raise BizError(3002, "没有已注册推送的设备,请先在 App 登录") - result = push_test_signal([r["jpush_rid"] for r in rows]) - return ok({"devices": len(rows), "jpush": result}) + rows = query( + "SELECT user_id, jpush_rid, is_tester FROM devices " + "WHERE rid_invalid = 0 AND jpush_rid IS NOT NULL" + ) + rids = list(dict.fromkeys(pick_test_devices(rows))) # 去重,同一注册ID只发一次 + if not rids: + raise BizError(3002, "没有专属测试机在线登记,请先在测试机的新版 App 上登录") + result = push_test_signal(rids) + return ok({"devices": len(rids), "jpush": result}) # ---- App 版本管理(APK 上传托管 + 客户端自动更新) ---------------------------- diff --git a/server/app/routers/me.py b/server/app/routers/me.py index c47b163..42b457f 100644 --- a/server/app/routers/me.py +++ b/server/app/routers/me.py @@ -131,6 +131,8 @@ class DeviceIn(BaseModel): brand: str | None = None os_ver: str | None = None jpush_rid: str | None = None + # 专属测试机标记:仅构建时注入 XL_TESTER=1 的客户端上报 1。旧版不带此字段 → 0。 + is_tester: bool = False @router.post("/device") @@ -138,17 +140,19 @@ def register_device(body: DeviceIn, user: dict = Depends(current_user)): # 设备以「最后一次登录」为准:换人同机必须改绑(隐私硬要求,PRD §8.2) execute( """ - INSERT INTO devices(device_id, user_id, brand, os_ver, jpush_rid, rid_invalid, last_seen) - VALUES(?,?,?,?,?,0,?) + INSERT INTO devices(device_id, user_id, brand, os_ver, jpush_rid, rid_invalid, last_seen, is_tester) + VALUES(?,?,?,?,?,?,?,?) ON CONFLICT(device_id) DO UPDATE SET user_id=excluded.user_id, brand=excluded.brand, os_ver=excluded.os_ver, jpush_rid=excluded.jpush_rid, rid_invalid=0, - last_seen=excluded.last_seen + last_seen=excluded.last_seen, + is_tester=excluded.is_tester """, - (body.device_id, user["id"], body.brand, body.os_ver, body.jpush_rid, now_s()), + (body.device_id, user["id"], body.brand, body.os_ver, body.jpush_rid, now_s(), + 1 if body.is_tester else 0), ) return ok({}) diff --git a/server/tests/test_tester_device.py b/server/tests/test_tester_device.py new file mode 100644 index 0000000..4cf927b --- /dev/null +++ b/server/tests/test_tester_device.py @@ -0,0 +1,35 @@ +"""专属测试机:测试信号只发给标记为专属测试的设备,旧版收不到。 + +背景(2026-12-02):测试信号原先发给全部已注册推送的设备,联调时会打扰所有 +用户。新版客户端构建时注入专属标记并在注册设备时上报,服务端据此过滤。 +""" +import pytest + +from app.routers.admin import pick_test_devices + + +def _dev(rid, tester=0): + return {"user_id": 1, "jpush_rid": rid, "is_tester": tester} + + +def test_only_tester_devices_are_picked(): + rows = [_dev("old-phone-rid", 0), _dev("test-phone-rid", 1)] + assert pick_test_devices(rows) == ["test-phone-rid"] + + +def test_no_tester_means_nothing_is_sent(): + """全部是旧版设备时不发(而不是退回"发给所有人")。""" + assert pick_test_devices([_dev("a", 0), _dev("b", 0)]) == [] + + +def test_multiple_testers_all_picked(): + assert pick_test_devices([_dev("a", 1), _dev("b", 0), _dev("c", 1)]) == ["a", "c"] + + +def test_none_flag_treated_as_old_version(): + """历史数据该列为 NULL:视为旧版,绝不误发。""" + assert pick_test_devices([{"user_id": 1, "jpush_rid": "x", "is_tester": None}]) == [] + + +def test_empty_rid_skipped(): + assert pick_test_devices([{"user_id": 1, "jpush_rid": None, "is_tester": 1}]) == []