message.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package message
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. message2 "hongze/hongze_yb/models/request/message"
  6. "hongze/hongze_yb/services/message"
  7. userService "hongze/hongze_yb/services/user"
  8. "hongze/hongze_yb/utils"
  9. "strconv"
  10. "strings"
  11. )
  12. func List(c *gin.Context) {
  13. reqType := c.DefaultQuery("type", "0")
  14. reqPageIndex := c.DefaultQuery("current_index", "1")
  15. reqPageSize := c.DefaultQuery("page_size", strconv.Itoa(utils.PageSize20))
  16. if !strings.Contains("0,1,2", reqType) {
  17. response.Fail("请输入正确的消息类型", c)
  18. return
  19. }
  20. msgtype, err := strconv.Atoi(reqType)
  21. if err != nil {
  22. response.Fail("请输入正确的消息类型", c)
  23. return
  24. }
  25. pageIndex, err := strconv.Atoi(reqPageIndex)
  26. if err != nil {
  27. response.Fail("请输入正确的条数限制", c)
  28. return
  29. }
  30. pageSize, err := strconv.Atoi(reqPageSize)
  31. if err != nil {
  32. response.Fail("请输入正确的页码", c)
  33. return
  34. }
  35. userInfo := userService.GetInfoByClaims(c)
  36. data, err := message.GetList(userInfo, msgtype, pageIndex, pageSize)
  37. if err != nil {
  38. response.Fail(err.Error(), c)
  39. return
  40. }
  41. response.OkData("查询成功", data, c)
  42. return
  43. }
  44. // BatchRead 一键已读
  45. func BatchRead(c *gin.Context) {
  46. var req message2.ReqMessageBatch
  47. if c.ShouldBind(&req) !=nil {
  48. response.Fail("参数异常", c)
  49. return
  50. }
  51. if !strings.Contains("0,1,2", strconv.Itoa(req.Type)) {
  52. response.Fail("请输入正确的消息类型", c)
  53. return
  54. }
  55. userInfo := userService.GetInfoByClaims(c)
  56. err := message.BatchRead(userInfo, req)
  57. if err != nil {
  58. response.Fail(err.Error(), c)
  59. return
  60. }
  61. response.Ok("操作成功", c)
  62. return
  63. }
  64. // SingleRead 设置单条消息已读
  65. func SingleRead(c *gin.Context) {
  66. var req message2.ReqMessage
  67. if c.ShouldBind(&req) !=nil {
  68. response.Fail("参数异常", c)
  69. return
  70. }
  71. if req.MsgId <=0 {
  72. response.Fail("请输入正确的消息Id", c)
  73. return
  74. }
  75. userInfo := userService.GetInfoByClaims(c)
  76. err := message.SingleRead(userInfo, req)
  77. if err != nil {
  78. response.Fail(err.Error(), c)
  79. return
  80. }
  81. response.Ok("操作成功", c)
  82. return
  83. }
  84. // SingleDelete 删除单条消息
  85. func SingleDelete(c *gin.Context) {
  86. var req message2.ReqMessage
  87. if c.ShouldBind(&req) !=nil {
  88. response.Fail("参数异常", c)
  89. return
  90. }
  91. if req.MsgId <=0 {
  92. response.Fail("请输入正确的消息Id", c)
  93. return
  94. }
  95. userInfo := userService.GetInfoByClaims(c)
  96. err := message.SingleDelete(userInfo, req)
  97. if err != nil {
  98. response.Fail(err.Error(), c)
  99. return
  100. }
  101. response.Ok("操作成功", c)
  102. return
  103. }