蛋蛋星球-客户端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

172 rader
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. AppName: GetSysCfgStr("app_name"),
  81. IsLike: "0",
  82. CreateAt: time.Unix(int64(article.CreatedAt), 0).Format("2006-01-02"),
  83. WatchCount: NumFormat(article.WatchCount),
  84. LikeCount: NumFormat(article.LikeCount),
  85. ForwardCount: NumFormat(article.ForwardCount),
  86. }
  87. count, _ := eg.Where("uid=? and article_id=?", uid, article.Id).Count(&model.ArticleUserLike{})
  88. if count > 0 {
  89. data.IsLike = "1"
  90. }
  91. }
  92. e.OutSuc(c, data, nil)
  93. return
  94. }
  95. func CollegeLikeAdd(c *gin.Context) {
  96. eg := db.Db
  97. var req md.CollegeDetailReq
  98. err := c.ShouldBindJSON(&req)
  99. if err != nil {
  100. err = HandleValidateErr(err)
  101. err1 := err.(e.E)
  102. e.OutErr(c, err1.Code, err1.Error())
  103. return
  104. }
  105. user := GetUser(c)
  106. NewArticleDb := implement.NewArticleDb(eg)
  107. article, _ := NewArticleDb.GetArticle(req.Id)
  108. if article == nil {
  109. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  110. return
  111. }
  112. count, _ := eg.Where("uid=? and article_id=?", user.Id, article.Id).Count(&model.ArticleUserLike{})
  113. if count > 0 {
  114. e.OutErr(c, 400, e.NewErr(400, "已点赞"))
  115. return
  116. }
  117. article.LikeCount++
  118. eg.Where("id=?", article.Id).Cols("like_count").Update(article)
  119. e.OutSuc(c, "success", nil)
  120. return
  121. }
  122. func CollegeLikeCancel(c *gin.Context) {
  123. eg := db.Db
  124. var req md.CollegeDetailReq
  125. err := c.ShouldBindJSON(&req)
  126. if err != nil {
  127. err = HandleValidateErr(err)
  128. err1 := err.(e.E)
  129. e.OutErr(c, err1.Code, err1.Error())
  130. return
  131. }
  132. user := GetUser(c)
  133. NewArticleDb := implement.NewArticleDb(eg)
  134. article, _ := NewArticleDb.GetArticle(req.Id)
  135. if article == nil {
  136. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  137. return
  138. }
  139. eg.Where("uid=? and article_id=?", user.Id, article.Id).Delete(&model.ArticleUserLike{})
  140. article.LikeCount--
  141. if article.LikeCount < 0 {
  142. article.LikeCount = 0
  143. }
  144. eg.Where("id=?", article.Id).Cols("like_count").Update(article)
  145. e.OutSuc(c, "success", nil)
  146. return
  147. }
  148. func CollegeShareAdd(c *gin.Context) {
  149. eg := db.Db
  150. var req md.CollegeDetailReq
  151. err := c.ShouldBindJSON(&req)
  152. if err != nil {
  153. err = HandleValidateErr(err)
  154. err1 := err.(e.E)
  155. e.OutErr(c, err1.Code, err1.Error())
  156. return
  157. }
  158. NewArticleDb := implement.NewArticleDb(eg)
  159. article, _ := NewArticleDb.GetArticle(req.Id)
  160. if article == nil {
  161. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  162. return
  163. }
  164. article.ForwardCount++
  165. eg.Where("id=?", article.Id).Cols("forward_count").Update(article)
  166. e.OutSuc(c, "success", nil)
  167. return
  168. }