feat: 类型帖互动、积分经济与发帖体验

补齐问答采纳/重开、投票匿名与有票禁编、悬赏过期退回、抽奖回帖开奖,并接入积分账本与发帖附件可见性。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-16 03:17:07 +08:00
parent 1ef3d8e299
commit b9ba8cb8c9
58 changed files with 6913 additions and 506 deletions

View File

@@ -54,12 +54,64 @@ const (
ContentStatusRejected = "rejected"
)
// 帖子类型
// 帖子类型(discussion 为默认;兼容旧值 normal → discussion)
const (
PostTypeNormal = "normal"
PostTypeQuestion = "question"
PostTypeDiscussion = "discussion" // 讨论(默认)
PostTypeQuestion = "question" // 问答
PostTypePoll = "poll" // 投票
PostTypeBounty = "bounty" // 悬赏
PostTypeLottery = "lottery" // 抽奖
PostTypeNormal = "normal" // 旧值,读取时归一为 discussion
)
// ValidPostType 发帖类型白名单
func ValidPostType(t string) bool {
switch t {
case PostTypeDiscussion, PostTypeQuestion, PostTypePoll, PostTypeBounty, PostTypeLottery:
return true
}
return false
}
// NormalizePostType 归一化类型(空/旧 normal → discussion)
func NormalizePostType(t string) string {
if t == "" || t == PostTypeNormal {
return PostTypeDiscussion
}
if ValidPostType(t) {
return t
}
return PostTypeDiscussion
}
// 正文可见性
const (
ContentAccessPublic = "public" // 公开
ContentAccessLogin = "login" // 登录可见
ContentAccessReply = "reply" // 回复可见
ContentAccessPoints = "points" // 积分购买可见
)
// ValidContentAccess 可见性白名单
func ValidContentAccess(a string) bool {
switch a {
case ContentAccessPublic, ContentAccessLogin, ContentAccessReply, ContentAccessPoints:
return true
}
return false
}
// NormalizeContentAccess 归一化可见性
func NormalizeContentAccess(a string) string {
if a == "" {
return ContentAccessPublic
}
if ValidContentAccess(a) {
return a
}
return ContentAccessPublic
}
// User 用户表
type User struct {
ID uint `gorm:"primaryKey" json:"id"`
@@ -71,6 +123,7 @@ type User struct {
Signature string `gorm:"size:255;default:''" json:"signature"` // 个性签名
Role Role `gorm:"size:16;default:user" json:"role"`
Banned bool `gorm:"default:false" json:"banned"`
Points int `gorm:"not null;default:0" json:"points"` // 可用积分余额
TokenVersion int `gorm:"default:0" json:"-"` // token 版本号,改密码/封禁时递增使旧 JWT 失效
LastSeenAt *time.Time `gorm:"index" json:"-"` // 最近活跃时间(在线统计,限频更新)
CreatedAt time.Time `json:"created_at"`
@@ -111,28 +164,105 @@ type Board struct {
// Post 帖子
type Post struct {
ID uint `gorm:"primaryKey" json:"id"`
BoardID uint `gorm:"index;not null" json:"board_id"`
UserID uint `gorm:"index;not null" json:"user_id"`
Title string `gorm:"size:256;not null" json:"title"`
Content string `gorm:"type:text;not null" json:"content"`
Tags string `gorm:"size:256" json:"tags"`
PostType string `gorm:"size:16;default:normal;index" json:"post_type"`
Pinned int `gorm:"default:0" json:"pinned"`
Recommended bool `gorm:"default:false;index" json:"recommended"`
Status string `gorm:"size:16;default:published;index" json:"status"`
LikeCount int `gorm:"default:0" json:"like_count"`
ViewCount int `gorm:"default:0" json:"view_count"`
CommentCount int `gorm:"default:0" json:"comment_count"`
Liked bool `gorm:"-" json:"liked"` // 当前用户是否已点赞(展示字段,不入库)
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
ID uint `gorm:"primaryKey" json:"id"`
BoardID uint `gorm:"index;not null" json:"board_id"`
UserID uint `gorm:"index;not null" json:"user_id"`
Title string `gorm:"size:256;not null" json:"title"`
Content string `gorm:"type:text;not null" json:"content"`
Tags string `gorm:"size:256" json:"tags"`
PostType string `gorm:"size:16;default:discussion;index" json:"post_type"`
ContentAccess string `gorm:"size:16;default:public;index" json:"content_access"` // public|login|reply|points
AccessPoints int `gorm:"not null;default:0" json:"access_points"` // points 可见时所需积分
TypeMeta string `gorm:"type:text;default:''" json:"type_meta"` // 类型扩展 JSON(投票/悬赏/抽奖壳)
Pinned int `gorm:"default:0" json:"pinned"`
Recommended bool `gorm:"default:false;index" json:"recommended"`
Status string `gorm:"size:16;default:published;index" json:"status"`
LikeCount int `gorm:"default:0" json:"like_count"`
ViewCount int `gorm:"default:0" json:"view_count"`
CommentCount int `gorm:"default:0" json:"comment_count"`
Liked bool `gorm:"-" json:"liked"` // 当前用户是否已点赞(展示字段,不入库)
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
Board Board `gorm:"foreignKey:BoardID" json:"board,omitempty"`
User User `gorm:"foreignKey:UserID" json:"user,omitempty"`
}
// PointLedger 积分流水(余额以 User.Points 为准,本表可审计)
type PointLedger struct {
ID uint `gorm:"primaryKey" json:"id"`
UserID uint `gorm:"index;not null" json:"user_id"`
Delta int `gorm:"not null" json:"delta"` // 正入负出
Balance int `gorm:"not null" json:"balance"` // 变动后余额
Reason string `gorm:"size:32;not null;index" json:"reason"`
RefType string `gorm:"size:32;not null;default:''" json:"ref_type"`
RefID uint `gorm:"not null;default:0" json:"ref_id"`
Note string `gorm:"size:256;not null;default:''" json:"note"`
CreatedAt time.Time `gorm:"index" json:"created_at"`
}
// 积分流水原因
const (
PointReasonCheckin = "checkin"
PointReasonUnlockPost = "unlock_post"
PointReasonDownloadFile = "download_file"
PointReasonMigrateCheckin = "migrate_checkin"
PointReasonBountyEscrow = "bounty_escrow"
PointReasonBountyRefund = "bounty_refund"
PointReasonBountyAward = "bounty_award"
)
// PostPollVote 投票记录(多选时同一用户多行)
type PostPollVote struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"uniqueIndex:idx_poll_vote;not null" json:"post_id"`
UserID uint `gorm:"uniqueIndex:idx_poll_vote;not null" json:"user_id"`
OptionIndex int `gorm:"uniqueIndex:idx_poll_vote;not null" json:"option_index"`
CreatedAt time.Time `json:"created_at"`
}
// PostLotteryEntry 抽奖报名
type PostLotteryEntry struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"uniqueIndex:idx_lottery_entry;not null" json:"post_id"`
UserID uint `gorm:"uniqueIndex:idx_lottery_entry;not null" json:"user_id"`
CreatedAt time.Time `json:"created_at"`
}
// PostContentUnlock 积分购买正文解锁记录
type PostContentUnlock struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"uniqueIndex:idx_post_unlock_user;not null" json:"post_id"`
UserID uint `gorm:"uniqueIndex:idx_post_unlock_user;not null" json:"user_id"`
Points int `gorm:"not null;default:0" json:"points"`
CreatedAt time.Time `json:"created_at"`
}
// PostAttachment 帖子文件附件(不走公开静态目录,经 API 鉴权下载)
type PostAttachment struct {
ID uint `gorm:"primaryKey" json:"id"`
PostID uint `gorm:"index;not null;default:0" json:"post_id"` // 0=草稿未绑定
UserID uint `gorm:"index;not null" json:"user_id"`
Name string `gorm:"size:256;not null" json:"name"` // 原始文件名
StoredName string `gorm:"size:64;not null" json:"-"` // 磁盘文件名
MIME string `gorm:"size:128;not null;default:application/octet-stream" json:"mime"`
Size int `gorm:"not null;default:0" json:"size"`
PricePoints int `gorm:"not null;default:0" json:"price_points"` // 0=免费
DownloadCount int `gorm:"not null;default:0" json:"download_count"`
CreatedAt time.Time `json:"created_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
// PostAttachmentUnlock 积分附件下载解锁(按附件计费一次)
type PostAttachmentUnlock struct {
ID uint `gorm:"primaryKey" json:"id"`
AttachmentID uint `gorm:"uniqueIndex:idx_att_unlock_user;not null" json:"attachment_id"`
UserID uint `gorm:"uniqueIndex:idx_att_unlock_user;not null" json:"user_id"`
Points int `gorm:"not null;default:0" json:"points"`
CreatedAt time.Time `json:"created_at"`
}
// Comment 评论(主评论 = 楼层,ParentID 为空;子评论挂 root_id 对应楼层下)
type Comment struct {
ID uint `gorm:"primaryKey" json:"id"`