蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

109 lines
2.9 KiB

  1. package article
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/svc"
  7. "applet/app/utils"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "github.com/gin-gonic/gin"
  11. "time"
  12. )
  13. func List(c *gin.Context) {
  14. var req *md.ArticleListReq
  15. if err := c.ShouldBindJSON(&req); err != nil {
  16. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  17. return
  18. }
  19. var resp md.ArticleListResp
  20. noticeList := make([]md.ArticleList, 0)
  21. NewArticleDb := implement.NewArticleDb(db.Db)
  22. notice, total, _ := NewArticleDb.FindArticleAndTotal(req.Page, req.Limit, "", "")
  23. resp.Total = total
  24. resp.SelectData = make([]map[string]interface{}, 0)
  25. NewArticleCateDb := implement.NewArticleCateDb(db.Db)
  26. pid, _ := NewArticleCateDb.FindArticleCate("1", "100", "0")
  27. if pid != nil {
  28. for _, v := range *pid {
  29. list := make([]map[string]string, 0)
  30. tmp := map[string]interface{}{
  31. "name": v.Name, "value": utils.IntToStr(v.Id), "list": list,
  32. }
  33. cate, _ := NewArticleCateDb.FindArticleCate("1", "1000", utils.IntToStr(v.Id))
  34. if cate != nil {
  35. for _, v1 := range *cate {
  36. tmp1 := map[string]string{
  37. "name": v1.Name, "value": utils.IntToStr(v1.Id),
  38. }
  39. list = append(list, tmp1)
  40. }
  41. }
  42. resp.SelectData = append(resp.SelectData, tmp)
  43. }
  44. }
  45. if notice != nil {
  46. for _, v := range *notice {
  47. tmp := md.ArticleList{
  48. CateId: utils.IntToStr(v.CateId),
  49. Title: v.Title,
  50. Content: v.Content,
  51. Cover: v.Cover,
  52. CoverUrl: svc.GetOssUrl(v.Cover),
  53. Id: utils.IntToStr(v.Id),
  54. Sort: utils.IntToStr(v.Sort),
  55. IsShow: utils.IntToStr(v.IsShow),
  56. Pid: utils.IntToStr(v.Pid),
  57. }
  58. noticeList = append(noticeList, tmp)
  59. }
  60. }
  61. resp.List = noticeList
  62. e.OutSuc(c, resp, nil)
  63. return
  64. }
  65. func Save(c *gin.Context) {
  66. var req *md.ArticleSaveReq
  67. if err := c.ShouldBindJSON(&req); err != nil {
  68. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  69. return
  70. }
  71. var data = new(model.Article)
  72. if utils.StrToInt(req.Id) > 0 {
  73. NewArticleDb := implement.NewArticleDb(db.Db)
  74. notice, _ := NewArticleDb.GetArticle(req.Id)
  75. if notice == nil {
  76. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  77. return
  78. }
  79. data = notice
  80. } else {
  81. data.CreatedAt = int(time.Now().Unix())
  82. db.Db.Insert(data)
  83. }
  84. data.UpdatedAt = int(time.Now().Unix())
  85. data.Title = req.Title
  86. data.Cover = req.Cover
  87. data.Content = req.Content
  88. data.TypeId = utils.StrToInt(req.Pid)
  89. data.CateId = utils.StrToInt(req.CateId)
  90. data.Sort = utils.StrToInt(req.Sort)
  91. data.Pid = utils.StrToInt(req.Pid)
  92. data.IsShow = utils.StrToInt(req.IsShow)
  93. db.Db.Where("id=?", data.Id).Update(data)
  94. e.OutSuc(c, "success", nil)
  95. return
  96. }
  97. func Del(c *gin.Context) {
  98. var req *md.ArticleDelReq
  99. if err := c.ShouldBindJSON(&req); err != nil {
  100. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  101. return
  102. }
  103. db.Db.In("id", req.Id).Delete(&model.Article{})
  104. e.OutSuc(c, "success", nil)
  105. return
  106. }