ntf.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package request
  2. import (
  3. "errors"
  4. "mtp20access/global"
  5. "mtp20access/res/pb"
  6. "regexp"
  7. "strings"
  8. "github.com/golang/protobuf/jsonpb"
  9. "github.com/golang/protobuf/proto"
  10. "go.uber.org/zap"
  11. )
  12. type MQNtfReq struct {
  13. Topic string `json:"topic"` // 主题
  14. FunCode uint32 `json:"funCode" binding:"required"` // 功能码
  15. Data string `json:"data" binding:"required"` // 数据
  16. }
  17. // GetProtoBytes 将客户端请求的数据转换成总线使用的Protobuf
  18. func (r *MQNtfReq) GetProtoBytes() (bytes *[]byte, err error) {
  19. // JSON -> Protobuf
  20. switch int(r.FunCode) {
  21. case global.THJPurchaseTradeNtf:
  22. m := pb.THJPurchaseTradeNtf{}
  23. if err = r.reflect(r.Data, &m); err != nil {
  24. return
  25. }
  26. if m.Header != nil {
  27. m.Header.RequestID = new(uint32)
  28. *m.Header.RequestID = 0
  29. } else {
  30. err = errors.New("请求信息序列化失败")
  31. return
  32. }
  33. if b, e := proto.Marshal(&m); e != nil {
  34. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  35. err = errors.New("请求信息序列化失败")
  36. return
  37. } else {
  38. bytes = &b
  39. }
  40. case global.PurchaseTransferNtf:
  41. m := pb.PurchaseTransferNtf{}
  42. if err = r.reflect(r.Data, &m); err != nil {
  43. return
  44. }
  45. if m.Header != nil {
  46. m.Header.RequestID = new(uint32)
  47. *m.Header.RequestID = 0
  48. } else {
  49. err = errors.New("请求信息序列化失败")
  50. return
  51. }
  52. if b, e := proto.Marshal(&m); e != nil {
  53. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  54. err = errors.New("请求信息序列化失败")
  55. return
  56. } else {
  57. bytes = &b
  58. }
  59. case global.WRTradeDealedNtf:
  60. m := pb.WRTradeDealedNtf{}
  61. if err = r.reflect(r.Data, &m); err != nil {
  62. return
  63. }
  64. if m.Header != nil {
  65. m.Header.RequestID = new(uint32)
  66. *m.Header.RequestID = 0
  67. } else {
  68. err = errors.New("请求信息序列化失败")
  69. return
  70. }
  71. if b, e := proto.Marshal(&m); e != nil {
  72. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  73. err = errors.New("请求信息序列化失败")
  74. return
  75. } else {
  76. bytes = &b
  77. }
  78. }
  79. return
  80. }
  81. func (r *MQNtfReq) reflect(data string, m proto.Message) (err error) {
  82. // 由于JS对Long类型支持不佳,故让小程序端在使用单号时以字符串发过来,服务这边会自动把19位数字的字符串转化为数字
  83. reg, _ := regexp.Compile(`"[0-9]{19}"`)
  84. all := reg.FindAll([]byte(data), -1)
  85. for _, i := range all {
  86. r := strings.Replace(string(i), "\"", "", -1)
  87. data = strings.Replace(data, string(i), r, -1)
  88. }
  89. if e := jsonpb.UnmarshalString(data, m); e != nil {
  90. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  91. err = errors.New("业务数据装箱失败")
  92. return
  93. }
  94. return
  95. }