mq.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package mq
  2. import (
  3. "errors"
  4. "fmt"
  5. "mtp20access/client"
  6. "mtp20access/global"
  7. commonRequest "mtp20access/model/common/request"
  8. rsp "mtp20access/model/common/response"
  9. "mtp20access/model/mq/request"
  10. "mtp20access/model/mq/response"
  11. "mtp20access/rabbitmq"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. "go.uber.org/zap"
  15. )
  16. // SendMQ 向总线发送信息
  17. func SendMQ(c *gin.Context, req *request.MQBodyReq) (err error) {
  18. // 获取请求账号信息
  19. s, exists := c.Get("claims")
  20. if !exists {
  21. err = errors.New("获取请求账号信息异常")
  22. global.M2A_LOG.Error(err.Error(), zap.Error(err))
  23. return
  24. }
  25. claims := s.(*commonRequest.CustomClaims)
  26. // 获取登录账户信息
  27. t, exists := client.Clients[claims.SessionID]
  28. if !exists {
  29. err = errors.New("获取登录账户信息异常")
  30. global.M2A_LOG.Error(err.Error(), zap.Any("SessionID", claims.SessionID), zap.Error(err))
  31. return
  32. }
  33. // 将客户端请求的数据转换成总线使用的Protobuf
  34. serialNumber := t.GetSerialNumber()
  35. bytes, err := req.GetProtoBytes(&serialNumber, t.SessionID)
  36. if err != nil {
  37. return
  38. }
  39. if bytes == nil {
  40. err = errors.New("请求信息序列化失败")
  41. return
  42. }
  43. // 创建异步任务
  44. key := fmt.Sprintf("%v_%v_%v", t.SessionID, req.FunCodeRsp, serialNumber)
  45. global.M2A_LOG.Info("创建异步任务", zap.Any("SessionID", t.SessionID), zap.Any("serialNumber", serialNumber))
  46. // 银行服务相关的回复流水号是错误的,所以需要特殊处理
  47. if int(req.FunCodeReq) == global.T2bBankSignReq ||
  48. int(req.FunCodeReq) == global.T2bSMSVerificationCodeReq ||
  49. int(req.FunCodeReq) == global.T2bBankCancelSignReq ||
  50. int(req.FunCodeReq) == global.T2bBankWithdrawReq ||
  51. int(req.FunCodeReq) == global.T2bBankDepositReq {
  52. key = fmt.Sprintf("%v_%v", t.SessionID, req.FunCodeRsp)
  53. }
  54. asyncTask := client.AsyncTask{
  55. FuncodeRsp: req.FunCodeRsp,
  56. SerialNumber: serialNumber,
  57. Own: t,
  58. IsEncrypted: *req.IsEncrypted,
  59. }
  60. t.SetAsyncTask(&asyncTask, key)
  61. // 获取对应主题
  62. // topic, exists := global.M2A_FuncodeTopic[int(req.FunCodeReq)]
  63. topic := global.GetTopic(int(req.FunCodeReq))
  64. if topic == "" {
  65. global.M2A_LOG.Error(err.Error(), zap.Error(err))
  66. return
  67. }
  68. // 向总线发送业务信息
  69. packet := &client.MQPacket{
  70. FunCode: req.FunCodeReq,
  71. SessionId: uint32(claims.SessionID),
  72. Data: bytes,
  73. }
  74. go rabbitmq.Publish(topic, packet)
  75. // 阻塞线程等待总线回复或超时
  76. // 这里make时没有指定长度,所以是发送者与接收者的同步操作,发送者会阻塞,所以一般会在下面使用超时来解锁。
  77. asyncTask.Rsp = make(chan response.MQBodyRsp)
  78. select {
  79. case r := <-asyncTask.Rsp: // 总线回复
  80. rsp.OkWithData(r, c)
  81. case <-time.After(time.Second * 20): // 超时
  82. // 删除对应异步任务
  83. t.DeleteAsyncTask(key)
  84. rsp.FailWithCodeAndDetail(nil, 11, "业务超时", c)
  85. }
  86. asyncTask.Finish()
  87. return
  88. }