| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- package initialize
- import (
- "encoding/base64"
- "fmt"
- "mtp20access/global"
- rsp "mtp20access/model/mq/response"
- "mtp20access/packet"
- "mtp20access/res/pb"
- "mtp20access/utils"
- "github.com/golang/protobuf/proto"
- "github.com/streadway/amqp"
- "go.uber.org/zap"
- "google.golang.org/protobuf/encoding/protojson"
- )
- func RabbitMQ() *global.RabbitMQ {
- url := global.M2A_CONFIG.Rabbitmq.Url
- connection, err := amqp.Dial(url)
- if err != nil {
- global.M2A_LOG.Error("rabbitmq connect failed, err:", zap.Error(err))
- return nil
- }
- channel, err := connection.Channel()
- if err != nil {
- global.M2A_LOG.Error("rabbitmq open channel failed, err:", zap.Error(err))
- return nil
- }
- global.M2A_LOG.Info("rabbitmq connect successed.")
- return &global.RabbitMQ{
- Connection: connection,
- Channel: channel,
- }
- }
- // MQProc 消息处理对象
- type MQProc struct{}
- // process 消息处理接口
- func (t *MQProc) Process(topic, queuename string, msg *[]byte) {
- // info := fmt.Sprintf("rabbitmq receive message from: topic[%s] queue[%s] contentLen[%d]",
- // topic,
- // queuename,
- // len(string(*msg)))
- // global.M2A_LOG.Info(info)
- if funcode, sessionId, bytes, serialNumber, err := t.getRspProtobuf(msg); err == nil && bytes != nil {
- // 尝试获取对应异步任务
- if client, exists := global.M2A_Clients[int(sessionId)]; exists {
- key := fmt.Sprintf("%v_%v_%v", sessionId, funcode, serialNumber)
- asyncTask := client.GetAsyncTask(key)
- if asyncTask != nil {
- rspData := string(*bytes)
- // 判断是否要加密
- if asyncTask.IsEncrypted {
- if b, err := packet.Encrypt(*bytes, packet.AESKey, true); err != nil {
- global.M2A_LOG.Error("总线回复数据加密失败", zap.Error(err))
- return
- } else {
- rspData = base64.StdEncoding.EncodeToString(b)
- }
- }
- // 给客户端回调
- global.M2A_LOG.Info("[S->C]", zap.Any("rsp", funcode), zap.Any("data", string(rspData)))
- r := rsp.MQBodyRsp{
- FunCode: funcode,
- IsEncrypted: asyncTask.IsEncrypted,
- Data: rspData,
- }
- asyncTask.Rsp <- r
- }
- }
- }
- }
- // getRspProtobuf 将总线回复的数据反序列化为Protobuf
- func (t *MQProc) getRspProtobuf(msg *[]byte) (funcode uint32, sessionId uint32, bytes *[]byte, serialNumber uint32, err error) {
- // 分解总线包信息
- funcode = utils.BytesToUint32((*msg)[0:4])
- sessionId = utils.BytesToUint32((*msg)[4:8])
- b := (*msg)[8:]
- switch int(funcode) {
- case global.ModifyPwdRsp: // 修改账户密码应答
- var p pb.ModifyPwdRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.UserReceiveInfoRsp: // 新增修改收货地址请求响应
- var p pb.UserReceiveInfoRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.DelUserReceiveInfoRsp: // 删除收货地址请求响应
- var p pb.DelUserReceiveInfoRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.UserReceiptInfoRsp: // 新增修改用户发票信息请求响应
- var p pb.UserReceiptInfoRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.DelUserReceiptInfoRsp: // 删除用户发票信息请求响应
- var p pb.DelUserReceiptInfoRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.T2bBankSignRsp: // 签约应答
- var p pb.T2BBankSignRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.T2bBankCancelSignRsp: // 解约应答
- var p pb.T2BBankCancelSignRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.T2bBankWithdrawRsp: // 出金应答
- var p pb.T2BBankWithdrawRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.T2bBankDepositRsp: // 入金应答
- var p pb.T2BBankDepositRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.WarehouseApplyRsp: // 仓库申请应答
- var p pb.WarehouseApplyRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.PerformanceContractedApplyRsp: // 违约申请应答
- var p pb.PerformanceContractedApplyRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.PerformanceDelayApplyRsp: // 延期申请应答
- var p pb.PerformanceDelayApplyRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.PerformanceManualConfirmRsp: // 履约手动确认应答
- var p pb.PerformanceManualConfirmRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.PerformanceModifyContactRsp: // 履约修改联络信息回应
- var p pb.PerformanceModifyContactRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.GoodsFavoriteOperateRsp: // 商品收藏操作接口应答
- var p pb.GoodsFavoriteOperateRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.ZSBuyOrderListingRsp: // 钻石买挂牌接口应答
- var p pb.ZSBuyOrderListingRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.ZSSellOrderListingRsp: // 钻石卖挂牌接口响应
- var p pb.ZSSellOrderListingRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.ZSBuyOrderDestingRsp: // 钻石卖摘牌申请接口响应
- var p pb.ZSBuyOrderDestingRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.ZSSellOrderDestingApplyOperateRsp: // 钻石卖摘牌申请操作接口应答
- var p pb.ZSSellOrderDestingApplyOperateRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.ZSBuyOrderDestingNegPriceRsp: // 买摘牌询价接口应答
- var p pb.ZSBuyOrderDestingNegPriceRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.ZSBuyOrderDestingNegPriceOperateRsp: // 买摘牌询价操作接口应答
- var p pb.ZSBuyOrderDestingNegPriceOperateRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.WRListingCancelOrderRsp: // 挂牌撤单应答
- var p pb.WRListingCancelOrderRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.ReceiptZSOutApplyRsp: // 钻石出库申请接口响应
- var p pb.ReceiptZSOutApplyRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- case global.SpotPresaleListingOrderRsp: // 铁合金现货预售挂牌接口应答
- var p pb.SpotPresaleListingOrderRsp
- if err = proto.Unmarshal(b, &p); err != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- }
- if bs, e := protojson.Marshal(&p); e != nil {
- global.M2A_LOG.Error("总线回复数据反序列化失败", zap.Error(err))
- return
- } else {
- bytes = &bs
- serialNumber = p.GetHeader().GetRequestID()
- }
- }
- return
- }
- // RabbitMQSubscribeTopic 订阅主题
- func RabbitMQSubscribeTopic() (err error) {
- // 订阅需要的总线响应主题
- if err = global.SubscribeTopic(global.TOPIC_RSP_QKERNEL); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- if err = global.SubscribeTopic(global.TOPIC_MANAGE_RSP); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- if err = global.SubscribeTopic(global.TOPIC_RSP_BANK); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- if err = global.SubscribeTopic(global.TOPIC_PERFORMANCE_RSP); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- if err = global.SubscribeTopic(global.TOPIC_WAREHOUSE_RECIEPT_RSP); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_TRADE); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_TRADE_GZ); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- // 铁合金
- if err = global.SubscribeTopic(global.TOPIC_RSP_WAREHOUSE_PRESALE_THJ); err != nil {
- global.M2A_LOG.Error("rabbitmq subscribe topic failed, err:", zap.Error(err))
- return
- }
- global.M2A_LOG.Info("rabbitmq subscribe topic successed.")
- return
- }
- // StartRabbitMQReceive 开始接收总线消息
- func StartRabbitMQReceive() {
- t := &MQProc{}
- go func() {
- for _, subinfo := range global.SubInfos {
- global.Receive(subinfo.Topic, subinfo.QueueName, t)
- }
- }()
- }
- // InitFuncodeTopic 初始化功能码主题MAP
- func InitFuncodeTopic() {
- if global.M2A_FuncodeTopic == nil {
- global.M2A_FuncodeTopic = make(map[string][]int)
- }
- global.M2A_FuncodeTopic[global.TOPIC_REQ_QKERNEL] = []int{
- global.ModifyPwdReq,
- }
- global.M2A_FuncodeTopic[global.TOPIC_MANAGE_REQ] = []int{
- global.UserReceiveInfoReq,
- global.DelUserReceiveInfoReq,
- global.UserReceiveIsDefaultReq,
- global.UserReceiptInfoReq,
- global.DelUserReceiptInfoReq,
- global.WarehouseApplyReq,
- }
- global.M2A_FuncodeTopic[global.TOPIC_REQ_BANK] = []int{
- global.T2bBankSignReq,
- global.T2bBankCancelSignReq,
- global.T2bBankWithdrawReq,
- global.T2bBankDepositReq,
- }
- global.M2A_FuncodeTopic[global.TOPIC_PERFORMANCE_REQ] = []int{
- global.PerformanceContractedApplyReq,
- global.PerformanceDelayApplyReq,
- global.PerformanceManualConfirmReq,
- global.PerformanceModifyContactReq,
- }
- global.M2A_FuncodeTopic[global.TOPIC_WAREHOUSE_RECIEPT] = []int{
- global.ReceiptZSOutApplyReq,
- }
- global.M2A_FuncodeTopic[global.TOPIC_REQ_WAREHOUSE_TRADE] = []int{
- global.WRListingCancelOrderReq,
- }
- global.M2A_FuncodeTopic[global.TOPIC_REQ_WAREHOUSE_TRADE_GZ] = []int{
- global.GoodsFavoriteOperateReq,
- global.ZSBuyOrderListingReq,
- global.ZSSellOrderListingReq,
- global.ZSBuyOrderDestingReq,
- global.ZSSellOrderDestingApplyReq,
- global.ZSSellOrderDestingApplyOperateReq,
- global.ZSBuyOrderDestingNegPriceReq,
- global.ZSBuyOrderDestingNegPriceOperateReq,
- }
- // 铁合金
- global.M2A_FuncodeTopic[global.TOPIC_REQ_WAREHOUSE_PRESALE_THJ] = []int{
- global.SpotPresaleListingOrderReq,
- }
- }
|