comment.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package community
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. "hongze/hongze_yb/logic/yb_community_question"
  6. "hongze/hongze_yb/models/request"
  7. responseModel "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/models/tables/yb_community_question_comment"
  9. "hongze/hongze_yb/services"
  10. userService "hongze/hongze_yb/services/user"
  11. "strconv"
  12. )
  13. // Comment 发布留言
  14. func Comment(c *gin.Context) {
  15. // 是否为备用小程序
  16. copyYb := c.Request.Header.Get("CopyYb")
  17. var req request.ReqComment
  18. if c.ShouldBind(&req) != nil {
  19. response.Fail("入参错误", c)
  20. return
  21. }
  22. userInfo := userService.GetInfoByClaims(c)
  23. // 随机头像
  24. var qaAvatarUrl string
  25. avatar, e := yb_community_question_comment.GetUserAvatarRandom()
  26. if e != nil {
  27. response.FailMsg("发布失败", e.Error(), c)
  28. return
  29. }
  30. qaAvatarUrl = avatar.AvatarUrl
  31. req.IsShowName = 0
  32. if userInfo.NickName != `` { //非空串时为实名
  33. req.IsShowName = 1
  34. }
  35. // 不传来源默认为问答社区
  36. if req.Source == 0 {
  37. req.Source = 1
  38. }
  39. ybCommunityQuestionComment, err, errMsg := yb_community_question.Comment(userInfo, req.CommunityQuestionID, req.Content, req.SourceAgent, req.IsShowName, req.Source, qaAvatarUrl, copyYb)
  40. if err != nil {
  41. response.FailMsg(errMsg, err.Error(), c)
  42. return
  43. }
  44. response.OkData("留言成功", responseModel.RespCommunityQuestionCommentAdd{
  45. CommunityQuestionCommentID: ybCommunityQuestionComment.CommunityQuestionCommentID,
  46. }, c)
  47. return
  48. }
  49. // DeleteComment 删除留言
  50. func DeleteComment(c *gin.Context) {
  51. var req *request.ReqDel
  52. if c.ShouldBind(&req) != nil {
  53. response.Fail("参数异常", c)
  54. return
  55. }
  56. userInfo := userService.GetInfoByClaims(c)
  57. err, errMsg := yb_community_question.Delete(userInfo, req.CommunityQuestionCommentID)
  58. if err != nil {
  59. response.FailMsg(errMsg, err.Error(), c)
  60. return
  61. }
  62. response.Ok("删除留言成功", c)
  63. return
  64. }
  65. // HotList 获取精选留言
  66. func HotList(c *gin.Context) {
  67. communityQuestionIDStr := c.DefaultQuery("community_question_id", "")
  68. communityQuestionID, err := strconv.Atoi(communityQuestionIDStr)
  69. if err != nil {
  70. response.Fail("问答id异常", c)
  71. return
  72. }
  73. // 来源: 默认为问答社区
  74. reqSource := c.DefaultQuery("source", "1")
  75. source, _ := strconv.Atoi(reqSource)
  76. if source == 0 {
  77. source = 1
  78. }
  79. userinfo := userService.GetInfoByClaims(c)
  80. page := services.GetCurrPageByClaims(c)
  81. pageSize := services.GetPageSizeByClaims(c)
  82. list, hotTotal, myTotal, err, errMsg := yb_community_question.List(userinfo.UserID, communityQuestionID, source, true, page, pageSize)
  83. if err != nil {
  84. response.FailMsg(errMsg, err.Error(), c)
  85. return
  86. }
  87. response.OkData("查询成功", responseModel.RespCommunityQuestionCommentList{
  88. List: list,
  89. Paging: responseModel.GetPaging(page, pageSize, int(hotTotal)),
  90. HotTotal: hotTotal,
  91. MyTotal: myTotal,
  92. }, c)
  93. return
  94. }
  95. // MyList 获取我的留言
  96. func MyList(c *gin.Context) {
  97. communityQuestionIDStr := c.DefaultQuery("community_question_id", "")
  98. communityQuestionID, err := strconv.Atoi(communityQuestionIDStr)
  99. if err != nil {
  100. response.Fail("问答id异常", c)
  101. return
  102. }
  103. // 来源: 默认为问答社区
  104. reqSource := c.DefaultQuery("source", "1")
  105. source, _ := strconv.Atoi(reqSource)
  106. if source == 0 {
  107. source = 1
  108. }
  109. userinfo := userService.GetInfoByClaims(c)
  110. page := services.GetCurrPageByClaims(c)
  111. pageSize := services.GetPageSizeByClaims(c)
  112. list, hotTotal, myTotal, err, errMsg := yb_community_question.MyList(userinfo.UserID, communityQuestionID, source, page, pageSize)
  113. if err != nil {
  114. response.FailMsg(errMsg, err.Error(), c)
  115. return
  116. }
  117. response.OkData("查询成功", responseModel.RespCommunityQuestionCommentList{
  118. List: list,
  119. Paging: responseModel.GetPaging(page, pageSize, int(myTotal)),
  120. HotTotal: hotTotal,
  121. MyTotal: myTotal,
  122. }, c)
  123. return
  124. }
  125. // GetNeedCommentAnonymousUserTips 获取是否 弹出让去设置头像 的提示框
  126. func GetNeedCommentAnonymousUserTips(c *gin.Context) {
  127. userinfo := userService.GetInfoByClaims(c)
  128. // true 不弹框,false弹框
  129. if userinfo.NickName != `` {
  130. response.OkData("获取成功", true, c)
  131. return
  132. }
  133. ok, err := yb_community_question.GetNeedCommentAnonymousUserTips(userinfo.UserID)
  134. if err != nil {
  135. response.FailMsg("获取失败", err.Error(), c)
  136. return
  137. }
  138. response.OkData("获取成功", ok, c)
  139. }
  140. // SetCommentAnonymousUserTips 设置不再提示 弹出让去设置头像 的提示框
  141. func SetCommentAnonymousUserTips(c *gin.Context) {
  142. userinfo := userService.GetInfoByClaims(c)
  143. err := yb_community_question.SetYbCommentAnonymousUserTips(userinfo.UserID)
  144. if err != nil {
  145. response.FailMsg("设置失败", err.Error(), c)
  146. return
  147. }
  148. response.Ok("设置成功", c)
  149. }