公告中心(前台公告列表/详情)此前完全没有站点概念,分站访客只能看到主站公告。 本次按「默认继承主站、站长可切仅本站」的口径支持分站自定义。 数据与口径: - 新增 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 与跨站防护、公告来源开关校验与非法值容错
112 lines
2.8 KiB
Go
112 lines
2.8 KiB
Go
package announcement
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Service struct {
|
|
repo *Repository
|
|
}
|
|
|
|
func NewService(repo *Repository) *Service {
|
|
return &Service{repo: repo}
|
|
}
|
|
|
|
var (
|
|
ErrNotFound = errors.New("公告不存在")
|
|
ErrInvalidRequest = errors.New("请求参数不正确")
|
|
ErrUnauthorized = errors.New("未授权")
|
|
)
|
|
|
|
// ResolvePublicSiteScope 解析前台请求应看到的公告归属站点集合(站点编码由请求头解析)。
|
|
func (s *Service) ResolvePublicSiteScope(ctx context.Context, siteCode string) ([]uint64, error) {
|
|
return s.repo.ResolvePublicSiteScope(ctx, siteCode)
|
|
}
|
|
|
|
// List 获取公告列表(前台用户)
|
|
func (s *Service) List(ctx context.Context, query AnnouncementListQuery) (*PaginatedResult, error) {
|
|
if query.Page < 1 {
|
|
query.Page = 1
|
|
}
|
|
if query.PageSize < 1 {
|
|
query.PageSize = 20
|
|
}
|
|
if query.PageSize > 100 {
|
|
query.PageSize = 100
|
|
}
|
|
|
|
return s.repo.List(ctx, query)
|
|
}
|
|
|
|
// GetByID 获取公告详情(限前台可见的站点范围)
|
|
func (s *Service) GetByID(ctx context.Context, id uint64, allowedSiteIDs []uint64) (*AnnouncementDTO, error) {
|
|
dto, err := s.repo.GetByID(ctx, id, allowedSiteIDs)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return dto, nil
|
|
}
|
|
|
|
// AdminList 管理员获取公告列表
|
|
func (s *Service) AdminList(ctx context.Context, query AnnouncementListQuery) (*PaginatedResult, error) {
|
|
if query.Page < 1 {
|
|
query.Page = 1
|
|
}
|
|
if query.PageSize < 1 {
|
|
query.PageSize = 20
|
|
}
|
|
if query.PageSize > 100 {
|
|
query.PageSize = 100
|
|
}
|
|
|
|
return s.repo.AdminList(ctx, query)
|
|
}
|
|
|
|
// AdminGetByID 管理员获取公告详情
|
|
func (s *Service) AdminGetByID(ctx context.Context, id uint64) (*AnnouncementDTO, error) {
|
|
dto, err := s.repo.AdminGetByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return dto, nil
|
|
}
|
|
|
|
// Create 创建公告
|
|
func (s *Service) Create(ctx context.Context, req CreateAnnouncementRequest, createdBy uint64) (*AnnouncementDTO, error) {
|
|
return s.repo.Create(ctx, req, createdBy)
|
|
}
|
|
|
|
// Update 更新公告
|
|
func (s *Service) Update(ctx context.Context, id uint64, req UpdateAnnouncementRequest) (*AnnouncementDTO, error) {
|
|
return s.repo.Update(ctx, id, req)
|
|
}
|
|
|
|
// Publish 发布公告
|
|
func (s *Service) Publish(ctx context.Context, id uint64) error {
|
|
return s.repo.Publish(ctx, id)
|
|
}
|
|
|
|
// Archive 归档公告
|
|
func (s *Service) Archive(ctx context.Context, id uint64) error {
|
|
return s.repo.Archive(ctx, id)
|
|
}
|
|
|
|
// Unarchive 取消归档公告
|
|
func (s *Service) Unarchive(ctx context.Context, id uint64) error {
|
|
return s.repo.Unarchive(ctx, id)
|
|
}
|
|
|
|
// Delete 删除公告
|
|
func (s *Service) Delete(ctx context.Context, id uint64) error {
|
|
return s.repo.Delete(ctx, id)
|
|
}
|