128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
package hostcenter
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"chatapp3-golang/internal/integration"
|
|
)
|
|
|
|
func (s *Service) GetAnchorPolicy(ctx context.Context, req AnchorPolicyRequest) (*AnchorPolicyResponse, error) {
|
|
if s.java == nil {
|
|
return nil, NewAppError(http.StatusServiceUnavailable, "java_gateway_unavailable", "java gateway is unavailable")
|
|
}
|
|
|
|
token := trimBearer(req.Token)
|
|
if token == "" {
|
|
return nil, NewAppError(http.StatusUnauthorized, "missing_token", "token is required")
|
|
}
|
|
|
|
credential, err := s.java.AuthenticateToken(ctx, token)
|
|
if err != nil {
|
|
return nil, NewAppError(http.StatusUnauthorized, "invalid_token", err.Error())
|
|
}
|
|
userID := int64(credential.UserID)
|
|
if userID <= 0 {
|
|
return nil, NewAppError(http.StatusUnauthorized, "invalid_token", "token user is invalid")
|
|
}
|
|
|
|
authorization := "Bearer " + token
|
|
profile, _ := s.java.GetUserProfile(ctx, userID)
|
|
userRegion, err := s.java.GetUserRegion(ctx, userID, authorization)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
region := strings.TrimSpace(userRegion.RegionID)
|
|
if region == "" {
|
|
region = strings.TrimSpace(userRegion.RegionCode)
|
|
}
|
|
if region == "" {
|
|
region = strings.TrimSpace(profile.RegionCode)
|
|
}
|
|
if region == "" {
|
|
return nil, NewAppError(http.StatusNotFound, "user_region_not_found", "user region is not found")
|
|
}
|
|
|
|
sysOrigin := normalizeSysOrigin(credential.SysOrigin, profile.OriginSys)
|
|
policyType := normalizePolicyType(req.PolicyType)
|
|
countryCode := strings.TrimSpace(profile.CountryCode)
|
|
|
|
release, err := s.loadReleasePolicy(ctx, sysOrigin, region, policyType, countryCode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp := &AnchorPolicyResponse{
|
|
UserID: userID,
|
|
SysOrigin: sysOrigin,
|
|
Region: region,
|
|
RegionCode: strings.TrimSpace(userRegion.RegionCode),
|
|
CountryCode: countryCode,
|
|
PolicyType: policyType,
|
|
Title: strings.TrimSpace(release.Title),
|
|
Release: release.Release,
|
|
Policy: release.Policy,
|
|
}
|
|
if resp.Title == "" {
|
|
resp.Title = "主播政策"
|
|
}
|
|
resp.Empty = int64(release.ID) == 0 && len(release.Policy) == 0
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *Service) loadReleasePolicy(ctx context.Context, sysOrigin, region, policyType, countryCode string) (integration.TeamPolicyRelease, error) {
|
|
query := integration.TeamPolicyReleaseQuery{
|
|
SysOrigin: sysOrigin,
|
|
Region: region,
|
|
PolicyType: javaPolicyType(policyType),
|
|
CountryCode: countryCode,
|
|
}
|
|
release, err := s.java.GetTeamPolicyRelease(ctx, query)
|
|
if err != nil {
|
|
return integration.TeamPolicyRelease{}, err
|
|
}
|
|
if !isEmptyRelease(release) || strings.TrimSpace(countryCode) == "" {
|
|
return release, nil
|
|
}
|
|
|
|
query.CountryCode = ""
|
|
return s.java.GetTeamPolicyRelease(ctx, query)
|
|
}
|
|
|
|
func normalizeSysOrigin(values ...string) string {
|
|
for _, value := range values {
|
|
if trimmed := strings.ToUpper(strings.TrimSpace(value)); trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return defaultSysOrigin
|
|
}
|
|
|
|
func normalizePolicyType(policyType string) string {
|
|
if strings.EqualFold(strings.TrimSpace(policyType), diamondPolicyType) {
|
|
return diamondPolicyType
|
|
}
|
|
return defaultPolicyType
|
|
}
|
|
|
|
func javaPolicyType(policyType string) string {
|
|
if policyType == diamondPolicyType {
|
|
return diamondPolicyType
|
|
}
|
|
return defaultPolicyType
|
|
}
|
|
|
|
func isEmptyRelease(release integration.TeamPolicyRelease) bool {
|
|
return int64(release.ID) == 0 && strings.TrimSpace(release.Title) == "" && len(release.Policy) == 0
|
|
}
|
|
|
|
func trimBearer(token string) string {
|
|
token = strings.TrimSpace(token)
|
|
if len(token) >= 7 && strings.EqualFold(token[:7], "Bearer ") {
|
|
return strings.TrimSpace(token[7:])
|
|
}
|
|
return token
|
|
}
|