| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package model
- import (
- "crypto/sha256"
- "encoding/hex"
- "time"
- )
- // HashPassword 生成密码哈希
- func HashPassword(password string) string {
- if password == "" {
- return ""
- }
- hash := sha256.Sum256([]byte(password))
- return hex.EncodeToString(hash[:])
- }
- // CheckPassword 验证密码
- func CheckPassword(password, hash string) bool {
- // 如果笔记没有设置密码(hash 为空),则不需要验证
- if hash == "" {
- return true
- }
- // 如果用户没有输入密码,验证失败
- if password == "" {
- return false
- }
- return HashPassword(password) == hash
- }
- // Note 笔记模型(也用于目录)
- type Note struct {
- ID uint `json:"id" gorm:"primaryKey"`
- Title string `json:"title" gorm:"size:255;not null" binding:"required"`
- Content string `json:"content" gorm:"type:text"`
- Category string `json:"category" gorm:"size:100;index"`
- Tags string `json:"tags" gorm:"type:text"` // JSON 数组格式存储
- Password string `json:"-" gorm:"size:255"` // 访问密码(哈希存储)
- IsPinned bool `json:"is_pinned" gorm:"default:false"`
- IsFavorite bool `json:"is_favorite" gorm:"default:false"`
- IsPublic bool `json:"is_public"` // 是否公开
- ParentID uint `json:"parent_id" gorm:"default:0;index"` // 父级目录 ID,0 表示根目录
- IsFolder bool `json:"is_folder" gorm:"default:false"` // 是否为文件夹
- SortOrder int `json:"sort_order" gorm:"default:0"` // 排序顺序
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- }
- // NoteListItem 笔记列表响应(不含内容和密码)
- type NoteListItem struct {
- ID uint `json:"id"`
- Title string `json:"title"`
- Category string `json:"category"`
- Tags string `json:"tags"`
- HasPassword bool `json:"has_password"` // 是否有密码保护
- IsPinned bool `json:"is_pinned"`
- IsFavorite bool `json:"is_favorite"`
- IsPublic bool `json:"is_public"`
- ParentID uint `json:"parent_id"`
- IsFolder bool `json:"is_folder"`
- SortOrder int `json:"sort_order"`
- CreatedAt time.Time `json:"created_at"`
- UpdatedAt time.Time `json:"updated_at"`
- }
- // NoteCreateRequest 创建笔记请求
- type NoteCreateRequest struct {
- Title string `json:"title" binding:"required"`
- Content string `json:"content"`
- Category string `json:"category"`
- Tags string `json:"tags"`
- Password string `json:"password"`
- IsPinned *bool `json:"is_pinned"`
- IsFavorite *bool `json:"is_favorite"`
- IsPublic *bool `json:"is_public"`
- ParentID *uint `json:"parent_id"`
- IsFolder bool `json:"is_folder"`
- SortOrder int `json:"sort_order"`
- }
- // NoteUpdateRequest 更新笔记请求
- type NoteUpdateRequest struct {
- Title *string `json:"title"`
- Content *string `json:"content"`
- Category *string `json:"category"`
- Tags *string `json:"tags"`
- Password *string `json:"password"`
- RemovePassword *bool `json:"remove_password"` // 是否移除密码
- IsPinned *bool `json:"is_pinned"`
- IsFavorite *bool `json:"is_favorite"`
- IsPublic *bool `json:"is_public"`
- ParentID *uint `json:"parent_id"`
- IsFolder *bool `json:"is_folder"`
- SortOrder *int `json:"sort_order"`
- }
- // NoteAccessRequest 笔记访问请求(验证密码)
- type NoteAccessRequest struct {
- Password string `json:"password"`
- }
|