fix(activity): 限制未开始福利活动曝光与参与

未开始的福利活动不再返回给前端活动列表,并统一后端参与时间校验,避免用户在开始前查看或参与活动。
This commit is contained in:
Zuncle 2026-05-02 23:04:43 +08:00
parent cb5061f1da
commit b3fcec569a
3 changed files with 25 additions and 5 deletions

View File

@ -123,12 +123,16 @@ func (s *service) ListActivities(ctx context.Context, req ListActivitiesRequest)
if req.PageSize > 100 {
req.PageSize = 100
}
now := time.Now()
db := s.repo.GetDbR().WithContext(ctx).Model(&Activity{}).Where("deleted_at IS NULL")
if req.Type != "" {
db = db.Where("type = ?", req.Type)
}
if req.Status != "" {
db = db.Where("status = ?", req.Status)
if req.Status == StatusActive {
db = db.Where("start_time <= ? AND end_time >= ? AND draw_time >= ?", now, now, now)
}
}
if strings.TrimSpace(req.Title) != "" {
db = db.Where("title LIKE ?", "%"+strings.TrimSpace(req.Title)+"%")
@ -157,6 +161,9 @@ func (s *service) GetActivity(ctx context.Context, id int64, userID int64) (*Act
if err := s.repo.GetDbR().WithContext(ctx).Where("id = ? AND deleted_at IS NULL", id).First(&item).Error; err != nil {
return nil, err
}
if userID <= 0 && item.Status == StatusActive && time.Now().Before(item.StartTime) {
return nil, errors.New("活动未开始")
}
detail := &ActivityDetail{Activity: item}
s.repo.GetDbR().WithContext(ctx).Where("activity_id = ?", id).Order("sort ASC, id ASC").Find(&detail.Prizes)
s.fillPrizeMeta(ctx, detail.Prizes)
@ -172,7 +179,7 @@ func (s *service) GetActivity(ctx context.Context, id int64, userID int64) (*Act
if userID > 0 {
start, end, period := periodRange(item.Type, time.Now())
detail.CurrentPaid, _ = s.sumPaidAmount(ctx, userID, start, end)
detail.CanJoin = detail.CurrentPaid >= item.ThresholdAmount && item.Status == StatusActive && time.Now().Before(item.DrawTime) && time.Now().Before(item.EndTime)
detail.CanJoin = isJoinWindowOpen(item, time.Now()) && detail.CurrentPaid >= item.ThresholdAmount
var count int64
s.repo.GetDbR().WithContext(ctx).Model(&Participant{}).Where("activity_id = ? AND user_id = ? AND period_key = ?", id, userID, period).Count(&count)
detail.Joined = count > 0

View File

@ -16,10 +16,10 @@ func (s *service) Join(ctx context.Context, activityID int64, userID int64) erro
return err
}
now := time.Now()
if activity.Status != StatusActive {
return errors.New("活动未开放参与")
if !isJoinWindowOpen(activity, now) {
if now.Before(activity.StartTime) {
return errors.New("活动未开始")
}
if now.Before(activity.StartTime) || now.After(activity.EndTime) || now.After(activity.DrawTime) {
return errors.New("当前不在活动参与时间内")
}
start, end, period := periodRange(activity.Type, now)

View File

@ -45,3 +45,16 @@ type service struct {
func New(log logger.CustomLogger, repo mysql.Repo) Service {
return &service{logger: log, repo: repo, userSvc: usersvc.New(log, repo)}
}
func isJoinWindowOpen(activity Activity, now time.Time) bool {
if activity.Status != StatusActive {
return false
}
if now.Before(activity.StartTime) {
return false
}
if now.After(activity.EndTime) || now.After(activity.DrawTime) {
return false
}
return true
}