note.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package model
  2. import (
  3. "crypto/sha256"
  4. "encoding/hex"
  5. "time"
  6. )
  7. // HashPassword 生成密码哈希
  8. func HashPassword(password string) string {
  9. if password == "" {
  10. return ""
  11. }
  12. hash := sha256.Sum256([]byte(password))
  13. return hex.EncodeToString(hash[:])
  14. }
  15. // CheckPassword 验证密码
  16. func CheckPassword(password, hash string) bool {
  17. // 如果笔记没有设置密码(hash 为空),则不需要验证
  18. if hash == "" {
  19. return true
  20. }
  21. // 如果用户没有输入密码,验证失败
  22. if password == "" {
  23. return false
  24. }
  25. return HashPassword(password) == hash
  26. }
  27. // Note 笔记模型(也用于目录)
  28. type Note struct {
  29. ID uint `json:"id" gorm:"primaryKey"`
  30. Title string `json:"title" gorm:"size:255;not null" binding:"required"`
  31. Content string `json:"content" gorm:"type:text"`
  32. Category string `json:"category" gorm:"size:100;index"`
  33. Tags string `json:"tags" gorm:"type:text"` // JSON 数组格式存储
  34. Password string `json:"-" gorm:"size:255"` // 访问密码(哈希存储)
  35. IsPinned bool `json:"is_pinned" gorm:"default:false"`
  36. IsFavorite bool `json:"is_favorite" gorm:"default:false"`
  37. IsPublic bool `json:"is_public"` // 是否公开
  38. ParentID uint `json:"parent_id" gorm:"default:0;index"` // 父级目录 ID,0 表示根目录
  39. IsFolder bool `json:"is_folder" gorm:"default:false"` // 是否为文件夹
  40. SortOrder int `json:"sort_order" gorm:"default:0"` // 排序顺序
  41. CreatedAt time.Time `json:"created_at"`
  42. UpdatedAt time.Time `json:"updated_at"`
  43. }
  44. // NoteListItem 笔记列表响应(不含内容和密码)
  45. type NoteListItem struct {
  46. ID uint `json:"id"`
  47. Title string `json:"title"`
  48. Category string `json:"category"`
  49. Tags string `json:"tags"`
  50. HasPassword bool `json:"has_password"` // 是否有密码保护
  51. IsPinned bool `json:"is_pinned"`
  52. IsFavorite bool `json:"is_favorite"`
  53. IsPublic bool `json:"is_public"`
  54. ParentID uint `json:"parent_id"`
  55. IsFolder bool `json:"is_folder"`
  56. SortOrder int `json:"sort_order"`
  57. CreatedAt time.Time `json:"created_at"`
  58. UpdatedAt time.Time `json:"updated_at"`
  59. }
  60. // NoteCreateRequest 创建笔记请求
  61. type NoteCreateRequest struct {
  62. Title string `json:"title" binding:"required"`
  63. Content string `json:"content"`
  64. Category string `json:"category"`
  65. Tags string `json:"tags"`
  66. Password string `json:"password"`
  67. IsPinned *bool `json:"is_pinned"`
  68. IsFavorite *bool `json:"is_favorite"`
  69. IsPublic *bool `json:"is_public"`
  70. ParentID *uint `json:"parent_id"`
  71. IsFolder bool `json:"is_folder"`
  72. SortOrder int `json:"sort_order"`
  73. }
  74. // NoteUpdateRequest 更新笔记请求
  75. type NoteUpdateRequest struct {
  76. Title *string `json:"title"`
  77. Content *string `json:"content"`
  78. Category *string `json:"category"`
  79. Tags *string `json:"tags"`
  80. Password *string `json:"password"`
  81. RemovePassword *bool `json:"remove_password"` // 是否移除密码
  82. IsPinned *bool `json:"is_pinned"`
  83. IsFavorite *bool `json:"is_favorite"`
  84. IsPublic *bool `json:"is_public"`
  85. ParentID *uint `json:"parent_id"`
  86. IsFolder *bool `json:"is_folder"`
  87. SortOrder *int `json:"sort_order"`
  88. }
  89. // NoteAccessRequest 笔记访问请求(验证密码)
  90. type NoteAccessRequest struct {
  91. Password string `json:"password"`
  92. }