sky/internal/domain/section.go
root 6ed55254a0 chore: initial import of sky project
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.
2026-11-09 22:22:44 +08:00

257 lines
6.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package domain
import (
"encoding/json"
"fmt"
"time"
)
// ────────────────────────────────────────────────────────────
// 区块判别联合(Discriminated Union)
//
// 与美篇的关键差异:美篇用 type=1 表示图文和商品,靠「哪些字段存在」猜种类;
// 这里 kind 是显式标签,编译期可穷尽,运行期可校验。
// ────────────────────────────────────────────────────────────
type Kind string
const (
KindText Kind = "text"
KindSubtitle Kind = "subtitle"
KindImage Kind = "image"
KindVideo Kind = "video"
KindAudio Kind = "audio"
KindVote Kind = "vote"
KindLocation Kind = "location"
KindGoods Kind = "goods"
)
var AllKinds = []Kind{
KindText, KindSubtitle, KindImage, KindVideo,
KindAudio, KindVote, KindLocation, KindGoods,
}
func (k Kind) Valid() bool {
for _, x := range AllKinds {
if x == k {
return true
}
}
return false
}
// Section 是统一存储形态:kind 做判别,payload 存专属字段
type Section struct {
ID string `json:"id"`
ArticleID string `json:"article_id,omitempty"`
OrderIndex int `json:"order_index"`
Kind Kind `json:"kind"`
Payload json.RawMessage `json:"payload"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ── 各 kind 的 payload 结构 ──
// TextPayload 正文段落。AST 为富文本树,plain_text 用于搜索与字数统计。
type TextPayload struct {
PlainText string `json:"plain_text"`
AST json.RawMessage `json:"ast,omitempty"`
TextAlign string `json:"text_align,omitempty"` // left|center|right
Indent int `json:"indent,omitempty"` // 首行缩进级数
}
type SubtitlePayload struct {
Text string `json:"text"`
Align string `json:"align,omitempty"`
Size string `json:"size,omitempty"` // sm|md|lg
}
type ImageItem struct {
URL string `json:"url"`
Caption string `json:"caption,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Size int64 `json:"size,omitempty"`
}
type ImagePayload struct {
Images []ImageItem `json:"images"`
Layout string `json:"layout,omitempty"` // single|grid2|grid3
}
type VideoPayload struct {
VideoURL string `json:"video_url"`
CoverURL string `json:"cover_url,omitempty"`
DurationSeconds int `json:"duration_seconds,omitempty"`
Title string `json:"title,omitempty"`
}
type AudioPayload struct {
AudioURL string `json:"audio_url"`
Title string `json:"title,omitempty"`
DurationSeconds int `json:"duration_seconds,omitempty"`
MusicID string `json:"music_id,omitempty"`
}
type VoteOption struct {
ID string `json:"id"`
Text string `json:"text"`
Count int `json:"count"`
}
type VotePayload struct {
Question string `json:"question"`
Options []VoteOption `json:"options"`
VoteType string `json:"vote_type,omitempty"` // single|multiple
Deadline string `json:"deadline,omitempty"` // RFC3339
}
type LocationPayload struct {
Name string `json:"name"`
Address string `json:"address,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
}
type GoodsPayload struct {
Title string `json:"title"`
ImageURL string `json:"image_url,omitempty"`
PriceCent int64 `json:"price_cent,omitempty"`
SellKey string `json:"sell_key,omitempty"`
Platform string `json:"platform,omitempty"`
}
// Decoded 把 payload 反序列化成对应的强类型结构。
// 新增 kind 时这里必须补 case —— 用 default 返回错误而非静默忽略。
func (s Section) Decoded() (any, error) {
switch s.Kind {
case KindText:
var v TextPayload
return v, json.Unmarshal(s.Payload, &v)
case KindSubtitle:
var v SubtitlePayload
return v, json.Unmarshal(s.Payload, &v)
case KindImage:
var v ImagePayload
return v, json.Unmarshal(s.Payload, &v)
case KindVideo:
var v VideoPayload
return v, json.Unmarshal(s.Payload, &v)
case KindAudio:
var v AudioPayload
return v, json.Unmarshal(s.Payload, &v)
case KindVote:
var v VotePayload
return v, json.Unmarshal(s.Payload, &v)
case KindLocation:
var v LocationPayload
return v, json.Unmarshal(s.Payload, &v)
case KindGoods:
var v GoodsPayload
return v, json.Unmarshal(s.Payload, &v)
default:
return nil, fmt.Errorf("未知区块类型: %s", s.Kind)
}
}
// Validate 按 kind 校验 payload 的必填字段。
// 这是「能存进去就读得出来」的保证:脏数据在写入前被挡住。
func (s Section) Validate() error {
if !s.Kind.Valid() {
return fmt.Errorf("未知区块类型: %s", s.Kind)
}
if len(s.Payload) == 0 {
return fmt.Errorf("payload 不能为空")
}
var tmp map[string]any
if err := json.Unmarshal(s.Payload, &tmp); err != nil {
return fmt.Errorf("payload 不是合法 JSON 对象")
}
switch s.Kind {
case KindText:
p, err := s.Decoded()
if err != nil {
return err
}
t := p.(TextPayload)
if len(t.PlainText) > 50000 {
return fmt.Errorf("正文段落超过 50000 字")
}
case KindSubtitle:
p, err := s.Decoded()
if err != nil {
return err
}
if p.(SubtitlePayload).Text == "" {
return fmt.Errorf("小标题内容不能为空")
}
case KindImage:
p, err := s.Decoded()
if err != nil {
return err
}
ip := p.(ImagePayload)
if len(ip.Images) == 0 {
return fmt.Errorf("图片区块至少需要一张图")
}
if len(ip.Images) > 100 {
return fmt.Errorf("单个图片区块最多 100 张")
}
for i, img := range ip.Images {
if img.URL == "" {
return fmt.Errorf("第 %d 张图片缺少 url", i+1)
}
}
case KindVideo:
p, err := s.Decoded()
if err != nil {
return err
}
if p.(VideoPayload).VideoURL == "" {
return fmt.Errorf("视频 url 不能为空")
}
case KindAudio:
p, err := s.Decoded()
if err != nil {
return err
}
if p.(AudioPayload).AudioURL == "" {
return fmt.Errorf("音频 url 不能为空")
}
case KindVote:
p, err := s.Decoded()
if err != nil {
return err
}
vp := p.(VotePayload)
if vp.Question == "" {
return fmt.Errorf("投票问题不能为空")
}
if len(vp.Options) < 2 {
return fmt.Errorf("投票至少需要 2 个选项")
}
if len(vp.Options) > 50 {
return fmt.Errorf("投票最多 50 个选项")
}
case KindLocation:
p, err := s.Decoded()
if err != nil {
return err
}
if p.(LocationPayload).Name == "" {
return fmt.Errorf("地点名称不能为空")
}
case KindGoods:
p, err := s.Decoded()
if err != nil {
return err
}
if p.(GoodsPayload).Title == "" {
return fmt.Errorf("商品标题不能为空")
}
}
return nil
}