蛋蛋星球-客户端
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.5 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. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "time"
  12. )
  13. func CollegeCate(c *gin.Context) {
  14. NewArticleCateDb := implement.NewArticleCateDb(db.Db)
  15. cate, _ := NewArticleCateDb.FindArticleCate("1", "1000", "4")
  16. data := make([]md.CollegeCate, 0)
  17. if cate != nil {
  18. for _, v := range *cate {
  19. tmp := md.CollegeCate{
  20. Id: utils.IntToStr(v.Id),
  21. Name: v.Name,
  22. Img: "",
  23. }
  24. data = append(data, tmp)
  25. }
  26. }
  27. e.OutSuc(c, data, nil)
  28. return
  29. }
  30. func CollegeList(c *gin.Context) {
  31. eg := db.Db
  32. var req md.CollegeListReq
  33. err := c.ShouldBindJSON(&req)
  34. if err != nil {
  35. err = HandleValidateErr(err)
  36. err1 := err.(e.E)
  37. e.OutErr(c, err1.Code, err1.Error())
  38. return
  39. }
  40. NewArticleDb := implement.NewArticleDb(eg)
  41. article, _ := NewArticleDb.FindArticle(req.Page, req.Limit, "", req.CateId)
  42. data := make([]md.CollegeListResp, 0)
  43. if article != nil {
  44. for _, v := range *article {
  45. tmp := md.CollegeListResp{
  46. Id: utils.IntToStr(v.Id),
  47. Url: "",
  48. Title: v.Title,
  49. Cover: GetOssUrl(v.Cover),
  50. CreateAt: time.Unix(int64(v.CreatedAt), 0).Format("2006-01-02"),
  51. WatchCount: NumFormat(v.WatchCount),
  52. LikeCount: NumFormat(v.LikeCount),
  53. ForwardCount: NumFormat(v.ForwardCount),
  54. }
  55. data = append(data, tmp)
  56. }
  57. }
  58. e.OutSuc(c, data, nil)
  59. return
  60. }
  61. func CollegeDetail(c *gin.Context, uid int) {
  62. eg := db.Db
  63. var req md.CollegeDetailReq
  64. err := c.ShouldBindJSON(&req)
  65. if err != nil {
  66. err = HandleValidateErr(err)
  67. err1 := err.(e.E)
  68. e.OutErr(c, err1.Code, err1.Error())
  69. return
  70. }
  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: fmt.Sprintf("%s%s?id=%s&is_hide=1", GetSysCfgStr("wap_host"), "/#/pages/course-detail-page/course-detail-page", utils.AnyToString(article.Id)),
  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=?", uid, 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. }