蛋蛋星球-客户端
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.
 
 
 
 
 
 

171 lines
4.4 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func CollegeCate(c *gin.Context) {
  13. NewArticleCateDb := implement.NewArticleCateDb(db.Db)
  14. cate, _ := NewArticleCateDb.FindArticleCate("1", "1000", "4")
  15. data := make([]md.CollegeCate, 0)
  16. if cate != nil {
  17. for _, v := range *cate {
  18. tmp := md.CollegeCate{
  19. Id: utils.IntToStr(v.Id),
  20. Name: v.Name,
  21. Img: "",
  22. }
  23. data = append(data, tmp)
  24. }
  25. }
  26. e.OutSuc(c, data, nil)
  27. return
  28. }
  29. func CollegeList(c *gin.Context) {
  30. eg := db.Db
  31. var req md.CollegeListReq
  32. err := c.ShouldBindJSON(&req)
  33. if err != nil {
  34. err = HandleValidateErr(err)
  35. err1 := err.(e.E)
  36. e.OutErr(c, err1.Code, err1.Error())
  37. return
  38. }
  39. NewArticleDb := implement.NewArticleDb(eg)
  40. article, _ := NewArticleDb.FindArticle(req.Page, req.Limit, "", req.CateId)
  41. data := make([]md.CollegeListResp, 0)
  42. if article != nil {
  43. for _, v := range *article {
  44. tmp := md.CollegeListResp{
  45. Id: utils.IntToStr(v.Id),
  46. Url: "",
  47. Title: v.Title,
  48. Cover: GetOssUrl(v.Cover),
  49. CreateAt: time.Unix(int64(v.CreatedAt), 0).Format("2006-01-02"),
  50. WatchCount: NumFormat(v.WatchCount),
  51. LikeCount: NumFormat(v.LikeCount),
  52. ForwardCount: NumFormat(v.ForwardCount),
  53. }
  54. data = append(data, tmp)
  55. }
  56. }
  57. e.OutSuc(c, data, nil)
  58. return
  59. }
  60. func CollegeDetail(c *gin.Context) {
  61. eg := db.Db
  62. var req md.CollegeDetailReq
  63. err := c.ShouldBindJSON(&req)
  64. if err != nil {
  65. err = HandleValidateErr(err)
  66. err1 := err.(e.E)
  67. e.OutErr(c, err1.Code, err1.Error())
  68. return
  69. }
  70. user := GetUser(c)
  71. NewArticleDb := implement.NewArticleDb(eg)
  72. article, _ := NewArticleDb.GetArticle(req.Id)
  73. data := md.CollegeDetailResp{}
  74. if article != nil {
  75. data = md.CollegeDetailResp{
  76. Id: utils.IntToStr(article.Id),
  77. Url: "",
  78. Title: article.Title,
  79. Content: article.Content,
  80. IsLike: "0",
  81. CreateAt: time.Unix(int64(article.CreatedAt), 0).Format("2006-01-02"),
  82. WatchCount: NumFormat(article.WatchCount),
  83. LikeCount: NumFormat(article.LikeCount),
  84. ForwardCount: NumFormat(article.ForwardCount),
  85. }
  86. count, _ := eg.Where("uid=? and article_id=?", user.Id, article.Id).Count(&model.ArticleUserLike{})
  87. if count > 0 {
  88. data.IsLike = "1"
  89. }
  90. }
  91. e.OutSuc(c, data, nil)
  92. return
  93. }
  94. func CollegeLikeAdd(c *gin.Context) {
  95. eg := db.Db
  96. var req md.CollegeDetailReq
  97. err := c.ShouldBindJSON(&req)
  98. if err != nil {
  99. err = HandleValidateErr(err)
  100. err1 := err.(e.E)
  101. e.OutErr(c, err1.Code, err1.Error())
  102. return
  103. }
  104. user := GetUser(c)
  105. NewArticleDb := implement.NewArticleDb(eg)
  106. article, _ := NewArticleDb.GetArticle(req.Id)
  107. if article == nil {
  108. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  109. return
  110. }
  111. count, _ := eg.Where("uid=? and article_id=?", user.Id, article.Id).Count(&model.ArticleUserLike{})
  112. if count > 0 {
  113. e.OutErr(c, 400, e.NewErr(400, "已点赞"))
  114. return
  115. }
  116. article.LikeCount++
  117. eg.Where("id=?", article.Id).Cols("like_count").Update(article)
  118. e.OutSuc(c, "success", nil)
  119. return
  120. }
  121. func CollegeLikeCancel(c *gin.Context) {
  122. eg := db.Db
  123. var req md.CollegeDetailReq
  124. err := c.ShouldBindJSON(&req)
  125. if err != nil {
  126. err = HandleValidateErr(err)
  127. err1 := err.(e.E)
  128. e.OutErr(c, err1.Code, err1.Error())
  129. return
  130. }
  131. user := GetUser(c)
  132. NewArticleDb := implement.NewArticleDb(eg)
  133. article, _ := NewArticleDb.GetArticle(req.Id)
  134. if article == nil {
  135. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  136. return
  137. }
  138. eg.Where("uid=? and article_id=?", user.Id, article.Id).Delete(&model.ArticleUserLike{})
  139. article.LikeCount--
  140. if article.LikeCount < 0 {
  141. article.LikeCount = 0
  142. }
  143. eg.Where("id=?", article.Id).Cols("like_count").Update(article)
  144. e.OutSuc(c, "success", nil)
  145. return
  146. }
  147. func CollegeShareAdd(c *gin.Context) {
  148. eg := db.Db
  149. var req md.CollegeDetailReq
  150. err := c.ShouldBindJSON(&req)
  151. if err != nil {
  152. err = HandleValidateErr(err)
  153. err1 := err.(e.E)
  154. e.OutErr(c, err1.Code, err1.Error())
  155. return
  156. }
  157. NewArticleDb := implement.NewArticleDb(eg)
  158. article, _ := NewArticleDb.GetArticle(req.Id)
  159. if article == nil {
  160. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  161. return
  162. }
  163. article.ForwardCount++
  164. eg.Where("id=?", article.Id).Cols("forward_count").Update(article)
  165. e.OutSuc(c, "success", nil)
  166. return
  167. }