feat(distribution): 公告中心支持分站自定义
公告中心(前台公告列表/详情)此前完全没有站点概念,分站访客只能看到主站公告。 本次按「默认继承主站、站长可切仅本站」的口径支持分站自定义。 数据与口径: - 新增 announcements.distribution_site_id(0=主站),前台按站点隔离 - 新增 distribution_sites.announcement_mode:inherit(默认,主站+本站)/ own_only(仅本站) 后端: - 公告模块新增 ResolvePublicSiteScope:主站 [0]、inherit [0, siteID]、own_only [siteID], 站点编码无效/已停用回落主站;列表与详情都按该范围过滤,详情不再允许跨站按 ID 读取 - 管理端公告列表支持按站点筛选(site_id:不传=全部,0=主站,>0=分站)并带出站点名 - 分销模块新增本站公告 CRUD(distribution/announcement.go):强制 distribution_site_id=本站, 先过 ensureSiteAccess,跨站更新/删除/上下架一律 ErrAnnouncementNotFound - UpdateSiteBranding 支持 announcement_mode 切换(非法值报错,读侧非法历史值回落 inherit) - 新增权限 distribution:announcement_view|manage 并授予 super_admin/ops/finance, 站长开通时继承 ops 角色即自动获得 - 路由:站长侧 /api/distribution/announcements*,平台侧 /api/admin/distribution/announcements* 前端: - 分销后台新增「公告中心」页:来源开关(继承主站+本站 / 仅本站)、本站公告列表与 新建/编辑/发布/下架/归档/恢复/删除,空态按来源模式给出引导 - 平台后台公告页新增「站点」列与筛选,新建公告可选择归属站点(主站或指定分站代发) - 前台 PC/移动无需改动:请求已带 X-Distribution-Site-Code,后端按站点隔离,页面已有空态 测试: - 公告模块:主站/继承/仅本站三种可见范围、跨站详情拦截、无效站点回落、管理端站点筛选 - 分销模块:本站公告 CRUD 与跨站防护、公告来源开关校验与非法值容错
This commit is contained in:
@@ -0,0 +1,235 @@
|
||||
package distribution
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 分站公告中心:站长在分销后台自助维护本站公告。
|
||||
//
|
||||
// 站点隔离口径:所有写操作与列表查询都强制 distribution_site_id = siteID,
|
||||
// 且先通过 ensureSiteAccess 校验当前管理员对该站点的访问权,避免跨站读写。
|
||||
// 前台展示范围由站点 announcement_mode 决定(见 announcement 模块 ResolvePublicSiteScope)。
|
||||
|
||||
// normalizeAnnouncementMode 校验公告中心来源模式,空值按默认 inherit 处理。
|
||||
func normalizeAnnouncementMode(value string) (string, error) {
|
||||
switch strings.TrimSpace(value) {
|
||||
case "", model.AnnouncementModeInherit:
|
||||
return model.AnnouncementModeInherit, nil
|
||||
case model.AnnouncementModeOwnOnly:
|
||||
return model.AnnouncementModeOwnOnly, nil
|
||||
default:
|
||||
return "", ErrInvalidAnnouncementMode
|
||||
}
|
||||
}
|
||||
|
||||
// ListSiteAnnouncements 分页查询本站公告(含草稿/归档,供站长管理)。
|
||||
func (r *Repository) ListSiteAnnouncements(ctx context.Context, adminID, siteID uint64, status string, page, pageSize int) (*SiteAnnouncementPage, error) {
|
||||
if siteID == 0 {
|
||||
return nil, ErrSiteNotFound
|
||||
}
|
||||
if err := r.ensureSiteAccess(ctx, adminID, siteID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
if pageSize > 100 {
|
||||
pageSize = 100
|
||||
}
|
||||
|
||||
base := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("distribution_site_id = ?", siteID)
|
||||
if s := strings.TrimSpace(status); s != "" {
|
||||
base = base.Where("status = ?", s)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := base.Session(&gorm.Session{}).Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rows []model.Announcement
|
||||
if err := base.Session(&gorm.Session{}).
|
||||
Order("is_pinned DESC, priority DESC, id DESC").
|
||||
Offset((page - 1) * pageSize).
|
||||
Limit(pageSize).
|
||||
Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items := make([]SiteAnnouncementDTO, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
items = append(items, toSiteAnnouncementDTO(row))
|
||||
}
|
||||
return &SiteAnnouncementPage{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
|
||||
}
|
||||
|
||||
// CreateSiteAnnouncement 在指定站点下新建公告(草稿)。
|
||||
func (r *Repository) CreateSiteAnnouncement(ctx context.Context, adminID, siteID uint64, req CreateSiteAnnouncementRequest) (*SiteAnnouncementDTO, error) {
|
||||
if siteID == 0 {
|
||||
return nil, ErrSiteNotFound
|
||||
}
|
||||
if err := r.ensureSiteAccess(ctx, adminID, siteID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
createdBy := adminID
|
||||
row := model.Announcement{
|
||||
DistributionSiteID: siteID,
|
||||
Title: strings.TrimSpace(req.Title),
|
||||
Content: req.Content,
|
||||
Category: strings.TrimSpace(req.Category),
|
||||
Priority: req.Priority,
|
||||
IsPinned: req.IsPinned,
|
||||
IsImportant: req.IsImportant,
|
||||
Status: "draft",
|
||||
CreatedBy: &createdBy,
|
||||
}
|
||||
if row.Category == "" {
|
||||
row.Category = "notice"
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Create(&row).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := toSiteAnnouncementDTO(row)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// UpdateSiteAnnouncement 更新本站公告内容。
|
||||
func (r *Repository) UpdateSiteAnnouncement(ctx context.Context, adminID, siteID, id uint64, req UpdateSiteAnnouncementRequest) (*SiteAnnouncementDTO, error) {
|
||||
if siteID == 0 {
|
||||
return nil, ErrSiteNotFound
|
||||
}
|
||||
if err := r.ensureSiteAccess(ctx, adminID, siteID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row, err := r.findSiteAnnouncement(ctx, siteID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updates := map[string]any{
|
||||
"priority": req.Priority,
|
||||
"is_pinned": req.IsPinned,
|
||||
"is_important": req.IsImportant,
|
||||
}
|
||||
if title := strings.TrimSpace(req.Title); title != "" {
|
||||
updates["title"] = title
|
||||
}
|
||||
if req.Content != "" {
|
||||
updates["content"] = req.Content
|
||||
}
|
||||
if category := strings.TrimSpace(req.Category); category != "" {
|
||||
updates["category"] = category
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", row.ID).Updates(updates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updated, err := r.findSiteAnnouncement(ctx, siteID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := toSiteAnnouncementDTO(*updated)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// UpdateSiteAnnouncementStatus 站点公告上下架:publish=发布,archive=归档,unarchive=取消归档。
|
||||
func (r *Repository) UpdateSiteAnnouncementStatus(ctx context.Context, adminID, siteID, id uint64, action string) (*SiteAnnouncementDTO, error) {
|
||||
if siteID == 0 {
|
||||
return nil, ErrSiteNotFound
|
||||
}
|
||||
if err := r.ensureSiteAccess(ctx, adminID, siteID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row, err := r.findSiteAnnouncement(ctx, siteID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
updates := map[string]any{}
|
||||
switch strings.TrimSpace(action) {
|
||||
case "publish":
|
||||
updates["status"] = "published"
|
||||
updates["published_at"] = now
|
||||
case "archive":
|
||||
updates["status"] = "archived"
|
||||
case "unarchive":
|
||||
updates["status"] = "published"
|
||||
updates["published_at"] = gorm.Expr("COALESCE(published_at, ?)", now)
|
||||
default:
|
||||
return nil, ErrInvalidListingStatus
|
||||
}
|
||||
if err := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", row.ID).Updates(updates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updated, err := r.findSiteAnnouncement(ctx, siteID, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dto := toSiteAnnouncementDTO(*updated)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
// DeleteSiteAnnouncement 删除本站公告。
|
||||
func (r *Repository) DeleteSiteAnnouncement(ctx context.Context, adminID, siteID, id uint64) error {
|
||||
if siteID == 0 {
|
||||
return ErrSiteNotFound
|
||||
}
|
||||
if err := r.ensureSiteAccess(ctx, adminID, siteID); err != nil {
|
||||
return err
|
||||
}
|
||||
row, err := r.findSiteAnnouncement(ctx, siteID, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.db.WithContext(ctx).Where("id = ?", row.ID).Delete(&model.Announcement{}).Error
|
||||
}
|
||||
|
||||
// findSiteAnnouncement 按站点归属读取公告,跨站 ID 一律视为不存在。
|
||||
func (r *Repository) findSiteAnnouncement(ctx context.Context, siteID, id uint64) (*model.Announcement, error) {
|
||||
var row model.Announcement
|
||||
err := r.db.WithContext(ctx).Where("id = ? AND distribution_site_id = ?", id, siteID).First(&row).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrAnnouncementNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &row, nil
|
||||
}
|
||||
|
||||
func toSiteAnnouncementDTO(a model.Announcement) SiteAnnouncementDTO {
|
||||
return SiteAnnouncementDTO{
|
||||
ID: a.ID,
|
||||
DistributionSiteID: a.DistributionSiteID,
|
||||
Title: a.Title,
|
||||
Content: a.Content,
|
||||
Category: a.Category,
|
||||
Priority: a.Priority,
|
||||
IsPinned: a.IsPinned,
|
||||
IsImportant: a.IsImportant,
|
||||
ViewCount: a.ViewCount,
|
||||
Status: a.Status,
|
||||
PublishedAt: a.PublishedAt,
|
||||
CreatedAt: a.CreatedAt,
|
||||
UpdatedAt: a.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeAnnouncementModeOrInherit 读侧容错:非法/历史空值一律按默认继承口径返回。
|
||||
func normalizeAnnouncementModeOrInherit(value string) string {
|
||||
mode, err := normalizeAnnouncementMode(value)
|
||||
if err != nil {
|
||||
return model.AnnouncementModeInherit
|
||||
}
|
||||
return mode
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package distribution
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
)
|
||||
|
||||
// TestSiteAnnouncementCRUDIsSiteScoped 验证站长自助公告只能读写本站数据:
|
||||
// 列表/更新/删除/上下架都强制 distribution_site_id = 本站,跨站 ID 视为不存在。
|
||||
func TestSiteAnnouncementCRUDIsSiteScoped(t *testing.T) {
|
||||
db := distributionTestDB(t)
|
||||
repo := NewRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
siteA, err := repo.CreateSite(ctx, CreateSiteRequest{
|
||||
SiteCode: "ann_site_a", SiteName: "公告站A", Domain: "ann-a.hfb.local",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
siteB, err := repo.CreateSite(ctx, CreateSiteRequest{
|
||||
SiteCode: "ann_site_b", SiteName: "公告站B", Domain: "ann-b.hfb.local",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
created, err := repo.CreateSiteAnnouncement(ctx, 0, siteA.ID, CreateSiteAnnouncementRequest{
|
||||
Title: "本站公告", Content: "内容", Category: "notice",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateSiteAnnouncement failed: %v", err)
|
||||
}
|
||||
if created.DistributionSiteID != siteA.ID || created.Status != "draft" {
|
||||
t.Fatalf("unexpected created announcement: %+v", created)
|
||||
}
|
||||
|
||||
// 列表按站点隔离
|
||||
pageA, err := repo.ListSiteAnnouncements(ctx, 0, siteA.ID, "", 1, 20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pageA.Total != 1 {
|
||||
t.Fatalf("A 站应有 1 条公告, got %d", pageA.Total)
|
||||
}
|
||||
pageB, err := repo.ListSiteAnnouncements(ctx, 0, siteB.ID, "", 1, 20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pageB.Total != 0 {
|
||||
t.Fatalf("B 站不应看到 A 站公告, got %d", pageB.Total)
|
||||
}
|
||||
|
||||
// 跨站更新/删除/上下架一律拒绝
|
||||
if _, err := repo.UpdateSiteAnnouncement(ctx, 0, siteB.ID, created.ID, UpdateSiteAnnouncementRequest{Title: "改"}); !errors.Is(err, ErrAnnouncementNotFound) {
|
||||
t.Fatalf("跨站更新应返回 ErrAnnouncementNotFound, got %v", err)
|
||||
}
|
||||
if err := repo.DeleteSiteAnnouncement(ctx, 0, siteB.ID, created.ID); !errors.Is(err, ErrAnnouncementNotFound) {
|
||||
t.Fatalf("跨站删除应返回 ErrAnnouncementNotFound, got %v", err)
|
||||
}
|
||||
if _, err := repo.UpdateSiteAnnouncementStatus(ctx, 0, siteB.ID, created.ID, "publish"); !errors.Is(err, ErrAnnouncementNotFound) {
|
||||
t.Fatalf("跨站发布应返回 ErrAnnouncementNotFound, got %v", err)
|
||||
}
|
||||
|
||||
// 本站更新与发布
|
||||
updated, err := repo.UpdateSiteAnnouncement(ctx, 0, siteA.ID, created.ID, UpdateSiteAnnouncementRequest{
|
||||
Title: "本站公告(改)", Content: "新内容", Category: "rule", Priority: 5, IsPinned: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("UpdateSiteAnnouncement failed: %v", err)
|
||||
}
|
||||
if updated.Title != "本站公告(改)" || updated.Category != "rule" || !updated.IsPinned || updated.Priority != 5 {
|
||||
t.Fatalf("unexpected updated announcement: %+v", updated)
|
||||
}
|
||||
published, err := repo.UpdateSiteAnnouncementStatus(ctx, 0, siteA.ID, created.ID, "publish")
|
||||
if err != nil {
|
||||
t.Fatalf("publish failed: %v", err)
|
||||
}
|
||||
if published.Status != "published" || published.PublishedAt == nil {
|
||||
t.Fatalf("发布后状态/时间不正确: %+v", published)
|
||||
}
|
||||
archived, err := repo.UpdateSiteAnnouncementStatus(ctx, 0, siteA.ID, created.ID, "archive")
|
||||
if err != nil {
|
||||
t.Fatalf("archive failed: %v", err)
|
||||
}
|
||||
if archived.Status != "archived" {
|
||||
t.Fatalf("归档后状态不正确: %+v", archived)
|
||||
}
|
||||
if _, err := repo.ListSiteAnnouncements(ctx, 0, siteA.ID, "archived", 1, 20); err != nil {
|
||||
t.Fatalf("按状态查询失败: %v", err)
|
||||
}
|
||||
if err := repo.DeleteSiteAnnouncement(ctx, 0, siteA.ID, created.ID); err != nil {
|
||||
t.Fatalf("本站删除失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateSiteBrandingAnnouncementMode 验证公告来源开关可保存并校验非法值。
|
||||
func TestUpdateSiteBrandingAnnouncementMode(t *testing.T) {
|
||||
db := distributionTestDB(t)
|
||||
repo := NewRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
site, err := repo.CreateSite(ctx, CreateSiteRequest{
|
||||
SiteCode: "ann_mode_site", SiteName: "模式站", Domain: "ann-mode.hfb.local",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if site.AnnouncementMode != model.AnnouncementModeInherit {
|
||||
t.Fatalf("默认公告来源应为 inherit, got %q", site.AnnouncementMode)
|
||||
}
|
||||
|
||||
ownOnly := model.AnnouncementModeOwnOnly
|
||||
updated, err := repo.UpdateSiteBranding(ctx, 0, site.ID, UpdateSiteBrandingRequest{AnnouncementMode: &ownOnly})
|
||||
if err != nil {
|
||||
t.Fatalf("切换公告来源失败: %v", err)
|
||||
}
|
||||
if updated.AnnouncementMode != model.AnnouncementModeOwnOnly {
|
||||
t.Fatalf("announcement_mode = %q, want own_only", updated.AnnouncementMode)
|
||||
}
|
||||
|
||||
invalid := "whatever"
|
||||
if _, err := repo.UpdateSiteBranding(ctx, 0, site.ID, UpdateSiteBrandingRequest{AnnouncementMode: &invalid}); !errors.Is(err, ErrInvalidAnnouncementMode) {
|
||||
t.Fatalf("非法来源应返回 ErrInvalidAnnouncementMode, got %v", err)
|
||||
}
|
||||
|
||||
// 读侧容错:库里历史非法值按 inherit 返回
|
||||
if err := db.Model(&model.DistributionSite{}).Where("id = ?", site.ID).
|
||||
Update("announcement_mode", "legacy_value").Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reloaded, err := repo.FindSite(ctx, 0, site.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reloaded.AnnouncementMode != model.AnnouncementModeInherit {
|
||||
t.Fatalf("非法历史值应回落 inherit, got %q", reloaded.AnnouncementMode)
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ type SiteDTO struct {
|
||||
AWRecycleJSON string `json:"aw_recycle_json"`
|
||||
CooperationFeedbackJSON string `json:"cooperation_feedback_json"`
|
||||
StorefrontTextJSON string `json:"storefront_text_json"`
|
||||
AnnouncementMode string `json:"announcement_mode"`
|
||||
DefaultSupportGroupID *uint64 `json:"default_support_group_id,omitempty"`
|
||||
ListingQrcodeURL string `json:"listing_qrcode_url"`
|
||||
ReceiveRatioDelta float64 `json:"receive_ratio_delta"`
|
||||
@@ -431,7 +432,51 @@ type UpdateSiteBrandingRequest struct {
|
||||
AWRecycleJSON *string `json:"aw_recycle_json"`
|
||||
CooperationFeedbackJSON *string `json:"cooperation_feedback_json"`
|
||||
StorefrontTextJSON *string `json:"storefront_text_json"`
|
||||
ListingQrcodeURL *string `json:"listing_qrcode_url"`
|
||||
// AnnouncementMode 公告中心来源:inherit(主站+本站)/ own_only(仅本站)。
|
||||
AnnouncementMode *string `json:"announcement_mode"`
|
||||
ListingQrcodeURL *string `json:"listing_qrcode_url"`
|
||||
}
|
||||
|
||||
// SiteAnnouncementDTO 分站公告(站长自助维护)。
|
||||
type SiteAnnouncementDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
DistributionSiteID uint64 `json:"distribution_site_id"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category"`
|
||||
Priority int `json:"priority"`
|
||||
IsPinned bool `json:"is_pinned"`
|
||||
IsImportant bool `json:"is_important"`
|
||||
ViewCount int `json:"view_count"`
|
||||
Status string `json:"status"`
|
||||
PublishedAt *time.Time `json:"published_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type SiteAnnouncementPage struct {
|
||||
Items []SiteAnnouncementDTO `json:"items"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
|
||||
type CreateSiteAnnouncementRequest struct {
|
||||
Title string `json:"title" binding:"required,max=255"`
|
||||
Content string `json:"content" binding:"required"`
|
||||
Category string `json:"category" binding:"omitempty,oneof=notice tutorial rule faq"`
|
||||
Priority int `json:"priority"`
|
||||
IsPinned bool `json:"is_pinned"`
|
||||
IsImportant bool `json:"is_important"`
|
||||
}
|
||||
|
||||
type UpdateSiteAnnouncementRequest struct {
|
||||
Title string `json:"title" binding:"max=255"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category" binding:"omitempty,oneof=notice tutorial rule faq"`
|
||||
Priority int `json:"priority"`
|
||||
IsPinned bool `json:"is_pinned"`
|
||||
IsImportant bool `json:"is_important"`
|
||||
}
|
||||
|
||||
// 自主加价 DTO:增加该站「1 元兑换哈夫币」比例,不是人民币。
|
||||
|
||||
@@ -642,6 +642,131 @@ func (h *Handler) resolveSiteID(c *gin.Context) (uint64, error) {
|
||||
return h.service.ScopeSiteID(c.Request.Context(), adminID, requested)
|
||||
}
|
||||
|
||||
// 分站公告中心(站长自助维护本站公告)。站点由 ?site_id= 解析并校验访问权。
|
||||
func (h *Handler) ListSiteAnnouncements(c *gin.Context) {
|
||||
siteID, adminID, ok := h.resolveAnnouncementSite(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
resp, err := h.service.ListSiteAnnouncements(c.Request.Context(), adminID, siteID, c.Query("status"), page, pageSize)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, resp)
|
||||
}
|
||||
|
||||
func (h *Handler) CreateSiteAnnouncement(c *gin.Context) {
|
||||
siteID, adminID, ok := h.resolveAnnouncementSite(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req CreateSiteAnnouncementRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "公告参数格式不正确")
|
||||
return
|
||||
}
|
||||
dto, err := h.service.CreateSiteAnnouncement(c.Request.Context(), adminID, siteID, req)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.Created(c, dto)
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateSiteAnnouncement(c *gin.Context) {
|
||||
siteID, adminID, ok := h.resolveAnnouncementSite(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, ok := parseSiteAnnouncementID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req UpdateSiteAnnouncementRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "公告参数格式不正确")
|
||||
return
|
||||
}
|
||||
dto, err := h.service.UpdateSiteAnnouncement(c.Request.Context(), adminID, siteID, id, req)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, dto)
|
||||
}
|
||||
|
||||
func (h *Handler) PublishSiteAnnouncement(c *gin.Context) {
|
||||
h.updateSiteAnnouncementStatus(c, "publish")
|
||||
}
|
||||
func (h *Handler) ArchiveSiteAnnouncement(c *gin.Context) {
|
||||
h.updateSiteAnnouncementStatus(c, "archive")
|
||||
}
|
||||
func (h *Handler) UnarchiveSiteAnnouncement(c *gin.Context) {
|
||||
h.updateSiteAnnouncementStatus(c, "unarchive")
|
||||
}
|
||||
|
||||
func (h *Handler) updateSiteAnnouncementStatus(c *gin.Context, action string) {
|
||||
siteID, adminID, ok := h.resolveAnnouncementSite(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, ok := parseSiteAnnouncementID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
dto, err := h.service.UpdateSiteAnnouncementStatus(c.Request.Context(), adminID, siteID, id, action)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, dto)
|
||||
}
|
||||
|
||||
func (h *Handler) DeleteSiteAnnouncement(c *gin.Context) {
|
||||
siteID, adminID, ok := h.resolveAnnouncementSite(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, ok := parseSiteAnnouncementID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.service.DeleteSiteAnnouncement(c.Request.Context(), adminID, siteID, id); err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, gin.H{"deleted": true})
|
||||
}
|
||||
|
||||
// resolveAnnouncementSite 解析公告所属站点:公告路由的 :id 是公告 ID,
|
||||
// 因此站点只能来自 ?site_id=(不能复用会优先读 :id 的 resolveSiteID)。
|
||||
func (h *Handler) resolveAnnouncementSite(c *gin.Context) (uint64, uint64, bool) {
|
||||
adminID, _ := currentAdminID(c)
|
||||
requested := uint64(parseQueryUint(c, "site_id"))
|
||||
siteID, err := h.service.ScopeSiteID(c.Request.Context(), adminID, requested)
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return 0, 0, false
|
||||
}
|
||||
if siteID == 0 {
|
||||
response.BadRequest(c, "未指定分销站点")
|
||||
return 0, 0, false
|
||||
}
|
||||
return siteID, adminID, true
|
||||
}
|
||||
|
||||
func parseSiteAnnouncementID(c *gin.Context) (uint64, bool) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
response.BadRequest(c, "公告ID不正确")
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateSiteBranding(c *gin.Context) {
|
||||
siteID, err := h.resolveSiteID(c)
|
||||
if err != nil {
|
||||
|
||||
@@ -52,6 +52,8 @@ var (
|
||||
ErrInvalidSettlementHold = errors.New("settlement hold days must be between 0 and 365")
|
||||
ErrBatchNotCancellable = errors.New("distribution settlement batch cannot be cancelled")
|
||||
ErrSettlementBatchStale = errors.New("distribution settlement batch stale")
|
||||
ErrAnnouncementNotFound = errors.New("distribution site announcement not found")
|
||||
ErrInvalidAnnouncementMode = errors.New("invalid announcement mode")
|
||||
)
|
||||
|
||||
var settlementReadyOrderStatuses = []string{"completed", "closed"}
|
||||
@@ -888,6 +890,7 @@ func siteDTO(row model.DistributionSite) SiteDTO {
|
||||
AWRecycleJSON: row.AWRecycleJSON,
|
||||
CooperationFeedbackJSON: row.CooperationFeedbackJSON,
|
||||
StorefrontTextJSON: row.StorefrontTextJSON,
|
||||
AnnouncementMode: normalizeAnnouncementModeOrInherit(row.AnnouncementMode),
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
}
|
||||
@@ -2750,6 +2753,14 @@ func (r *Repository) UpdateSiteBranding(ctx context.Context, adminID, siteID uin
|
||||
site.StorefrontTextJSON = strings.TrimSpace(*req.StorefrontTextJSON)
|
||||
updates["storefront_text_json"] = site.StorefrontTextJSON
|
||||
}
|
||||
if req.AnnouncementMode != nil {
|
||||
mode, err := normalizeAnnouncementMode(*req.AnnouncementMode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
site.AnnouncementMode = mode
|
||||
updates["announcement_mode"] = mode
|
||||
}
|
||||
if req.ListingQrcodeURL != nil {
|
||||
site.ListingQrcodeURL = strings.TrimSpace(*req.ListingQrcodeURL)
|
||||
updates["listing_qrcode_url"] = site.ListingQrcodeURL
|
||||
|
||||
@@ -313,6 +313,41 @@ func (s *Service) BatchUpdateAccountDisplay(ctx context.Context, adminID, siteID
|
||||
return s.repo.BatchUpdateAccountDisplay(ctx, adminID, siteID, req)
|
||||
}
|
||||
|
||||
func (s *Service) ListSiteAnnouncements(ctx context.Context, adminID, siteID uint64, status string, page, pageSize int) (*SiteAnnouncementPage, error) {
|
||||
if err := s.available(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.ListSiteAnnouncements(ctx, adminID, siteID, status, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) CreateSiteAnnouncement(ctx context.Context, adminID, siteID uint64, req CreateSiteAnnouncementRequest) (*SiteAnnouncementDTO, error) {
|
||||
if err := s.available(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.CreateSiteAnnouncement(ctx, adminID, siteID, req)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateSiteAnnouncement(ctx context.Context, adminID, siteID, id uint64, req UpdateSiteAnnouncementRequest) (*SiteAnnouncementDTO, error) {
|
||||
if err := s.available(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.UpdateSiteAnnouncement(ctx, adminID, siteID, id, req)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateSiteAnnouncementStatus(ctx context.Context, adminID, siteID, id uint64, action string) (*SiteAnnouncementDTO, error) {
|
||||
if err := s.available(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.UpdateSiteAnnouncementStatus(ctx, adminID, siteID, id, action)
|
||||
}
|
||||
|
||||
func (s *Service) DeleteSiteAnnouncement(ctx context.Context, adminID, siteID, id uint64) error {
|
||||
if err := s.available(); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.repo.DeleteSiteAnnouncement(ctx, adminID, siteID, id)
|
||||
}
|
||||
|
||||
func (s *Service) ListReceivedListings(ctx context.Context, adminID, siteID uint64, q ReceivedListingQuery) (*ReceivedListingPage, error) {
|
||||
if err := s.available(); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -3,8 +3,8 @@ package distribution
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"hfb_sys/backend/internal/model"
|
||||
)
|
||||
|
||||
func TestSiteCRUD_UpdateSiteAndDomainUniqueness(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user