Go + PostgreSQL + Valkey backend (cmd/internal/migrations), uni-app mobile frontend, PC web (web/), admin console (admin/), Android WebView container (android_project/). Build artifacts, node_modules, local env and debug screenshots excluded.
220 lines
7.1 KiB
Go
220 lines
7.1 KiB
Go
package domain
|
||
|
||
import (
|
||
"encoding/json"
|
||
"time"
|
||
)
|
||
|
||
type ArticleStatus string
|
||
|
||
const (
|
||
StatusDraft ArticleStatus = "draft"
|
||
StatusPending ArticleStatus = "pending"
|
||
StatusPublished ArticleStatus = "published"
|
||
StatusOffline ArticleStatus = "offline"
|
||
)
|
||
|
||
type ReviewStatus string
|
||
|
||
const (
|
||
ReviewPending ReviewStatus = "pending"
|
||
ReviewApproved ReviewStatus = "approved"
|
||
ReviewRejected ReviewStatus = "rejected"
|
||
)
|
||
|
||
type Privacy string
|
||
|
||
const (
|
||
PrivacyPublic Privacy = "public"
|
||
PrivacyPrivate Privacy = "private"
|
||
PrivacyPassword Privacy = "password"
|
||
PrivacyPaid Privacy = "paid"
|
||
)
|
||
|
||
// Article 是聚合根。Sections 只在详情读取时填充,列表查询为空以省带宽。
|
||
type Article struct {
|
||
ID string `json:"id"`
|
||
MaskID string `json:"mask_id"`
|
||
|
||
Title string `json:"title"`
|
||
RichTextTitle json.RawMessage `json:"rich_text_title,omitempty"`
|
||
Subtitle *string `json:"subtitle,omitempty"`
|
||
Abstract *string `json:"abstract,omitempty"`
|
||
CoverImgURL *string `json:"cover_img_url,omitempty"`
|
||
CoverURL *string `json:"cover_url,omitempty"`
|
||
CoverCrop json.RawMessage `json:"cover_crop,omitempty"`
|
||
|
||
VisualConfig json.RawMessage `json:"visual_config,omitempty"`
|
||
InteractionConfig json.RawMessage `json:"interaction_config,omitempty"`
|
||
|
||
BgStyle *string `json:"bg_style,omitempty"`
|
||
BgImgURL *string `json:"bg_img_url,omitempty"`
|
||
FontID *string `json:"font_id,omitempty"`
|
||
MusicID *string `json:"music_id,omitempty"`
|
||
TextPos *string `json:"text_pos,omitempty"`
|
||
EnableWatermark bool `json:"enable_watermark"`
|
||
EnableComment bool `json:"enable_comment"`
|
||
Privacy Privacy `json:"privacy"`
|
||
HasReward bool `json:"has_reward"`
|
||
|
||
OwnerID string `json:"owner_id"`
|
||
CategoryID *string `json:"category_id,omitempty"`
|
||
TopicID *string `json:"topic_id,omitempty"`
|
||
TopicName *string `json:"topic_name,omitempty"`
|
||
|
||
Status ArticleStatus `json:"status"`
|
||
ReviewStatus ReviewStatus `json:"review_status"`
|
||
ReviewNote *string `json:"review_note,omitempty"`
|
||
ReviewedBy *string `json:"reviewed_by,omitempty"`
|
||
ReviewedAt *time.Time `json:"reviewed_at,omitempty"`
|
||
|
||
PublishedAt *time.Time `json:"published_at,omitempty"`
|
||
|
||
ViewCount int64 `json:"view_count"`
|
||
LikeCount int64 `json:"like_count"`
|
||
FavCount int64 `json:"fav_count"`
|
||
CommentCount int64 `json:"comment_count"`
|
||
ShareCount int64 `json:"share_count"`
|
||
|
||
SchemaVersion int `json:"schema_version"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
|
||
Sections []Section `json:"sections,omitempty"`
|
||
Tags []Tag `json:"tags,omitempty"`
|
||
|
||
// 列表展示用的作者信息(JOIN 得到,非独立实体)
|
||
AuthorNickname *string `json:"author_nickname,omitempty"`
|
||
AuthorAvatar *string `json:"author_avatar,omitempty"`
|
||
CategoryName *string `json:"category_name,omitempty"`
|
||
|
||
// 当前访问者的互动状态(非持久化字段,由 handler 按登录用户填充)
|
||
IsLiked bool `json:"is_liked"`
|
||
IsFavorited bool `json:"is_favorited"`
|
||
IsFollowing bool `json:"is_following"`
|
||
|
||
ViewedAt *time.Time `json:"viewed_at,omitempty"`
|
||
}
|
||
|
||
// CanBeReadBy 判断某文章对给定访问者是否可见。
|
||
// 这是权限判断的唯一入口,避免各处散落 if。
|
||
func (a *Article) CanBeReadBy(viewerID string, viewerRole string) bool {
|
||
// 作者本人永远能读自己的
|
||
if viewerID != "" && a.OwnerID == viewerID {
|
||
return true
|
||
}
|
||
// 运营与审核可以读所有(含草稿)
|
||
if viewerRole == "operator" || viewerRole == "super_admin" || viewerRole == "reviewer" {
|
||
return true
|
||
}
|
||
if a.Status != StatusPublished {
|
||
return false
|
||
}
|
||
if a.Privacy == PrivacyPrivate {
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
// CanBeEditedBy 只有作者本人或运营可编辑
|
||
func (a *Article) CanBeEditedBy(userID, role string) bool {
|
||
if role == "operator" || role == "super_admin" {
|
||
return true
|
||
}
|
||
return a.OwnerID == userID
|
||
}
|
||
|
||
// ── 其他实体 ──
|
||
|
||
type Tag struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
ArticleCount int `json:"article_count"`
|
||
HeatCount int64 `json:"heat_count"`
|
||
Status string `json:"status"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|
||
|
||
type Category struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
Slug string `json:"slug"`
|
||
ParentID *string `json:"parent_id,omitempty"`
|
||
SortOrder int `json:"sort_order"`
|
||
IconURL *string `json:"icon_url,omitempty"`
|
||
Status string `json:"status"`
|
||
Children []Category `json:"children,omitempty"`
|
||
}
|
||
|
||
type User struct {
|
||
ID string `json:"id"`
|
||
Account string `json:"account"`
|
||
Nickname string `json:"nickname"`
|
||
Email *string `json:"email,omitempty"`
|
||
AvatarURL *string `json:"avatar_url,omitempty"`
|
||
Role string `json:"role"`
|
||
Status string `json:"status"`
|
||
Bio *string `json:"bio,omitempty"`
|
||
Gender *string `json:"gender,omitempty"`
|
||
Region *string `json:"region,omitempty"`
|
||
LastLoginAt *time.Time `json:"last_login_at,omitempty"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|
||
|
||
type Topic struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
Description *string `json:"description,omitempty"`
|
||
CoverURL *string `json:"cover_url,omitempty"`
|
||
CategoryID *string `json:"category_id,omitempty"`
|
||
CategoryName *string `json:"category_name,omitempty"`
|
||
FollowCount int64 `json:"follow_count"`
|
||
ArticleCount int64 `json:"article_count"`
|
||
Status string `json:"status"`
|
||
CreatorID *string `json:"creator_id,omitempty"`
|
||
IsFollowed bool `json:"is_following"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
}
|
||
|
||
type Template struct {
|
||
ID string `json:"id"`
|
||
Title string `json:"title"`
|
||
Description *string `json:"description,omitempty"`
|
||
Category string `json:"category"`
|
||
CoverURL *string `json:"cover_url,omitempty"`
|
||
TargetPlatform string `json:"target_platform"`
|
||
SectionsJSON any `json:"sections_json"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
UpdatedAt time.Time `json:"updated_at"`
|
||
}
|
||
|
||
type Comment struct {
|
||
ID string `json:"id"`
|
||
ArticleID string `json:"article_id"`
|
||
UserID string `json:"user_id"`
|
||
ParentID *string `json:"parent_id,omitempty"`
|
||
Depth int `json:"depth"`
|
||
Content string `json:"content"`
|
||
LikeCount int64 `json:"like_count"`
|
||
Status string `json:"status"`
|
||
|
||
AuthorNickname *string `json:"author_nickname,omitempty"`
|
||
AuthorAvatar *string `json:"author_avatar,omitempty"`
|
||
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|
||
|
||
type Notification struct {
|
||
ID string `json:"id"`
|
||
UserID string `json:"user_id"`
|
||
ActorID *string `json:"actor_id,omitempty"`
|
||
Type string `json:"type"`
|
||
EntityType string `json:"entity_type"`
|
||
EntityID *string `json:"entity_id,omitempty"`
|
||
Title string `json:"title"`
|
||
Content string `json:"content"`
|
||
IsRead bool `json:"is_read"`
|
||
CreatedAt time.Time `json:"created_at"`
|
||
}
|