refactor(sitecode): 统一各模块分销站点编码解析为公共包

- 新增 internal/sitecode 包:FromRequest 头优先解析、FromQueryThenHeader
  探测接口专用、ExtractHost/IsLocalHost 提取 Host 域名
- 替换 auth/order/listing/chat/notification/withdrawal/wallet/distribution
  八模块 17 处各自内联的解析逻辑,统一优先级与 TrimSpace 行为
- 修复 notification 漏 TrimSpace、探测接口会话头压过显式 query、
  GetPublicSiteInfo 把 localhost Host 当站点域名查库等不一致
- 顺手清理 auth 中无引用的 extractHost 死代码
- gofmt 修复 withdrawal/presenter、chat/file/payment 测试四个历史遗留格式问题
This commit is contained in:
yml2213
2026-09-17 14:54:38 +08:00
parent 69774458b0
commit 8dc21d2757
16 changed files with 236 additions and 119 deletions
@@ -7,6 +7,7 @@ import (
"strings"
"hfb_sys/backend/internal/middleware"
"hfb_sys/backend/internal/sitecode"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
@@ -355,20 +356,13 @@ func (h *Handler) ListPublicSiteListings(c *gin.Context) {
}
func (h *Handler) GetPublicSiteInfo(c *gin.Context) {
code := strings.TrimSpace(c.Query("code"))
if code == "" {
code = strings.TrimSpace(c.Query("site_code"))
}
if code == "" {
code = strings.TrimSpace(c.GetHeader("X-Distribution-Site-Code"))
}
code := sitecode.FromQueryThenHeader(c)
domain := strings.TrimSpace(c.Query("domain"))
if code == "" && domain == "" {
host := c.Request.Host
if colon := strings.Index(host, ":"); colon != -1 {
host = host[:colon]
host := sitecode.ExtractHost(c.Request.Host)
if host != "" && !sitecode.IsLocalHost(host) {
domain = host
}
domain = host
}
info, err := h.service.GetPublicSiteInfo(c.Request.Context(), code, domain)
if err != nil {
@@ -382,14 +376,11 @@ func (h *Handler) GetPublicSiteInfo(c *gin.Context) {
// 仅当域名属于系统中正常运行(active)的分销站点时返回 200,其余情况返回 404,
// 防止任意外部未授权域名解析到该服务器刷爆 Let's Encrypt 证书申请配额。
func (h *Handler) CheckDomain(c *gin.Context) {
domain := strings.ToLower(strings.TrimSpace(c.Query("domain")))
domain := sitecode.ExtractHost(c.Query("domain"))
if domain == "" {
c.String(http.StatusBadRequest, "domain required")
return
}
if colon := strings.Index(domain, ":"); colon != -1 {
domain = domain[:colon]
}
info, err := h.service.GetPublicSiteInfo(c.Request.Context(), "", domain)
if err != nil || info == nil || info.Status != "active" {