comment.go 4.6 KB

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