优化首页公告与轮播图
This commit is contained in:
@@ -3,6 +3,7 @@ package systemconfig
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -29,16 +30,82 @@ func (s *Service) PublishOptions() (*PublishOptionsDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
value, err := s.repo.FindValue(publishOptionsConfigKey)
|
||||
options, err := s.publishOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &options, nil
|
||||
}
|
||||
|
||||
func (s *Service) publishOptions() (PublishOptionsDTO, error) {
|
||||
value, err := s.repo.FindValue(publishOptionsConfigKey)
|
||||
if err != nil {
|
||||
return PublishOptionsDTO{}, err
|
||||
}
|
||||
options := DefaultPublishOptions()
|
||||
if err := json.Unmarshal([]byte(value), &options); err != nil {
|
||||
options = DefaultPublishOptions()
|
||||
}
|
||||
normalizePublishOptions(&options)
|
||||
return &options, nil
|
||||
return options, nil
|
||||
}
|
||||
|
||||
func (s *Service) HomeAnnouncements() (*HomeAnnouncementsDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
items, err := s.homeAnnouncements()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &HomeAnnouncementsDTO{Items: items}, nil
|
||||
}
|
||||
|
||||
func (s *Service) HomeConfig() (*HomeConfigDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
announcements, err := s.homeAnnouncements()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
banners, err := s.homeBanners()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
publishOptions, err := s.publishOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &HomeConfigDTO{
|
||||
Announcements: announcements,
|
||||
Banners: banners,
|
||||
PublishOptions: publishOptions,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) homeAnnouncements() ([]string, error) {
|
||||
value, err := s.repo.FindValue(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() ([]HomeBannerItem, error) {
|
||||
value, err := s.repo.FindValue(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) Update(actorID uint64, key string, req UpdateRequest, meta AuditMeta) (*ConfigDTO, error) {
|
||||
@@ -51,6 +118,43 @@ func (s *Service) Update(actorID uint64, key string, req UpdateRequest, meta Aud
|
||||
return s.repo.Update(actorID, key, req, meta)
|
||||
}
|
||||
|
||||
func normalizeAnnouncements(items []string) []string {
|
||||
normalized := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
item = strings.TrimSpace(item)
|
||||
if item != "" {
|
||||
normalized = append(normalized, item)
|
||||
}
|
||||
}
|
||||
if len(normalized) == 0 {
|
||||
return defaultHomeAnnouncements()
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func normalizeHomeBanners(items []HomeBannerItem) []HomeBannerItem {
|
||||
normalized := make([]HomeBannerItem, 0, len(items))
|
||||
for _, item := range items {
|
||||
item.Eyebrow = strings.TrimSpace(item.Eyebrow)
|
||||
item.Title = strings.TrimSpace(item.Title)
|
||||
item.Badge = strings.TrimSpace(item.Badge)
|
||||
item.Pill = strings.TrimSpace(item.Pill)
|
||||
item.Tone = strings.TrimSpace(item.Tone)
|
||||
item.ImageURL = strings.TrimSpace(item.ImageURL)
|
||||
if item.Title == "" && item.ImageURL == "" {
|
||||
continue
|
||||
}
|
||||
if item.Tone != "green" && item.Tone != "orange" {
|
||||
item.Tone = "blue"
|
||||
}
|
||||
normalized = append(normalized, item)
|
||||
}
|
||||
if len(normalized) == 0 {
|
||||
return defaultHomeBanners()
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func normalizePublishOptions(options *PublishOptionsDTO) {
|
||||
defaults := DefaultPublishOptions()
|
||||
if len(options.ServerOptions) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user