feat: support baishun app name in golang bridge

This commit is contained in:
hy001 2026-04-15 21:22:15 +08:00
parent ca828818cf
commit f396a19298
2 changed files with 154 additions and 147 deletions

View File

@ -7,54 +7,56 @@ import (
) )
type Config struct { type Config struct {
ListenAddr string ListenAddr string
PublicBaseURL string PublicBaseURL string
MySQLDSN string MySQLDSN string
RedisAddr string RedisAddr string
RedisPassword string RedisPassword string
RedisDB int RedisDB int
AutoMigrate bool AutoMigrate bool
JavaAuthBaseURL string JavaAuthBaseURL string
JavaDeviceBaseURL string JavaDeviceBaseURL string
JavaAppBaseURL string JavaAppBaseURL string
JavaBridgeBaseURL string JavaBridgeBaseURL string
JavaOtherBaseURL string JavaOtherBaseURL string
JavaWalletBaseURL string JavaWalletBaseURL string
BaishunPlatformBaseURL string BaishunPlatformBaseURL string
BaishunAppID int64 BaishunAppID int64
BaishunAppChannel string BaishunAppName string
BaishunAppKey string BaishunAppChannel string
BaishunGSP int BaishunAppKey string
BaishunGSP int
BaishunLaunchCodeTTLSeconds int BaishunLaunchCodeTTLSeconds int
BaishunSSTokenTTLSeconds int BaishunSSTokenTTLSeconds int
InternalCallbackSecret string InternalCallbackSecret string
Timeout time.Duration Timeout time.Duration
} }
func Load() Config { func Load() Config {
return Config{ return Config{
ListenAddr: getEnv("INVITE_HTTP_ADDR", ":2900"), ListenAddr: getEnv("INVITE_HTTP_ADDR", ":2900"),
PublicBaseURL: getEnv("INVITE_PUBLIC_BASE_URL", "http://localhost:2900"), PublicBaseURL: getEnv("INVITE_PUBLIC_BASE_URL", "http://localhost:2900"),
MySQLDSN: getEnv("INVITE_MYSQL_DSN", "root:root@tcp(127.0.0.1:3306)/chatapp3?charset=utf8mb4&parseTime=True&loc=Local"), MySQLDSN: getEnv("INVITE_MYSQL_DSN", "root:root@tcp(127.0.0.1:3306)/chatapp3?charset=utf8mb4&parseTime=True&loc=Local"),
RedisAddr: getEnv("INVITE_REDIS_ADDR", "127.0.0.1:6379"), RedisAddr: getEnv("INVITE_REDIS_ADDR", "127.0.0.1:6379"),
RedisPassword: getEnv("INVITE_REDIS_PASSWORD", ""), RedisPassword: getEnv("INVITE_REDIS_PASSWORD", ""),
RedisDB: getEnvInt("INVITE_REDIS_DB", 0), RedisDB: getEnvInt("INVITE_REDIS_DB", 0),
AutoMigrate: getEnvBool("INVITE_AUTO_MIGRATE", false), AutoMigrate: getEnvBool("INVITE_AUTO_MIGRATE", false),
JavaAuthBaseURL: getEnv("INVITE_JAVA_AUTH_BASE_URL", "http://127.0.0.1:2100"), JavaAuthBaseURL: getEnv("INVITE_JAVA_AUTH_BASE_URL", "http://127.0.0.1:2100"),
JavaDeviceBaseURL: getEnv("INVITE_JAVA_DEVICE_BASE_URL", "http://127.0.0.1:2400"), JavaDeviceBaseURL: getEnv("INVITE_JAVA_DEVICE_BASE_URL", "http://127.0.0.1:2400"),
JavaAppBaseURL: getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400"), JavaAppBaseURL: getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400"),
JavaBridgeBaseURL: getEnv("INVITE_JAVA_BRIDGE_BASE_URL", "http://127.0.0.1:2200"), JavaBridgeBaseURL: getEnv("INVITE_JAVA_BRIDGE_BASE_URL", "http://127.0.0.1:2200"),
JavaOtherBaseURL: getEnv("GAME_JAVA_OTHER_BASE_URL", getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400")), JavaOtherBaseURL: getEnv("GAME_JAVA_OTHER_BASE_URL", getEnv("INVITE_JAVA_APP_BASE_URL", "http://127.0.0.1:2400")),
JavaWalletBaseURL: getEnv("GAME_JAVA_WALLET_BASE_URL", "http://127.0.0.1:2300"), JavaWalletBaseURL: getEnv("GAME_JAVA_WALLET_BASE_URL", "http://127.0.0.1:2300"),
BaishunPlatformBaseURL: getEnv("BAISHUN_PLATFORM_BASE_URL", ""), BaishunPlatformBaseURL: getEnv("BAISHUN_PLATFORM_BASE_URL", ""),
BaishunAppID: getEnvInt64("BAISHUN_APP_ID", 0), BaishunAppID: getEnvInt64("BAISHUN_APP_ID", 0),
BaishunAppChannel: getEnv("BAISHUN_APP_CHANNEL", ""), BaishunAppName: getEnv("BAISHUN_APP_NAME", ""),
BaishunAppKey: getEnv("BAISHUN_APP_KEY", ""), BaishunAppChannel: getEnv("BAISHUN_APP_CHANNEL", ""),
BaishunGSP: getEnvInt("BAISHUN_GSP", 101), BaishunAppKey: getEnv("BAISHUN_APP_KEY", ""),
BaishunGSP: getEnvInt("BAISHUN_GSP", 101),
BaishunLaunchCodeTTLSeconds: getEnvInt("BAISHUN_LAUNCH_CODE_TTL_SECONDS", 300), BaishunLaunchCodeTTLSeconds: getEnvInt("BAISHUN_LAUNCH_CODE_TTL_SECONDS", 300),
BaishunSSTokenTTLSeconds: getEnvInt("BAISHUN_SS_TOKEN_TTL_SECONDS", 86400), BaishunSSTokenTTLSeconds: getEnvInt("BAISHUN_SS_TOKEN_TTL_SECONDS", 86400),
InternalCallbackSecret: getEnv("GAME_INTERNAL_CALLBACK_SECRET", ""), InternalCallbackSecret: getEnv("GAME_INTERNAL_CALLBACK_SECRET", ""),
Timeout: time.Duration(getEnvInt("INVITE_HTTP_TIMEOUT_SECONDS", 8)) * time.Second, Timeout: time.Duration(getEnvInt("INVITE_HTTP_TIMEOUT_SECONDS", 8)) * time.Second,
} }
} }

