system_msg.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package v1
  2. import (
  3. "git.getensh.com/common/gopkgs/logger"
  4. "git.getensh.com/common/gopkgs/tasker/httptasker"
  5. "git.getensh.com/common/gopkgs/util"
  6. "github.com/gin-gonic/gin"
  7. "go.uber.org/zap"
  8. "net/http"
  9. "property-system-gateway/errors"
  10. param_v1 "property-system-gateway/param/v1"
  11. "property-system-gateway/pb"
  12. "property-system-gateway/pb/v1"
  13. "property-system-gateway/utils"
  14. )
  15. //
  16. // @Summary 设置消息为已读
  17. // @Description 设置消息为已读
  18. // @Tags 系统消息
  19. // @Accept json
  20. // @Produce json
  21. // @Param token header string true "token"
  22. // @Param body body v1.SystemMsgReadedBody true "信息"
  23. // @Success 200 {object} v1.VehicleAddResponse
  24. // @Failure 500 {object} base.HTTPError
  25. // @Router /api/v1/system_msg [put]
  26. func (c *Controller) SystemMsgReaded(ctx *gin.Context) {
  27. // 解析参数
  28. req := &param_v1.SystemMsgReadedRequest{}
  29. parseParamTask := func() error {
  30. err := util.ShouldBind(ctx, &req.Header, nil, nil, &req.SystemMsgReadedBody)
  31. if err != nil {
  32. logger.Error("func",
  33. zap.String("call", "util.ShouldBind"),
  34. zap.String("error", err.Error()))
  35. return errors.ParamsError
  36. }
  37. return nil
  38. }
  39. // 业务处理
  40. handleServiceTask := func() error {
  41. tokenInfo, err := utils.GetSubjectValue(ctx)
  42. if err != nil {
  43. return err
  44. }
  45. // 响应数据
  46. resp := param_v1.SystemMsgReadedResponse{}
  47. rpcReq := &v1.SystemMsgReadedRequest{
  48. GardenId: tokenInfo.GardenId,
  49. Id: req.Id,
  50. }
  51. _, err = pb.Garden.SystemMsgReaded(ctx, rpcReq)
  52. if err != nil {
  53. s, _ := json.MarshalToString(req)
  54. logger.Error("func",
  55. zap.String("call", "pb.Garden.SystemMsgReaded"),
  56. zap.String("params", s),
  57. zap.String("error", err.Error()))
  58. return errors.ErrorTransForm(err)
  59. }
  60. ctx.JSON(http.StatusOK, resp)
  61. return nil
  62. }
  63. // 执行任务
  64. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  65. }
  66. //
  67. // @Summary 获取未读消息列表
  68. // @Description 获取未读消息列表
  69. // @Tags 系统消息
  70. // @Accept json
  71. // @Produce json
  72. // @Param token header string true "token"
  73. // @Param page query int false " "
  74. // @Param page_size query int false " "
  75. // @Success 200 {object} v1.SystemMsgListResponse
  76. // @Failure 500 {object} base.HTTPError
  77. // @Router /api/v1/system_msg/list [get]
  78. func (c *Controller) SystemMsgList(ctx *gin.Context) {
  79. // 解析参数
  80. req := &param_v1.SystemMsgListRequest{}
  81. parseParamTask := func() error {
  82. err := util.ShouldBind(ctx, &req.Header, nil, &req.SystemMsgListQuery, nil)
  83. if err != nil {
  84. logger.Error("func",
  85. zap.String("call", "util.ShouldBind"),
  86. zap.String("error", err.Error()))
  87. return errors.ParamsError
  88. }
  89. return nil
  90. }
  91. // 业务处理
  92. handleServiceTask := func() error {
  93. tokenInfo, err := utils.GetSubjectValue(ctx)
  94. if err != nil {
  95. return err
  96. }
  97. // 响应数据
  98. resp := param_v1.SystemMsgListResponse{}
  99. rpcReq := &v1.SystemMsgListRequest{
  100. GardenId: tokenInfo.GardenId,
  101. Uid: tokenInfo.Uid,
  102. Page: req.Page,
  103. PageSize: req.PageSize,
  104. }
  105. rpcRsp, err := pb.Garden.SystemMsgList(ctx, rpcReq)
  106. if err != nil {
  107. s, _ := json.MarshalToString(req)
  108. logger.Error("func",
  109. zap.String("call", "pb.Garden.SystemMsgList"),
  110. zap.String("params", s),
  111. zap.String("error", err.Error()))
  112. return errors.ErrorTransForm(err)
  113. }
  114. if rpcRsp.List == nil {
  115. rpcRsp.List = make([]*v1.SystemMsgItem, 0)
  116. }
  117. resp.Data = *rpcRsp
  118. ctx.JSON(http.StatusOK, resp)
  119. return nil
  120. }
  121. // 执行任务
  122. httptasker.Exec(ctx, parseParamTask, handleServiceTask)
  123. }