yumi-golang/internal/router/game_open_routes.go
2026-06-10 15:58:52 +08:00

110 lines
3.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package router
import (
"encoding/json"
"net/http"
"strings"
"chatapp3-golang/internal/config"
"chatapp3-golang/internal/service/gameopen"
"github.com/gin-gonic/gin"
)
// registerGameOpenRoutes 注册统一三方游戏回调接口。
func registerGameOpenRoutes(engine *gin.Engine, cfg config.Config, service *gameopen.GameOpenService) {
if service == nil {
return
}
callbackGroup := engine.Group("/game/open")
callbackGroup.POST("/user-info", func(c *gin.Context) {
raw, _ := c.GetRawData()
var req gameopen.QueryUserRequest
if err := json.Unmarshal(raw, &req); err != nil {
c.JSON(http.StatusOK, gameopen.CallbackResponse{ErrorCode: 4001, ErrorMsg: err.Error()})
return
}
c.JSON(http.StatusOK, service.HandleQueryUser(c.Request.Context(), req, string(raw)))
})
callbackGroup.POST("/update-coin", func(c *gin.Context) {
raw, _ := c.GetRawData()
var req gameopen.UpdateCoinRequest
if err := json.Unmarshal(raw, &req); err != nil {
c.JSON(http.StatusOK, gameopen.CallbackResponse{ErrorCode: 4001, ErrorMsg: err.Error()})
return
}
c.JSON(http.StatusOK, service.HandleUpdateCoin(c.Request.Context(), req, string(raw)))
})
callbackGroup.POST("/supplement", func(c *gin.Context) {
raw, _ := c.GetRawData()
var req gameopen.SupplementRequest
if err := json.Unmarshal(raw, &req); err != nil {
c.JSON(http.StatusOK, gameopen.CallbackResponse{ErrorCode: 4001, ErrorMsg: err.Error()})
return
}
c.JSON(http.StatusOK, service.HandleSupplement(c.Request.Context(), req, string(raw)))
})
registerHotgameCallbackRoutes(engine, service)
internalGroup := engine.Group("/internal/game/open")
internalGroup.Use(internalSecretMiddleware(cfg.HTTP.InternalCallbackSecret))
internalGroup.GET("/info", func(c *gin.Context) {
resp, err := service.GetIntegrationInfo(c.Request.Context(), resolvePublicBaseURL(c, cfg))
if err != nil {
writeError(c, err)
return
}
writeOK(c, resp)
})
}
func registerHotgameCallbackRoutes(engine *gin.Engine, service *gameopen.GameOpenService) {
handleUserInfo := func(c *gin.Context) {
raw, _ := c.GetRawData()
var req gameopen.HotgameGetUserInfoRequest
if err := json.Unmarshal(raw, &req); err != nil {
c.JSON(http.StatusOK, gameopen.CallbackResponse{ErrorCode: 4001, ErrorMsg: err.Error()})
return
}
c.JSON(http.StatusOK, service.HandleHotgameGetUserInfo(c.Request.Context(), req, string(raw)))
}
handleUpdateBalance := func(c *gin.Context) {
raw, _ := c.GetRawData()
var req gameopen.HotgameUpdateBalanceRequest
if err := json.Unmarshal(raw, &req); err != nil {
c.JSON(http.StatusOK, gameopen.CallbackResponse{ErrorCode: 4001, ErrorMsg: err.Error()})
return
}
c.JSON(http.StatusOK, service.HandleHotgameUpdateBalance(c.Request.Context(), req, string(raw)))
}
// 热游文档要求平台直接提供 /getUserInfo 和 /updateBalance同时保留命名空间路径便于网关灰度。
engine.POST("/getUserInfo", handleUserInfo)
engine.POST("/updateBalance", handleUpdateBalance)
hotgameGroup := engine.Group("/game/hotgame")
hotgameGroup.POST("/getUserInfo", handleUserInfo)
hotgameGroup.POST("/updateBalance", handleUpdateBalance)
}
func resolvePublicBaseURL(c *gin.Context, cfg config.Config) string {
if value := strings.TrimRight(strings.TrimSpace(cfg.GameOpen.PublicBaseURL), "/"); value != "" {
return value
}
scheme := "http"
if forwarded := strings.TrimSpace(c.GetHeader("X-Forwarded-Proto")); forwarded != "" {
scheme = forwarded
} else if c.Request.TLS != nil {
scheme = "https"
}
host := strings.TrimSpace(c.GetHeader("X-Forwarded-Host"))
if host == "" {
host = strings.TrimSpace(c.Request.Host)
}
if host == "" {
return ""
}
return scheme + "://" + host
}