rabbitmq.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. package initialize
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "mtp20access/global"
  6. rsp "mtp20access/model/mq/response"
  7. "mtp20access/packet"
  8. "mtp20access/res/pb"
  9. "mtp20access/utils"
  10. "github.com/golang/protobuf/proto"
  11. "github.com/streadway/amqp"
  12. "go.uber.org/zap"
  13. "google.golang.org/protobuf/encoding/protojson"
  14. )
  15. func RabbitMQ() *global.RabbitMQ {
  16. url := global.M2A_CONFIG.Rabbitmq.Url
  17. connection, err := amqp.Dial(url)
  18. if err != nil {
  19. global.M2A_LOG.Error("rabbitmq connect failed, err:", zap.Error(err))
  20. return nil
  21. }
  22. channel, err := connection.Channel()
  23. if err != nil {
  24. global.M2A_LOG.Error("rabbitmq open channel failed, err:", zap.Error(err))
  25. return nil
  26. }
  27. global.M2A_LOG.Info("rabbitmq connect successed.")
  28. return &global.RabbitMQ{
  29. Connection: connection,
  30. Channel: channel,
  31. }
  32. }
  33. // MQProc 消息处理对象
  34. type MQProc struct{}
  35. // process 消息处理接口
  36. func (t *MQProc) Process(topic, queuename string, msg *[]byte) {
  37. // info := fmt.Sprintf("rabbitmq receive message from: topic[%s] queue[%s] contentLen[%d]",
  38. // topic,
  39. // queuename,
  40. // len(string(*msg)))
  41. // global.M2A_LOG.Info(info)
  42. if funcode, sessionId, bytes, serialNumber, err := t.getRspProtobuf(msg); err == nil && bytes != nil {
  43. // 尝试获取对应异步任务
  44. if client, exists := global.M2A_Clients[int(sessionId)]; exists {
  45. key := fmt.Sprintf("%v_%v_%v", sessionId, funcode, serialNumber)
  46. asyncTask := client.GetAsyncTask(key)
  47. if asyncTask != nil {
  48. rspData := string(*bytes)
  49. // 判断是否要加密
  50. if asyncTask.IsEncrypted {
  51. if b, err := packet.Encrypt(*bytes, packet.AESKey, true); err != nil {
  52. global.M2A_LOG.Error("总线回复数据加密失败", zap.Error(err))
  53. return
  54. } else {
  55. rspData = base64.StdEncoding.EncodeToString(b)
  56. }
  57. }
  58. // 给客户端回调
  59. global.M2A_LOG.Info("[S->C]", zap.Any("rsp", funcode), zap.Any("data", string(rspData)))
  60. r := rsp.MQBodyRsp{
  61. FunCode: funcode,
  62. IsEncrypted: asyncTask.IsEncrypted,
  63. Data: rspData,
  64. }
  65. asyncTask.Rsp <- r
  66. }
  67. }
  68. }
  69. }
  70. // getRspProtobuf 将总线回复的数据反序列化为Protobuf
  71. func (t *MQProc) getRspProtobuf(msg *[]byte) (funcode uint32, sessionId uint32, bytes *[]byte, serialNumber uint32, err error) {
  72. // 分解总线包信息
  73. funcode = utils.BytesToUint32((*msg)[0:4])
  74. sessionId = utils.BytesToUint32((*msg)[4:8])
  75. b := (*msg)[8:]
  76. switch int(funcode) {
  77. case global.ModifyPwdRsp: // 修改账户密码应答
  78. var p pb.ModifyPwdRsp
  79. if err = proto.Unmarshal(b, &p); err != nil {
  80. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  81. return
  82. }
  83. if bs, e := protojson.Marshal(&p); e != nil {
  84. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  85. return
  86. } else {
  87. bytes = &bs
  88. serialNumber = p.GetHeader().GetRequestID()
  89. }
  90. case global.UserReceiveInfoRsp: // 新增修改收货地址请求响应
  91. var p pb.UserReceiveInfoRsp
  92. if err = proto.Unmarshal(b, &p); err != nil {
  93. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  94. return
  95. }
  96. if bs, e := protojson.Marshal(&p); e != nil {
  97. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  98. return
  99. } else {
  100. bytes = &bs
  101. serialNumber = p.GetHeader().GetRequestID()
  102. }
  103. case global.DelUserReceiveInfoRsp: // 删除收货地址请求响应
  104. var p pb.DelUserReceiveInfoRsp
  105. if err = proto.Unmarshal(b, &p); err != nil {
  106. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  107. return
  108. }
  109. if bs, e := protojson.Marshal(&p); e != nil {
  110. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  111. return
  112. } else {
  113. bytes = &bs
  114. serialNumber = p.GetHeader().GetRequestID()
  115. }
  116. case global.UserReceiptInfoRsp: // 新增修改用户发票信息请求响应
  117. var p pb.UserReceiptInfoRsp
  118. if err = proto.Unmarshal(b, &p); err != nil {
  119. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  120. return
  121. }
  122. if bs, e := protojson.Marshal(&p); e != nil {
  123. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  124. return
  125. } else {
  126. bytes = &bs
  127. serialNumber = p.GetHeader().GetRequestID()
  128. }
  129. case global.DelUserReceiptInfoRsp: // 删除用户发票信息请求响应
  130. var p pb.DelUserReceiptInfoRsp
  131. if err = proto.Unmarshal(b, &p); err != nil {
  132. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  133. return
  134. }
  135. if bs, e := protojson.Marshal(&p); e != nil {
  136. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  137. return
  138. } else {
  139. bytes = &bs
  140. serialNumber = p.GetHeader().GetRequestID()
  141. }
  142. case global.T2bBankSignRsp: // 签约应答
  143. var p pb.T2BBankSignRsp
  144. if err = proto.Unmarshal(b, &p); err != nil {
  145. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  146. return
  147. }
  148. if bs, e := protojson.Marshal(&p); e != nil {
  149. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  150. return
  151. } else {
  152. bytes = &bs
  153. serialNumber = p.GetHeader().GetRequestID()
  154. }
  155. case global.T2bBankCancelSignRsp: // 解约应答
  156. var p pb.T2BBankCancelSignRsp
  157. if err = proto.Unmarshal(b, &p); err != nil {
  158. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  159. return
  160. }
  161. if bs, e := protojson.Marshal(&p); e != nil {
  162. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  163. return
  164. } else {
  165. bytes = &bs
  166. serialNumber = p.GetHeader().GetRequestID()
  167. }
  168. case global.T2bBankWithdrawRsp: // 出金应答
  169. var p pb.T2BBankWithdrawRsp
  170. if err = proto.Unmarshal(b, &p); err != nil {
  171. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  172. return
  173. }
  174. if bs, e := protojson.Marshal(&p); e != nil {
  175. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  176. return
  177. } else {
  178. bytes = &bs
  179. serialNumber = p.GetHeader().GetRequestID()
  180. }
  181. case global.T2bBankDepositRsp: // 入金应答
  182. var p pb.T2BBankDepositRsp
  183. if err = proto.Unmarshal(b, &p); err != nil {
  184. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  185. return
  186. }
  187. if bs, e := protojson.Marshal(&p); e != nil {
  188. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  189. return
  190. } else {
  191. bytes = &bs
  192. serialNumber = p.GetHeader().GetRequestID()
  193. }
  194. case global.WarehouseApplyRsp: // 仓库申请应答
  195. var p pb.WarehouseApplyRsp
  196. if err = proto.Unmarshal(b, &p); err != nil {
  197. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  198. return
  199. }
  200. if bs, e := protojson.Marshal(&p); e != nil {
  201. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  202. return
  203. } else {
  204. bytes = &bs
  205. serialNumber = p.GetHeader().GetRequestID()
  206. }
  207. case global.PerformanceContractedApplyRsp: // 违约申请应答
  208. var p pb.PerformanceContractedApplyRsp
  209. if err = proto.Unmarshal(b, &p); err != nil {
  210. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  211. return
  212. }
  213. if bs, e := protojson.Marshal(&p); e != nil {
  214. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  215. return
  216. } else {
  217. bytes = &bs
  218. serialNumber = p.GetHeader().GetRequestID()
  219. }
  220. case global.PerformanceDelayApplyRsp: // 延期申请应答
  221. var p pb.PerformanceDelayApplyRsp
  222. if err = proto.Unmarshal(b, &p); err != nil {
  223. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  224. return
  225. }
  226. if bs, e := protojson.Marshal(&p); e != nil {
  227. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  228. return
  229. } else {
  230. bytes = &bs
  231. serialNumber = p.GetHeader().GetRequestID()
  232. }
  233. case global.PerformanceManualConfirmRsp: // 履约手动确认应答
  234. var p pb.PerformanceManualConfirmRsp
  235. if err = proto.Unmarshal(b, &p); err != nil {
  236. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  237. return
  238. }
  239. if bs, e := protojson.Marshal(&p); e != nil {
  240. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  241. return
  242. } else {
  243. bytes = &bs
  244. serialNumber = p.GetHeader().GetRequestID()
  245. }
  246. case global.PerformanceModifyContactRsp: // 履约修改联络信息回应
  247. var p pb.PerformanceModifyContactRsp
  248. if err = proto.Unmarshal(b, &p); err != nil {
  249. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  250. return
  251. }
  252. if bs, e := protojson.Marshal(&p); e != nil {
  253. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  254. return
  255. } else {
  256. bytes = &bs
  257. serialNumber = p.GetHeader().GetRequestID()
  258. }
  259. case global.GoodsFavoriteOperateRsp: // 商品收藏操作接口应答
  260. var p pb.GoodsFavoriteOperateRsp
  261. if err = proto.Unmarshal(b, &p); err != nil {
  262. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  263. return
  264. }
  265. if bs, e := protojson.Marshal(&p); e != nil {
  266. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  267. return
  268. } else {
  269. bytes = &bs
  270. serialNumber = p.GetHeader().GetRequestID()
  271. }
  272. case global.ZSBuyOrderListingRsp: // 钻石买挂牌接口应答
  273. var p pb.ZSBuyOrderListingRsp
  274. if err = proto.Unmarshal(b, &p); err != nil {
  275. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  276. return
  277. }
  278. if bs, e := protojson.Marshal(&p); e != nil {
  279. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  280. return
  281. } else {
  282. bytes = &bs
  283. serialNumber = p.GetHeader().GetRequestID()
  284. }
  285. case global.ZSSellOrderListingRsp: // 钻石卖挂牌接口响应
  286. var p pb.ZSSellOrderListingRsp
  287. if err = proto.Unmarshal(b, &p); err != nil {
  288. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  289. return
  290. }
  291. if bs, e := protojson.Marshal(&p); e != nil {
  292. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  293. return
  294. } else {
  295. bytes = &bs
  296. serialNumber = p.GetHeader().GetRequestID()
  297. }
  298. case global.ZSBuyOrderDestingRsp: // 钻石卖摘牌申请接口响应
  299. var p pb.ZSBuyOrderDestingRsp
  300. if err = proto.Unmarshal(b, &p); err != nil {
  301. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  302. return
  303. }
  304. if bs, e := protojson.Marshal(&p); e != nil {
  305. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  306. return
  307. } else {
  308. bytes = &bs
  309. serialNumber = p.GetHeader().GetRequestID()
  310. }
  311. case global.ZSSellOrderDestingApplyOperateRsp: // 钻石卖摘牌申请操作接口应答
  312. var p pb.ZSSellOrderDestingApplyOperateRsp
  313. if err = proto.Unmarshal(b, &p); err != nil {
  314. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  315. return
  316. }
  317. if bs, e := protojson.Marshal(&p); e != nil {
  318. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  319. return
  320. } else {
  321. bytes = &bs
  322. serialNumber = p.GetHeader().GetRequestID()
  323. }
  324. case global.ZSBuyOrderDestingNegPriceRsp: // 买摘牌询价接口应答
  325. var p pb.ZSBuyOrderDestingNegPriceRsp
  326. if err = proto.Unmarshal(b, &p); err != nil {
  327. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  328. return
  329. }
  330. if bs, e := protojson.Marshal(&p); e != nil {
  331. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  332. return
  333. } else {
  334. bytes = &bs
  335. serialNumber = p.GetHeader().GetRequestID()
  336. }
  337. case global.ZSBuyOrderDestingNegPriceOperateRsp: // 买摘牌询价操作接口应答
  338. var p pb.ZSBuyOrderDestingNegPriceOperateRsp
  339. if err = proto.Unmarshal(b, &p); err != nil {
  340. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  341. return
  342. }
  343. if bs, e := protojson.Marshal(&p); e != nil {
  344. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  345. return
  346. } else {
  347. bytes = &bs
  348. serialNumber = p.GetHeader().GetRequestID()
  349. }
  350. case global.WRListingCancelOrderRsp: // 挂牌撤单应答
  351. var p pb.WRListingCancelOrderRsp
  352. if err = proto.Unmarshal(b, &p); err != nil {
  353. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  354. return
  355. }
  356. if bs, e := protojson.Marshal(&p); e != nil {
  357. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  358. return
  359. } else {
  360. bytes = &bs
  361. serialNumber = p.GetHeader().GetRequestID()
  362. }
  363. case global.ReceiptZSOutApplyRsp: // 钻石出库申请接口响应
  364. var p pb.ReceiptZSOutApplyRsp
  365. if err = proto.Unmarshal(b, &p); err != nil {
  366. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  367. return
  368. }
  369. if bs, e := protojson.Marshal(&p); e != nil {
  370. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  371. return
  372. } else {
  373. bytes = &bs
  374. serialNumber = p.GetHeader().GetRequestID()
  375. }
  376. case global.SpotPresaleListingOrderRsp: // 铁合金现货预售挂牌接口应答
  377. var p pb.SpotPresaleListingOrderRsp
  378. if err = proto.Unmarshal(b, &p); err != nil {
  379. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  380. return
  381. }
  382. if bs, e := protojson.Marshal(&p); e != nil {
  383. global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
  384. return
  385. } else {
  386. bytes = &bs
  387. serialNumber = p.GetHeader().GetRequestID()
  388. }
  389. }
  390. return
  391. }
  392. // RabbitMQSubscribeTopic 订阅主题
  393. func RabbitMQSubscribeTopic() (err error) {
  394. // 订阅需要的总线响应主题
  395. if err = global.SubscribeTopic(global.TOPIC_RSP_QKERNEL); err != nil {
  396. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  397. return
  398. }
  399. if err = global.SubscribeTopic(global.TOPIC_MANAGE_RSP); err != nil {
  400. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  401. return
  402. }
  403. if err = global.SubscribeTopic(global.TOPIC_RSP_BANK); err != nil {
  404. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  405. return
  406. }
  407. if err = global.SubscribeTopic(global.TOPIC_PERFORMANCE_RSP); err != nil {
  408. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  409. return
  410. }
  411. if err = global.SubscribeTopic(global.TOPIC_WAREHOUSE_RECIEPT_RSP); err != nil {
  412. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  413. return
  414. }
  415. if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_TRADE); err != nil {
  416. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  417. return
  418. }
  419. if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_TRADE_GZ); err != nil {
  420. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  421. return
  422. }
  423. // 铁合金
  424. if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_PRESALE_THJ); err != nil {
  425. global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
  426. return
  427. }
  428. global.M2A_LOG.Info("rabbitmq subscribe topic successed.")
  429. return
  430. }
  431. // StartRabbitMQReceive 开始接收总线消息
  432. func StartRabbitMQReceive() {
  433. t := &MQProc{}
  434. go func() {
  435. for _, subinfo := range global.SubInfos {
  436. global.Receive(subinfo.Topic, subinfo.QueueName, t)
  437. }
  438. }()
  439. }
  440. // InitFuncodeTopic 初始化功能码主题MAP
  441. func InitFuncodeTopic() {
  442. if global.M2A_FuncodeTopic == nil {
  443. global.M2A_FuncodeTopic = make(map[string][]int)
  444. }
  445. global.M2A_FuncodeTopic[global.TOPIC_REQ_QKERNEL] = []int{
  446. global.ModifyPwdReq,
  447. }
  448. global.M2A_FuncodeTopic[global.TOPIC_MANAGE_REQ] = []int{
  449. global.UserReceiveInfoReq,
  450. global.DelUserReceiveInfoReq,
  451. global.UserReceiveIsDefaultReq,
  452. global.UserReceiptInfoReq,
  453. global.DelUserReceiptInfoReq,
  454. global.WarehouseApplyReq,
  455. }
  456. global.M2A_FuncodeTopic[global.TOPIC_REQ_BANK] = []int{
  457. global.T2bBankSignReq,
  458. global.T2bBankCancelSignReq,
  459. global.T2bBankWithdrawReq,
  460. global.T2bBankDepositReq,
  461. }
  462. global.M2A_FuncodeTopic[global.TOPIC_PERFORMANCE_REQ] = []int{
  463. global.PerformanceContractedApplyReq,
  464. global.PerformanceDelayApplyReq,
  465. global.PerformanceManualConfirmReq,
  466. global.PerformanceModifyContactReq,
  467. }
  468. global.M2A_FuncodeTopic[global.TOPIC_WAREHOUSE_RECIEPT] = []int{
  469. global.ReceiptZSOutApplyReq,
  470. }
  471. global.M2A_FuncodeTopic[global.TOPIC_REQ_WAREHOUSE_TRADE] = []int{
  472. global.WRListingCancelOrderReq,
  473. }
  474. global.M2A_FuncodeTopic[global.TOPIC_REQ_WAREHOUSE_TRADE_GZ] = []int{
  475. global.GoodsFavoriteOperateReq,
  476. global.ZSBuyOrderListingReq,
  477. global.ZSSellOrderListingReq,
  478. global.ZSBuyOrderDestingReq,
  479. global.ZSSellOrderDestingApplyReq,
  480. global.ZSSellOrderDestingApplyOperateReq,
  481. global.ZSBuyOrderDestingNegPriceReq,
  482. global.ZSBuyOrderDestingNegPriceOperateReq,
  483. }
  484. // 铁合金
  485. global.M2A_FuncodeTopic[global.TOPIC_REQ_WAREHOUSE_PRESALE_THJ] = []int{
  486. global.SpotPresaleListingOrderReq,
  487. }
  488. }