From bea31229ce1d62061b48be4fc230b8cd56e3cb11 Mon Sep 17 00:00:00 2001 From: zhx Date: Wed, 1 Jul 2026 12:04:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=B8=E6=88=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/service/game/baishun_adapter.go | 37 ++++++++++++++++--- .../internal/service/game/service_test.go | 32 ++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/services/game-service/internal/service/game/baishun_adapter.go b/services/game-service/internal/service/game/baishun_adapter.go index d2ef9133..6fe72122 100644 --- a/services/game-service/internal/service/game/baishun_adapter.go +++ b/services/game-service/internal/service/game/baishun_adapter.go @@ -42,10 +42,10 @@ type baishunAdapterConfig struct { TokenTTLSeconds int64 `json:"token_ttl_seconds"` // 以下字段是 NativeBridge.getConfig 需要返回给 H5 的配置,同时也会拼在启动 URL query 中给壳层复用。 - GameMode string `json:"game_mode"` - SceneMode int64 `json:"scene_mode"` - CurrencyIcon string `json:"currency_icon"` - GSP int64 `json:"gsp"` + GameMode baishunGameModeValue `json:"game_mode"` + SceneMode int64 `json:"scene_mode"` + CurrencyIcon string `json:"currency_icon"` + GSP int64 `json:"gsp"` // 可选用户管控字段;配置后会随 get_sstoken/get_user_info 一起返回给百顺。 UserType int `json:"user_type"` @@ -77,7 +77,7 @@ func baishunConfigFromPlatform(value any) baishunAdapterConfig { config.AppChannel = strings.TrimSpace(config.AppChannel) config.UIDMode = strings.ToLower(strings.TrimSpace(config.UIDMode)) config.DefaultLang = strings.TrimSpace(config.DefaultLang) - config.GameMode = strings.TrimSpace(config.GameMode) + config.GameMode = baishunGameModeValue(strings.TrimSpace(string(config.GameMode))) config.CurrencyIcon = strings.TrimSpace(config.CurrencyIcon) config.Extend = strings.TrimSpace(config.Extend) config.LaunchURLTemplate = strings.TrimSpace(config.LaunchURLTemplate) @@ -101,11 +101,36 @@ func (c baishunAdapterConfig) LanguageValue() string { func (c baishunAdapterConfig) GameModeValue() string { if c.GameMode != "" { - return c.GameMode + return string(c.GameMode) } return "3" } +type baishunGameModeValue string + +func (v *baishunGameModeValue) UnmarshalJSON(raw []byte) error { + // 后台适配器配置是自由 JSON,历史上既可能保存 "2",也可能保存 2。 + // 百顺启动 query 最终只需要文本值;在配置入口归一,避免数字类型被 Go string 字段吞掉后误走默认 gameMode=3。 + decoder := json.NewDecoder(strings.NewReader(strings.TrimSpace(string(raw)))) + decoder.UseNumber() + var value any + if err := decoder.Decode(&value); err != nil { + return err + } + switch typed := value.(type) { + case nil: + *v = "" + case string: + *v = baishunGameModeValue(strings.TrimSpace(typed)) + case json.Number: + *v = baishunGameModeValue(strings.TrimSpace(typed.String())) + default: + // 复杂类型不是百顺协议值;按未配置处理,保留默认值兜底,同时不影响同一 JSON 中其它配置解析。 + *v = "" + } + return nil +} + func baishunLaunchBaseURL(game gamedomain.LaunchableGame, config baishunAdapterConfig) string { for _, key := range []string{game.ProviderGameID, game.GameID, strings.ToLower(game.GameID)} { if raw := strings.TrimSpace(config.GameURLs[strings.TrimSpace(key)]); raw != "" { diff --git a/services/game-service/internal/service/game/service_test.go b/services/game-service/internal/service/game/service_test.go index 07d39846..61b0fbdf 100644 --- a/services/game-service/internal/service/game/service_test.go +++ b/services/game-service/internal/service/game/service_test.go @@ -396,6 +396,38 @@ func TestLaunchGameBuildsBaishunURL(t *testing.T) { } } +func TestBaishunConfigGameModeAcceptsNumberAndString(t *testing.T) { + cases := []struct { + name string + raw string + want string + }{ + { + name: "string_value_from_existing_config", + raw: `{"game_mode":"2"}`, + want: "2", + }, + { + name: "numeric_value_from_admin_json_editor", + raw: `{"game_mode":2}`, + want: "2", + }, + { + name: "missing_value_keeps_baishun_default", + raw: `{}`, + want: "3", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + config := baishunConfigFromPlatform(gamedomain.LaunchableGame{AdapterConfigJSON: tc.raw}) + if got := config.GameModeValue(); got != tc.want { + t.Fatalf("baishun gameMode mismatch: got %q want %q raw=%s", got, tc.want, tc.raw) + } + }) + } +} + func TestLaunchGameBuildsVivaGamesURL(t *testing.T) { repo := &fakeRepository{ launchable: gamedomain.LaunchableGame{