分站可单独配置回收、合作反馈和客服二维码,未配置时继承主站;Logo、图标和轮播支持直接上传。 Co-authored-by: Cursor <cursoragent@cursor.com>
150 lines
4.1 KiB
Go
150 lines
4.1 KiB
Go
package systemconfig
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"hfb_sys/backend/internal/model"
|
|
)
|
|
|
|
func (s *Service) HomeAnnouncements(ctx context.Context) (*HomeAnnouncementsDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
items, err := s.homeAnnouncements(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &HomeAnnouncementsDTO{Items: items}, nil
|
|
}
|
|
|
|
func (s *Service) HomeConfig(ctx context.Context, siteCode string) (*HomeConfigDTO, error) {
|
|
if s.repo == nil {
|
|
return nil, ErrDependencyUnavailable
|
|
}
|
|
announcements, err := s.homeAnnouncements(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
banners, err := s.homeBanners(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
awRecycle, err := s.awRecycle(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cooperationFeedback, err := s.cooperationFeedback(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
publishOptions, err := s.publishOptions(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
config := &HomeConfigDTO{
|
|
Announcements: announcements,
|
|
Banners: banners,
|
|
PublishOptions: publishOptions,
|
|
AWRecycle: awRecycle,
|
|
CooperationFeedback: cooperationFeedback,
|
|
}
|
|
s.applySiteHomeOverrides(ctx, siteCode, config)
|
|
return config, nil
|
|
}
|
|
|
|
// applySiteHomeOverrides 用分站自己的首页入口覆盖主站配置。字段为空表示继续继承主站。
|
|
func (s *Service) applySiteHomeOverrides(ctx context.Context, siteCode string, config *HomeConfigDTO) {
|
|
siteCode = strings.TrimSpace(siteCode)
|
|
if s == nil || s.repo == nil || s.repo.db == nil || config == nil || siteCode == "" {
|
|
return
|
|
}
|
|
var site model.DistributionSite
|
|
err := s.repo.db.WithContext(ctx).
|
|
Select("aw_recycle_json", "cooperation_feedback_json").
|
|
Where("site_code = ? AND status = ?", siteCode, "active").
|
|
First(&site).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
if item, ok := parseAWRecycleOverride(site.AWRecycleJSON); ok {
|
|
config.AWRecycle = item
|
|
}
|
|
if item, ok := parseCooperationFeedbackOverride(site.CooperationFeedbackJSON); ok {
|
|
config.CooperationFeedback = item
|
|
}
|
|
}
|
|
|
|
func parseAWRecycleOverride(raw string) (AWRecycleConfig, bool) {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return AWRecycleConfig{}, false
|
|
}
|
|
var item AWRecycleConfig
|
|
if err := json.Unmarshal([]byte(raw), &item); err != nil {
|
|
return AWRecycleConfig{}, false
|
|
}
|
|
return normalizeAWRecycle(item), true
|
|
}
|
|
|
|
func parseCooperationFeedbackOverride(raw string) (CooperationFeedbackConfig, bool) {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return CooperationFeedbackConfig{}, false
|
|
}
|
|
var item CooperationFeedbackConfig
|
|
if err := json.Unmarshal([]byte(raw), &item); err != nil {
|
|
return CooperationFeedbackConfig{}, false
|
|
}
|
|
return normalizeCooperationFeedback(item), true
|
|
}
|
|
|
|
func (s *Service) homeAnnouncements(ctx context.Context) ([]string, error) {
|
|
value, err := s.repo.FindValue(ctx, homeAnnouncementsConfigKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := defaultHomeAnnouncements()
|
|
if err := json.Unmarshal([]byte(value), &items); err != nil {
|
|
items = defaultHomeAnnouncements()
|
|
}
|
|
return normalizeAnnouncements(items), nil
|
|
}
|
|
|
|
func (s *Service) homeBanners(ctx context.Context) ([]HomeBannerItem, error) {
|
|
value, err := s.repo.FindValue(ctx, homeBannersConfigKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := defaultHomeBanners()
|
|
if err := json.Unmarshal([]byte(value), &items); err != nil {
|
|
items = defaultHomeBanners()
|
|
}
|
|
return normalizeHomeBanners(items), nil
|
|
}
|
|
|
|
func (s *Service) awRecycle(ctx context.Context) (AWRecycleConfig, error) {
|
|
value, err := s.repo.FindValue(ctx, awRecycleConfigKey)
|
|
if err != nil {
|
|
return defaultAWRecycle(), err
|
|
}
|
|
item := defaultAWRecycle()
|
|
if err := json.Unmarshal([]byte(value), &item); err != nil {
|
|
item = defaultAWRecycle()
|
|
}
|
|
return normalizeAWRecycle(item), nil
|
|
}
|
|
|
|
func (s *Service) cooperationFeedback(ctx context.Context) (CooperationFeedbackConfig, error) {
|
|
value, err := s.repo.FindValue(ctx, cooperationFeedbackConfigKey)
|
|
if err != nil {
|
|
return defaultCooperationFeedback(), err
|
|
}
|
|
item := defaultCooperationFeedback()
|
|
if err := json.Unmarshal([]byte(value), &item); err != nil {
|
|
item = defaultCooperationFeedback()
|
|
}
|
|
return normalizeCooperationFeedback(item), nil
|
|
}
|