2026-04-24 13:04:06 +08:00

62 lines
1.5 KiB
Go

package baishun
import (
"chatapp3-golang/internal/model"
"context"
"net/http"
"strings"
"gorm.io/gorm"
)
const (
baishunProfileProd = "PROD"
baishunProfileTest = "TEST"
)
func normalizeBaishunProfile(profile string) string {
switch strings.ToUpper(strings.TrimSpace(profile)) {
case "", "PROD", "PRODUCT", "PRODUCTION", "ONLINE", "OFFICIAL", "FORMAL":
return baishunProfileProd
case "TEST", "TESTING", "SANDBOX", "DEV":
return baishunProfileTest
default:
return strings.ToUpper(strings.TrimSpace(profile))
}
}
func validateBaishunProfile(profile string) (string, error) {
profile = normalizeBaishunProfile(profile)
switch profile {
case baishunProfileProd, baishunProfileTest:
return profile, nil
default:
return "", NewAppError(http.StatusBadRequest, "profile_invalid", "profile must be PROD or TEST")
}
}
func (s *BaishunService) activeProfile(ctx context.Context, sysOrigin string) (string, error) {
sysOrigin = normalizeAdminSysOrigin(sysOrigin)
var row model.BaishunProviderConfig
err := s.repo.DB.WithContext(ctx).
Where("sys_origin = ? AND active = ?", sysOrigin, true).
Order("update_time DESC").
Limit(1).
First(&row).Error
if err == nil {
return normalizeBaishunProfile(row.Profile), nil
}
if err == gorm.ErrRecordNotFound {
return baishunProfileProd, nil
}
return "", err
}
func (s *BaishunService) requestProfile(ctx context.Context, sysOrigin string, profile string) (string, error) {
if strings.TrimSpace(profile) != "" {
return validateBaishunProfile(profile)
}
return s.activeProfile(ctx, sysOrigin)
}