cms_news.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // ============================================================================
  2. // This is auto-generated by gf cli tool only once. Fill this file as you wish.
  3. // ============================================================================
  4. package cms_news
  5. import (
  6. "gfast/library/utils"
  7. "github.com/gogf/gf/errors/gerror"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/os/gtime"
  10. "github.com/gogf/gf/util/gconv"
  11. )
  12. // Fill with you ideas below.
  13. //添加文章参数
  14. type ReqAddParams struct {
  15. NewsStatus uint `p:"status" v:"in:0,1#状态只能为0或1"` // 状态;1:已发布;0:未发布;
  16. IsTop uint `p:"IsTop" v:"in:0,1#置顶只能为0或1"` // 是否置顶;1:置顶;0:不置顶
  17. Recommended uint `p:"recommended" v:"in:0,1#推荐只能为0或1"` // 是否推荐;1:推荐;0:不推荐
  18. PublishedTime string `p:"published_time"` // 发布时间
  19. NewsTitle string `p:"title" v:"required#标题不能为空"` // post标题
  20. NewsKeywords string `p:"keywords"` // seo keywords
  21. NewsExcerpt string `p:"excerpt"` // post摘要
  22. NewsSource string `p:"source" ` // 转载文章的来源
  23. NewsContent string `p:"content" v:"required#文章内容不能为空"` // 文章内容
  24. Thumbnail string `p:"thumbnail" ` // 缩略图
  25. IsJump uint `p:"IsJump" v:"in:0,1#跳转类型只能为0或1"` // 是否跳转地址
  26. JumpUrl string `p:"JumpUrl" ` // 跳转地址
  27. }
  28. //添加文章操作
  29. func AddNews(req *ReqAddParams, cateIds []int, userId int) (insId []int64, err error) {
  30. if len(cateIds) == 0 {
  31. err = gerror.New("栏目不能为空")
  32. return
  33. }
  34. tx, err := g.DB().Begin()
  35. if err != nil {
  36. g.Log().Error(err)
  37. err = gerror.New("添加失败")
  38. return
  39. }
  40. for _, cateId := range cateIds {
  41. entity := &Entity{
  42. CateId: gconv.Uint(cateId),
  43. UserId: gconv.Uint64(userId),
  44. NewsStatus: req.NewsStatus,
  45. IsTop: req.IsTop,
  46. Recommended: req.Recommended,
  47. CreateTime: gconv.Uint(gtime.Timestamp()),
  48. PublishedTime: gconv.Uint(utils.StrToTimestamp(req.PublishedTime)),
  49. NewsTitle: req.NewsTitle,
  50. NewsKeywords: req.NewsKeywords,
  51. NewsExcerpt: req.NewsExcerpt,
  52. NewsSource: req.NewsExcerpt,
  53. NewsContent: req.NewsContent,
  54. Thumbnail: req.Thumbnail,
  55. IsJump: req.IsJump,
  56. JumpUrl: req.JumpUrl,
  57. }
  58. res, e := entity.Save()
  59. if e != nil {
  60. g.Log().Error(e)
  61. err = gerror.New("添加文章失败")
  62. tx.Rollback()
  63. return
  64. }
  65. id, e := res.LastInsertId()
  66. if e != nil {
  67. g.Log().Error(e)
  68. err = gerror.New("添加文章失败")
  69. tx.Rollback()
  70. return
  71. }
  72. insId = append(insId, id)
  73. }
  74. tx.Commit()
  75. return
  76. }