蛋蛋星球 后台端
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.

svc_list.go 3.3 KiB

5 days ago
5 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. tmp["list"] = list
  42. }
  43. resp.SelectData = append(resp.SelectData, tmp)
  44. }
  45. }
  46. resp.CateData = make([]map[string]interface{}, 0)
  47. cate, _, _ := NewArticleCateDb.FindArticleCateAndTotalPid("1", "100")
  48. if cate != nil {
  49. for _, v := range *cate {
  50. list := make([]map[string]string, 0)
  51. tmp := map[string]interface{}{
  52. "name": v.Name, "value": utils.IntToStr(v.Id), "list": list,
  53. }
  54. resp.CateData = append(resp.CateData, tmp)
  55. }
  56. }
  57. if notice != nil {
  58. for _, v := range *notice {
  59. tmp := md.ArticleList{
  60. CateId: utils.IntToStr(v.CateId),
  61. Title: v.Title,
  62. Content: v.Content,
  63. Cover: v.Cover,
  64. CoverUrl: svc.GetOssUrl(v.Cover),
  65. Id: utils.IntToStr(v.Id),
  66. Sort: utils.IntToStr(v.Sort),
  67. IsShow: utils.IntToStr(v.IsShow),
  68. Pid: utils.IntToStr(v.Pid),
  69. }
  70. noticeList = append(noticeList, tmp)
  71. }
  72. }
  73. resp.List = noticeList
  74. e.OutSuc(c, resp, nil)
  75. return
  76. }
  77. func Save(c *gin.Context) {
  78. var req *md.ArticleSaveReq
  79. if err := c.ShouldBindJSON(&req); err != nil {
  80. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  81. return
  82. }
  83. var data = new(model.Article)
  84. if utils.StrToInt(req.Id) > 0 {
  85. NewArticleDb := implement.NewArticleDb(db.Db)
  86. notice, _ := NewArticleDb.GetArticle(req.Id)
  87. if notice == nil {
  88. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  89. return
  90. }
  91. data = notice
  92. } else {
  93. data.CreatedAt = int(time.Now().Unix())
  94. db.Db.Insert(data)
  95. }
  96. data.UpdatedAt = int(time.Now().Unix())
  97. data.Title = req.Title
  98. data.Cover = req.Cover
  99. data.Content = req.Content
  100. data.TypeId = utils.StrToInt(req.Pid)
  101. data.CateId = utils.StrToInt(req.CateId)
  102. data.Sort = utils.StrToInt(req.Sort)
  103. data.Pid = utils.StrToInt(req.Pid)
  104. data.IsShow = utils.StrToInt(req.IsShow)
  105. db.Db.Where("id=?", data.Id).Update(data)
  106. e.OutSuc(c, "success", nil)
  107. return
  108. }
  109. func Del(c *gin.Context) {
  110. var req *md.ArticleDelReq
  111. if err := c.ShouldBindJSON(&req); err != nil {
  112. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  113. return
  114. }
  115. db.Db.In("id", req.Id).Delete(&model.Article{})
  116. e.OutSuc(c, "success", nil)
  117. return
  118. }