View File

@ -89,14 +89,14 @@ type RoomGameListResponse struct {
} }
type BaishunRoomStateResponse struct { type BaishunRoomStateResponse struct {
RoomID string `json:"roomId"` RoomID string `json:"roomId"`
State string `json:"state"` State string `json:"state"`
GameSessionID string `json:"gameSessionId,omitempty"` GameSessionID string `json:"gameSessionId,omitempty"`
CurrentGameID string `json:"currentGameId,omitempty"` CurrentGameID string `json:"currentGameId,omitempty"`
CurrentVendorGameID int `json:"currentVendorGameId,omitempty"` CurrentVendorGameID int `json:"currentVendorGameId,omitempty"`
CurrentGameName string `json:"currentGameName,omitempty"` CurrentGameName string `json:"currentGameName,omitempty"`
CurrentGameCover string `json:"currentGameCover,omitempty"` CurrentGameCover string `json:"currentGameCover,omitempty"`
HostUserID int64 `json:"hostUserId,omitempty"` HostUserID int64 `json:"hostUserId,omitempty"`
} }
type BaishunLaunchAppRequest struct { type BaishunLaunchAppRequest struct {
@ -118,15 +118,16 @@ type BaishunGameConfig struct {
} }
type BaishunBridgeConfig struct { type BaishunBridgeConfig struct {
AppChannel string `json:"appChannel"` AppName string `json:"appName"`
AppID int64 `json:"appId"` AppChannel string `json:"appChannel"`
UserID string `json:"userId"` AppID int64 `json:"appId"`
Code string `json:"code"` UserID string `json:"userId"`
RoomID string `json:"roomId"` Code string `json:"code"`
GameMode string `json:"gameMode"` RoomID string `json:"roomId"`
Language string `json:"language"` GameMode string `json:"gameMode"`
GSP int `json:"gsp"` Language string `json:"language"`
GameConfig BaishunGameConfig `json:"gameConfig"` GSP int `json:"gsp"`
GameConfig BaishunGameConfig `json:"gameConfig"`
} }
type BaishunLaunchEntry struct { type BaishunLaunchEntry struct {
@ -192,22 +193,22 @@ type BaishunUpdateTokenRequest struct {
} }
type BaishunChangeBalanceRequest struct { type BaishunChangeBalanceRequest struct {
AppID int64 `json:"app_id"` AppID int64 `json:"app_id"`
UserID string `json:"user_id"` UserID string `json:"user_id"`
SSToken string `json:"ss_token"` SSToken string `json:"ss_token"`
CurrencyDiff int64 `json:"currency_diff"` CurrencyDiff int64 `json:"currency_diff"`
DiffMsg string `json:"diff_msg"` DiffMsg string `json:"diff_msg"`
GameID int `json:"game_id"` GameID int `json:"game_id"`
GameRoundID string `json:"game_round_id"` GameRoundID string `json:"game_round_id"`
RoomID string `json:"room_id"` RoomID string `json:"room_id"`
ChangeTimeAt int64 `json:"change_time_at"` ChangeTimeAt int64 `json:"change_time_at"`
OrderID string `json:"order_id"` OrderID string `json:"order_id"`
Extend string `json:"extend"` Extend string `json:"extend"`
MsgType string `json:"msg_type"` MsgType string `json:"msg_type"`
CurrencyType *int `json:"currency_type"` CurrencyType *int `json:"currency_type"`
Signature string `json:"signature"` Signature string `json:"signature"`
SignatureNonce string `json:"signature_nonce"` SignatureNonce string `json:"signature_nonce"`
Timestamp int64 `json:"timestamp"` Timestamp int64 `json:"timestamp"`
} }
type BaishunBalanceInfoRequest struct { type BaishunBalanceInfoRequest struct {
@ -234,39 +235,39 @@ type baishunBalanceInfoResponse struct {
} }
type gameListRow struct { type gameListRow struct {
ConfigID int64 ConfigID int64
SysOrigin string SysOrigin string
InternalGameID string InternalGameID string
Name string Name string
Category string Category string
Cover string Cover string
Sort int64 Sort int64
FullScreen bool FullScreen bool
GameModeRaw string GameModeRaw string
VendorType string VendorType string
VendorGameID string VendorGameID string
LaunchMode string LaunchMode string
ExtPackageVersion string ExtPackageVersion string
ExtPreviewURL string ExtPreviewURL string
PackageURL string PackageURL string
ExtOrientation *int ExtOrientation *int
ExtSafeHeight int ExtSafeHeight int
CurrencyIcon string CurrencyIcon string
ExtGSP string ExtGSP string
CatalogName string CatalogName string
CatalogCover string CatalogCover string
CatalogPreviewURL string CatalogPreviewURL string
CatalogDownloadURL string CatalogDownloadURL string
CatalogPackageVersion string CatalogPackageVersion string
CatalogOrientation *int CatalogOrientation *int
CatalogSafeHeight int CatalogSafeHeight int
CatalogStatus string CatalogStatus string
} }
type baishunPlatformResponse struct { type baishunPlatformResponse struct {
Code int `json:"code"` Code int `json:"code"`
Message string `json:"message"` Message string `json:"message"`
Data []baishunPlatformGame `json:"data"` Data []baishunPlatformGame `json:"data"`
} }
type baishunPlatformOneGameResponse struct { type baishunPlatformOneGameResponse struct {
@ -276,7 +277,7 @@ type baishunPlatformOneGameResponse struct {
} }
type baishunPlatformGame struct { type baishunPlatformGame struct {
GameID int `json:"game_id"` GameID int `json:"game_id"`
Name string `json:"name"` Name string `json:"name"`
PreviewURL string `json:"preview_url"` PreviewURL string `json:"preview_url"`
GameVersion string `json:"game_version"` GameVersion string `json:"game_version"`
@ -387,19 +388,19 @@ func (s *BaishunService) LaunchGame(ctx context.Context, user AuthUser, req Bais
} }
roomState := model.BaishunRoomState{ roomState := model.BaishunRoomState{
ID: roomStateID, ID: roomStateID,
SysOrigin: user.SysOrigin, SysOrigin: user.SysOrigin,
RoomID: req.RoomID, RoomID: req.RoomID,
HostUserID: user.UserID, HostUserID: user.UserID,
CurrentGameID: row.InternalGameID, CurrentGameID: row.InternalGameID,
CurrentGameName: row.name(), CurrentGameName: row.name(),
CurrentGameCover: row.cover(), CurrentGameCover: row.cover(),
GameSessionID: session.GameSessionID, GameSessionID: session.GameSessionID,
LaunchUserID: &user.UserID, LaunchUserID: &user.UserID,
State: baishunRoomStatePlaying, State: baishunRoomStatePlaying,
StartTime: &now, StartTime: &now,
CreateTime: now, CreateTime: now,
UpdateTime: now, UpdateTime: now,
} }
vendorGameID := row.vendorGameIDInt() vendorGameID := row.vendorGameIDInt()
roomState.CurrentVendorGameID = &vendorGameID roomState.CurrentVendorGameID = &vendorGameID
@ -435,6 +436,7 @@ func (s *BaishunService) LaunchGame(ctx context.Context, user AuthUser, req Bais
SafeHeight: row.safeHeight(), SafeHeight: row.safeHeight(),
}, },
BridgeConfig: BaishunBridgeConfig{ BridgeConfig: BaishunBridgeConfig{
AppName: s.cfg.BaishunAppName,
AppChannel: defaultIfBlank(s.cfg.BaishunAppChannel, "skychat"), AppChannel: defaultIfBlank(s.cfg.BaishunAppChannel, "skychat"),
AppID: s.cfg.BaishunAppID, AppID: s.cfg.BaishunAppID,
UserID: strconv.FormatInt(user.UserID, 10), UserID: strconv.FormatInt(user.UserID, 10),
@ -449,14 +451,14 @@ func (s *BaishunService) LaunchGame(ctx context.Context, user AuthUser, req Bais
}, },
}, },
RoomState: BaishunRoomStateResponse{ RoomState: BaishunRoomStateResponse{
RoomID: req.RoomID, RoomID: req.RoomID,
State: baishunRoomStatePlaying, State: baishunRoomStatePlaying,
GameSessionID: session.GameSessionID, GameSessionID: session.GameSessionID,
CurrentGameID: row.InternalGameID, CurrentGameID: row.InternalGameID,
CurrentVendorGameID: row.vendorGameIDInt(), CurrentVendorGameID: row.vendorGameIDInt(),
CurrentGameName: row.name(), CurrentGameName: row.name(),
CurrentGameCover: row.cover(), CurrentGameCover: row.cover(),
HostUserID: user.UserID, HostUserID: user.UserID,
}, },
}, nil }, nil
} }
@ -483,14 +485,14 @@ func (s *BaishunService) CloseGame(ctx context.Context, user AuthUser, req Baish
Model(&model.BaishunRoomState{}). Model(&model.BaishunRoomState{}).
Where("sys_origin = ? AND room_id = ?", user.SysOrigin, req.RoomID). Where("sys_origin = ? AND room_id = ?", user.SysOrigin, req.RoomID).
Updates(map[string]any{ Updates(map[string]any{
"state": baishunRoomStateIdle, "state": baishunRoomStateIdle,
"end_time": now, "end_time": now,
"game_session_id": "", "game_session_id": "",
"current_game_id": "", "current_game_id": "",
"current_vendor_game_id": nil, "current_vendor_game_id": nil,
"current_game_name": "", "current_game_name": "",
"current_game_cover": "", "current_game_cover": "",
"update_time": now, "update_time": now,
}).Error; err != nil { }).Error; err != nil {
return nil, err return nil, err
} }
@ -1017,6 +1019,9 @@ func (s *BaishunService) fetchPlatformGames(ctx context.Context) ([]baishunPlatf
"signature_nonce": nonce, "signature_nonce": nonce,
"timestamp": timestamp, "timestamp": timestamp,
} }
if strings.TrimSpace(s.cfg.BaishunAppName) != "" {
payload["app_name"] = s.cfg.BaishunAppName
}
body, _ := json.Marshal(payload) body, _ := json.Marshal(payload)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(s.cfg.BaishunPlatformBaseURL, "/")+"/v1/api/gamelist", bytes.NewReader(body)) req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(s.cfg.BaishunPlatformBaseURL, "/")+"/v1/api/gamelist", bytes.NewReader(body))
if err != nil { if err != nil {
@ -1143,24 +1148,24 @@ func (s *BaishunService) persistOrderRecord(ctx context.Context, sysOrigin strin
return return
} }
record := model.BaishunOrderIdempotency{ record := model.BaishunOrderIdempotency{
ID: id, ID: id,
SysOrigin: sysOrigin, SysOrigin: sysOrigin,
OrderID: req.OrderID, OrderID: req.OrderID,
GameRoundID: req.GameRoundID, GameRoundID: req.GameRoundID,
RoomID: req.RoomID, RoomID: req.RoomID,
UserID: parseInt64Default(req.UserID), UserID: parseInt64Default(req.UserID),
VendorGameID: req.GameID, VendorGameID: req.GameID,
CurrencyDiff: req.CurrencyDiff, CurrencyDiff: req.CurrencyDiff,
DiffMsg: req.DiffMsg, DiffMsg: req.DiffMsg,
MsgType: req.MsgType, MsgType: req.MsgType,
CurrencyType: req.CurrencyType, CurrencyType: req.CurrencyType,
WalletEventID: walletEventID, WalletEventID: walletEventID,
WalletAssetRecordID: assetRecordID, WalletAssetRecordID: assetRecordID,
Status: status, Status: status,
RequestJSON: requestJSON, RequestJSON: requestJSON,
ResponseJSON: responseJSON, ResponseJSON: responseJSON,
CreateTime: time.Now(), CreateTime: time.Now(),
UpdateTime: time.Now(), UpdateTime: time.Now(),
} }
_ = s.repo.DB.WithContext(ctx).Clauses(clause.OnConflict{ _ = s.repo.DB.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "sys_origin"}, {Name: "order_id"}}, Columns: []clause.Column{{Name: "sys_origin"}, {Name: "order_id"}},