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

123 lines
3.4 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. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "time"
  13. )
  14. func List(c *gin.Context) {
  15. var req *md.ArticleListReq
  16. if err := c.ShouldBindJSON(&req); err != nil {
  17. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  18. return
  19. }
  20. var resp md.ArticleListResp
  21. noticeList := make([]md.ArticleList, 0)
  22. NewArticleDb := implement.NewArticleDb(db.Db)
  23. notice, total, _ := NewArticleDb.FindArticleAndTotal(req.Page, req.Limit, "", "")
  24. resp.Total = total
  25. resp.SelectData = make([]map[string]interface{}, 0)
  26. NewArticleCateDb := implement.NewArticleCateDb(db.Db)
  27. pid, _ := NewArticleCateDb.FindArticleCate("1", "100", "0")
  28. if pid != nil {
  29. for _, v := range *pid {
  30. list := make([]map[string]string, 0)
  31. tmp := map[string]interface{}{
  32. "name": v.Name, "value": utils.IntToStr(v.Id), "list": list,
  33. }
  34. cate, _ := NewArticleCateDb.FindArticleCate("1", "1000", utils.IntToStr(v.Id))
  35. if cate != nil {
  36. for _, v1 := range *cate {
  37. tmp1 := map[string]string{
  38. "name": v1.Name, "value": utils.IntToStr(v1.Id),
  39. }
  40. list = append(list, tmp1)
  41. }
  42. tmp["list"] = list
  43. }
  44. resp.SelectData = append(resp.SelectData, tmp)
  45. }
  46. }
  47. resp.CateData = make([]map[string]interface{}, 0)
  48. cate, _, _ := NewArticleCateDb.FindArticleCateAndTotalPid("1", "100")
  49. if cate != nil {
  50. for _, v := range *cate {
  51. list := make([]map[string]string, 0)
  52. tmp := map[string]interface{}{
  53. "name": v.Name, "value": utils.IntToStr(v.Id), "list": list,
  54. }
  55. resp.CateData = append(resp.CateData, tmp)
  56. }
  57. }
  58. if notice != nil {
  59. for _, v := range *notice {
  60. tmp := md.ArticleList{
  61. CateId: utils.IntToStr(v.CateId),
  62. Title: v.Title,
  63. Content: v.Content,
  64. Cover: v.Cover,
  65. CoverUrl: svc.GetOssUrl(v.Cover),
  66. Id: utils.IntToStr(v.Id),
  67. Sort: utils.IntToStr(v.Sort),
  68. IsShow: utils.IntToStr(v.IsShow),
  69. Pid: utils.IntToStr(v.Pid),
  70. Url: fmt.Sprintf("%s%s?id=%s&is_hide=0", svc.GetSysCfgStr("wap_host"), "/#/pages/course-detail-page/course-detail-page", utils.AnyToString(v.Id)),
  71. }
  72. noticeList = append(noticeList, tmp)
  73. }
  74. }
  75. resp.List = noticeList
  76. e.OutSuc(c, resp, nil)
  77. return
  78. }
  79. func Save(c *gin.Context) {
  80. var req *md.ArticleSaveReq
  81. if err := c.ShouldBindJSON(&req); err != nil {
  82. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  83. return
  84. }
  85. var data = new(model.Article)
  86. if utils.StrToInt(req.Id) > 0 {
  87. NewArticleDb := implement.NewArticleDb(db.Db)
  88. notice, _ := NewArticleDb.GetArticle(req.Id)
  89. if notice == nil {
  90. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  91. return
  92. }
  93. data = notice
  94. } else {
  95. data.CreatedAt = int(time.Now().Unix())
  96. db.Db.Insert(data)
  97. }
  98. data.UpdatedAt = int(time.Now().Unix())
  99. data.Title = req.Title
  100. data.Cover = req.Cover
  101. data.Content = req.Content
  102. data.TypeId = utils.StrToInt(req.Pid)
  103. data.CateId = utils.StrToInt(req.CateId)
  104. data.Sort = utils.StrToInt(req.Sort)
  105. data.Pid = utils.StrToInt(req.Pid)
  106. data.IsShow = utils.StrToInt(req.IsShow)
  107. db.Db.Where("id=?", data.Id).Update(data)
  108. e.OutSuc(c, "success", nil)
  109. return
  110. }
  111. func Del(c *gin.Context) {
  112. var req *md.ArticleDelReq
  113. if err := c.ShouldBindJSON(&req); err != nil {
  114. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  115. return
  116. }
  117. db.Db.In("id", req.Id).Delete(&model.Article{})
  118. e.OutSuc(c, "success", nil)
  119. return
  120. }