From 6b55694c4315adf4d43cff57a0ab5e7b2268131e Mon Sep 17 00:00:00 2001 From: lingniu Date: Wed, 1 Jul 2026 22:34:47 +0800 Subject: [PATCH] fix: resolve jt808 phone without leading zero --- .../internal/identity/resolver.go | 3 +++ .../internal/identity/resolver_test.go | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/go/vehicle-gateway/internal/identity/resolver.go b/go/vehicle-gateway/internal/identity/resolver.go index 1f569dab..a8dad1cf 100644 --- a/go/vehicle-gateway/internal/identity/resolver.go +++ b/go/vehicle-gateway/internal/identity/resolver.go @@ -105,6 +105,9 @@ func CandidateKeys(env envelope.FrameEnvelope) []CandidateKey { out = append(out, CandidateKey{Column: column, Value: value}) } add("phone", env.Phone) + if trimmed := strings.TrimLeft(strings.TrimSpace(env.Phone), "0"); trimmed != "" { + add("phone", trimmed) + } add("device_id", env.DeviceID) add("plate", env.Plate) add("vin", env.VehicleKeyHint) diff --git a/go/vehicle-gateway/internal/identity/resolver_test.go b/go/vehicle-gateway/internal/identity/resolver_test.go index 2bbf1960..285f8199 100644 --- a/go/vehicle-gateway/internal/identity/resolver_test.go +++ b/go/vehicle-gateway/internal/identity/resolver_test.go @@ -21,7 +21,26 @@ func TestCandidateKeysPrioritizesPhoneDevicePlate(t *testing.T) { for _, key := range keys { got = append(got, key.Column+"="+key.Value) } - want := []string{"phone=013307795425", "device_id=D1", "plate=豫A12345", "vin=D1"} + want := []string{"phone=013307795425", "phone=13307795425", "device_id=D1", "plate=豫A12345", "vin=D1"} + if len(got) != len(want) { + t.Fatalf("candidate count = %d got=%#v", len(got), got) + } + for i := range want { + if got[i] != want[i] { + t.Fatalf("candidate[%d] = %q, want %q", i, got[i], want[i]) + } + } +} + +func TestCandidateKeysAddsTrimmedPhoneVariant(t *testing.T) { + keys := CandidateKeys(envelope.FrameEnvelope{ + Phone: "013079963379", + }) + got := []string{} + for _, key := range keys { + got = append(got, key.Column+"="+key.Value) + } + want := []string{"phone=013079963379", "phone=13079963379"} if len(got) != len(want) { t.Fatalf("candidate count = %d got=%#v", len(got), got) } @@ -66,6 +85,9 @@ func TestMySQLResolverFallsBackToDeviceID(t *testing.T) { mock.ExpectQuery("SELECT vin FROM vehicle_identity_binding WHERE phone = \\?"). WithArgs("013307795425"). WillReturnError(sql.ErrNoRows) + mock.ExpectQuery("SELECT vin FROM vehicle_identity_binding WHERE phone = \\?"). + WithArgs("13307795425"). + WillReturnError(sql.ErrNoRows) mock.ExpectQuery("SELECT vin FROM vehicle_identity_binding WHERE device_id = \\?"). WithArgs("D1"). WillReturnRows(sqlmock.NewRows([]string{"vin"}).AddRow("LNBVIN00000000002"))