蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

218 lines
7.0 KiB

  1. package hdl
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/pkg/pb"
  8. "applet/app/svc"
  9. "applet/app/utils"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  11. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  12. "github.com/gin-gonic/gin"
  13. "github.com/tidwall/gjson"
  14. )
  15. // SendRedPackageDetail
  16. // @Summary 蛋蛋星球-即时通讯-发送红包详情
  17. // @Tags 即时通讯
  18. // @Description 发送红包详情
  19. // @Accept json
  20. // @Produce json
  21. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  22. // @Param req body md.SendRedPackageDetailReq true "红包的订单 ID"
  23. // @Success 200 {object} md.SendRedPackageDetailResp "具体数据"
  24. // @Failure 400 {object} md.Response "具体错误"
  25. // @Router /api/v1/im/user/sendRedPackageDetail [POST]
  26. func SendRedPackageDetail(c *gin.Context) {
  27. var req *md.SendRedPackageDetailReq
  28. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  29. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  30. return
  31. }
  32. //1、查找红包
  33. redPackageOrdDb := implement.NewImSendRedPackageOrdDb(db.Db)
  34. imSendRedPackageOrd, err := redPackageOrdDb.ImSendRedPackageOrdGetOneByParams(map[string]interface{}{
  35. "key": "ord_no",
  36. "value": req.OrderId,
  37. })
  38. if err != nil {
  39. e.OutErr(c, e.ERR_DB_ORM, nil)
  40. return
  41. }
  42. var redPackageID int64
  43. im := md.SeqType{
  44. Seq: 0,
  45. }
  46. if imSendRedPackageOrd != nil {
  47. im.Seq = int(gjson.Get(imSendRedPackageOrd.ImData, "seq").Int())
  48. redPackageID = imSendRedPackageOrd.Id
  49. }
  50. resp := md.SendRedPackageDetailResp{
  51. Im: im,
  52. RedPackageID: redPackageID,
  53. }
  54. e.OutSuc(c, resp, nil)
  55. return
  56. }
  57. // GrabRedPackage
  58. // @Summary 蛋蛋星球-即时通讯-领取红包
  59. // @Tags 即时通讯
  60. // @Description 领取红包
  61. // @Accept json
  62. // @Produce json
  63. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  64. // @Param req body md.GrabRedPackageReq true "领取红包信息"
  65. // @Success 200 {object} md.GrabRedPackageResp "具体数据"
  66. // @Failure 400 {object} md.Response "具体错误"
  67. // @Router /api/v1/im/user/grabRedPackage [POST]
  68. func GrabRedPackage(c *gin.Context) {
  69. var req *md.GrabRedPackageReq
  70. if err1 := c.ShouldBindJSON(&req); err1 != nil {
  71. e.OutErr(c, e.ERR_INVALID_ARGS, err1.Error())
  72. return
  73. }
  74. user := svc.GetUser(c)
  75. redPackageContent, grabAmount, err := svc.DelGrabRedPackage(req, user)
  76. if err != nil {
  77. e.OutErr(c, e.ERR, err.Error())
  78. return
  79. }
  80. resp := md.GrabRedPackageResp{
  81. Im: &pb.SendRedPacketResp{Seq: 0},
  82. GrabAmount: grabAmount,
  83. }
  84. if redPackageContent == nil {
  85. e.OutSuc(c, resp, nil)
  86. return
  87. }
  88. //TODO::调用im GRPC 接口
  89. res, err := utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).SendRedPacket(utils.GetCtx(req.Token, req.UserId, req.DeviceId), &pb.SendRedPacketReq{
  90. ReceiverType: pb.ReceiverType(req.ReceiverType),
  91. ReceiverId: req.ReceiverId,
  92. MessageType: pb.MessageType_MT_RED_PACKAGE,
  93. MessageContent: redPackageContent,
  94. SendTime: req.SendTime,
  95. //ToUserIds: args.ToUserIds,
  96. MessageContentBack: "",
  97. })
  98. if err != nil {
  99. e.OutErr(c, e.ERR_IS_GRPC, err.Error())
  100. return
  101. }
  102. resp.Im = res
  103. e.OutSuc(c, resp, nil)
  104. return
  105. }
  106. // RedPackageDetail
  107. // @Summary 蛋蛋星球-即时通讯-红包详情(获取)
  108. // @Tags 即时通讯
  109. // @Description 红包详情(获取)
  110. // @Accept json
  111. // @Produce json
  112. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  113. // @Param red_package_id query string true "红包 ID"
  114. // @Success 200 {object} md.RedPackageDetailResp "具体数据"
  115. // @Failure 400 {object} md.Response "具体错误"
  116. // @Router /api/v1/im/redPackageDetail [GET]
  117. func RedPackageDetail(c *gin.Context) {
  118. redPackageId := c.DefaultQuery("red_package_id", "")
  119. res, imSendRedPackageOrd, err := svc.DetailGrabRedPackage(redPackageId)
  120. if err != nil {
  121. e.OutErr(c, e.ERR, err.Error())
  122. return
  123. }
  124. resp := md.RedPackageDetailResp{
  125. List: res,
  126. Detail: model.ImSendRedPackageOrd{
  127. OrdNo: imSendRedPackageOrd.OrdNo,
  128. Uid: imSendRedPackageOrd.Uid,
  129. ImUid: imSendRedPackageOrd.ImUid,
  130. Amount: imSendRedPackageOrd.Amount,
  131. RedPacketBalanceAmount: imSendRedPackageOrd.RedPacketBalanceAmount,
  132. RedPacketType: imSendRedPackageOrd.RedPacketType,
  133. RedPacketNums: imSendRedPackageOrd.RedPacketNums,
  134. RedPacketBalanceNums: imSendRedPackageOrd.RedPacketBalanceNums,
  135. State: imSendRedPackageOrd.State,
  136. WaitDrawUserIds: imSendRedPackageOrd.WaitDrawUserIds,
  137. WaitDrawImUserIds: imSendRedPackageOrd.WaitDrawImUserIds,
  138. ReceivedUserIds: imSendRedPackageOrd.ReceivedUserIds,
  139. ReceivedImUserIds: imSendRedPackageOrd.ReceivedImUserIds,
  140. ReceivedUserAmount: imSendRedPackageOrd.ReceivedUserAmount,
  141. ReceivedTimes: imSendRedPackageOrd.ReceivedTimes,
  142. CreateTime: imSendRedPackageOrd.CreateTime,
  143. UpdateTime: imSendRedPackageOrd.UpdateTime,
  144. },
  145. }
  146. e.OutSuc(c, resp, nil)
  147. return
  148. }
  149. // SendRedPackage
  150. // @Summary 蛋蛋星球-即时通讯-发送红包
  151. // @Tags 即时通讯
  152. // @Description 发送红包
  153. // @Accept json
  154. // @Produce json
  155. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  156. // @Param req body md.SendRedPackageReq true "发送红包信息"
  157. // @Success 200 {object} md.SendRedPackageResp "具体数据"
  158. // @Failure 400 {object} md.Response "具体错误"
  159. // @Router /api/v1/im/user/sendRedPackage [POST]
  160. func SendRedPackage(c *gin.Context) {
  161. var args md.SendRedPackageReq
  162. if err := c.ShouldBindJSON(&args); err != nil {
  163. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  164. return
  165. }
  166. if args.RedPacketNums <= 0 || utils.StrToFloat64(args.Amount)*100 <= 0 || utils.StrToFloat64(args.Amount)*100 < float64(args.RedPacketNums)*utils.RED_PACKET_MIN_MONEY {
  167. e.OutErr(c, e.ERR, "传参有误")
  168. return
  169. }
  170. user := svc.GetUser(c)
  171. redPackageContent, redPackageId, err := svc.BalancePayForRedPackage(user, args.Amount, args)
  172. if err != nil {
  173. e.OutErr(c, e.ERR, err)
  174. return
  175. }
  176. //TODO::调用im GRPC 接口
  177. res, err := utils.GetLogicExtClient(cfg.ImLogicRpc.URL, cfg.ImLogicRpc.PORT).SendRedPacket(utils.GetCtx(args.Token, args.UserId, args.DeviceId), &pb.SendRedPacketReq{
  178. ReceiverType: pb.ReceiverType(args.ReceiverType),
  179. ReceiverId: args.ReceiverId,
  180. MessageType: pb.MessageType_MT_RED_PACKAGE,
  181. MessageContent: redPackageContent,
  182. SendTime: args.SendTime,
  183. ToUserIds: args.ToUserIds,
  184. MessageContentBack: "",
  185. })
  186. if err != nil {
  187. e.OutErr(c, e.ERR_IS_GRPC, err)
  188. return
  189. }
  190. resp := md.SendRedPackageResp{
  191. Im: res,
  192. RedPackageId: redPackageId,
  193. }
  194. // 处理统计发红包数 推到 mq 处理
  195. go svc.AddSendRedPackageTimesPushToMQ(user.Id)
  196. e.OutSuc(c, resp, nil)
  197. return
  198. }