mq.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. package request
  2. import (
  3. "encoding/base64"
  4. "errors"
  5. "mtp20access/global"
  6. "mtp20access/packet"
  7. "mtp20access/res/pb"
  8. "regexp"
  9. "strings"
  10. "github.com/golang/protobuf/jsonpb"
  11. "github.com/golang/protobuf/proto"
  12. "go.uber.org/zap"
  13. )
  14. type MQBodyReq struct {
  15. FunCodeReq uint32 `json:"funCodeReq" binding:"required"` // 请求功能码
  16. FunCodeRsp uint32 `json:"funCodeRsp"` // 回复功能码
  17. IsEncrypted *bool `json:"isEncrypted" binding:"required"` // 是否加密
  18. Data string `json:"data" binding:"required"` // 请求数据
  19. }
  20. // GetProtoBytes 将客户端请求的数据转换成总线使用的Protobuf
  21. //
  22. // serialNumber 流水号
  23. func (r *MQBodyReq) GetProtoBytes(serialNumber *uint32) (bytes *[]byte, err error) {
  24. // 判断是否需要解密数据内容
  25. data := r.Data
  26. if *r.IsEncrypted {
  27. d, e := base64.StdEncoding.DecodeString(r.Data)
  28. if e != nil {
  29. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  30. err = errors.New("解密失败")
  31. return
  32. }
  33. d1 := d[4 : len(d)-8] // 解密时要去头尾
  34. p, e := packet.Decrypt(d1, packet.AESKey, true)
  35. if e != nil {
  36. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  37. err = errors.New("解密失败")
  38. return
  39. }
  40. data = string(p)
  41. }
  42. // JSON -> Protobuf
  43. switch int(r.FunCodeReq) {
  44. case global.ModifyPwdReq:
  45. m := pb.ModifyPwdReq{}
  46. if err = r.reflect(data, &m); err != nil {
  47. return
  48. }
  49. if m.Header != nil {
  50. m.Header.RequestID = serialNumber
  51. } else {
  52. err = errors.New("请求信息序列化失败")
  53. return
  54. }
  55. if b, e := proto.Marshal(&m); e != nil {
  56. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  57. err = errors.New("请求信息序列化失败")
  58. return
  59. } else {
  60. bytes = &b
  61. }
  62. case global.UserReceiveInfoReq:
  63. m := pb.UserReceiveInfoReq{}
  64. if err = r.reflect(data, &m); err != nil {
  65. return
  66. }
  67. if m.Header != nil {
  68. m.Header.RequestID = serialNumber
  69. } else {
  70. err = errors.New("请求信息序列化失败")
  71. return
  72. }
  73. if b, e := proto.Marshal(&m); e != nil {
  74. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  75. err = errors.New("请求信息序列化失败")
  76. return
  77. } else {
  78. bytes = &b
  79. }
  80. case global.DelUserReceiveInfoReq:
  81. m := pb.DelUserReceiveInfoReq{}
  82. if err = r.reflect(data, &m); err != nil {
  83. return
  84. }
  85. if m.Header != nil {
  86. m.Header.RequestID = serialNumber
  87. } else {
  88. err = errors.New("请求信息序列化失败")
  89. return
  90. }
  91. if b, e := proto.Marshal(&m); e != nil {
  92. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  93. err = errors.New("请求信息序列化失败")
  94. return
  95. } else {
  96. bytes = &b
  97. }
  98. case global.UserReceiveIsDefaultReq:
  99. m := pb.UserReceiveIsDefaultReq{}
  100. if err = r.reflect(data, &m); err != nil {
  101. return
  102. }
  103. if m.Header != nil {
  104. m.Header.RequestID = serialNumber
  105. } else {
  106. err = errors.New("请求信息序列化失败")
  107. return
  108. }
  109. if b, e := proto.Marshal(&m); e != nil {
  110. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  111. err = errors.New("请求信息序列化失败")
  112. return
  113. } else {
  114. bytes = &b
  115. }
  116. case global.UserReceiptInfoReq:
  117. m := pb.UserReceiptInfoReq{}
  118. if err = r.reflect(data, &m); err != nil {
  119. return
  120. }
  121. if m.Header != nil {
  122. m.Header.RequestID = serialNumber
  123. } else {
  124. err = errors.New("请求信息序列化失败")
  125. return
  126. }
  127. if b, e := proto.Marshal(&m); e != nil {
  128. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  129. err = errors.New("请求信息序列化失败")
  130. return
  131. } else {
  132. bytes = &b
  133. }
  134. case global.DelUserReceiptInfoReq:
  135. m := pb.DelUserReceiptInfoReq{}
  136. if err = r.reflect(data, &m); err != nil {
  137. return
  138. }
  139. if m.Header != nil {
  140. m.Header.RequestID = serialNumber
  141. } else {
  142. err = errors.New("请求信息序列化失败")
  143. return
  144. }
  145. if b, e := proto.Marshal(&m); e != nil {
  146. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  147. err = errors.New("请求信息序列化失败")
  148. return
  149. } else {
  150. bytes = &b
  151. }
  152. case global.T2bBankSignReq:
  153. m := pb.T2BBankSignReq{}
  154. if err = r.reflect(data, &m); err != nil {
  155. return
  156. }
  157. if m.Header != nil {
  158. m.Header.RequestID = serialNumber
  159. } else {
  160. err = errors.New("请求信息序列化失败")
  161. return
  162. }
  163. if b, e := proto.Marshal(&m); e != nil {
  164. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  165. err = errors.New("请求信息序列化失败")
  166. return
  167. } else {
  168. bytes = &b
  169. }
  170. case global.T2bBankCancelSignReq:
  171. m := pb.T2BBankCancelSignReq{}
  172. if err = r.reflect(data, &m); err != nil {
  173. return
  174. }
  175. if m.Header != nil {
  176. m.Header.RequestID = serialNumber
  177. } else {
  178. err = errors.New("请求信息序列化失败")
  179. return
  180. }
  181. if b, e := proto.Marshal(&m); e != nil {
  182. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  183. err = errors.New("请求信息序列化失败")
  184. return
  185. } else {
  186. bytes = &b
  187. }
  188. case global.T2bBankWithdrawReq:
  189. m := pb.T2BBankWithdrawReq{}
  190. if err = r.reflect(data, &m); err != nil {
  191. return
  192. }
  193. if m.Header != nil {
  194. m.Header.RequestID = serialNumber
  195. } else {
  196. err = errors.New("请求信息序列化失败")
  197. return
  198. }
  199. if b, e := proto.Marshal(&m); e != nil {
  200. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  201. err = errors.New("请求信息序列化失败")
  202. return
  203. } else {
  204. bytes = &b
  205. }
  206. case global.T2bBankDepositReq:
  207. m := pb.T2BBankDepositReq{}
  208. if err = r.reflect(data, &m); err != nil {
  209. return
  210. }
  211. if m.Header != nil {
  212. m.Header.RequestID = serialNumber
  213. } else {
  214. err = errors.New("请求信息序列化失败")
  215. return
  216. }
  217. if b, e := proto.Marshal(&m); e != nil {
  218. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  219. err = errors.New("请求信息序列化失败")
  220. return
  221. } else {
  222. bytes = &b
  223. }
  224. case global.WarehouseApplyReq:
  225. m := pb.WarehouseApplyReq{}
  226. if err = r.reflect(data, &m); err != nil {
  227. return
  228. }
  229. if m.Header != nil {
  230. m.Header.RequestID = serialNumber
  231. } else {
  232. err = errors.New("请求信息序列化失败")
  233. return
  234. }
  235. if b, e := proto.Marshal(&m); e != nil {
  236. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  237. err = errors.New("请求信息序列化失败")
  238. return
  239. } else {
  240. bytes = &b
  241. }
  242. case global.PerformanceContractedApplyReq:
  243. m := pb.PerformanceContractedApplyReq{}
  244. if err = r.reflect(data, &m); err != nil {
  245. return
  246. }
  247. if m.Header != nil {
  248. m.Header.RequestID = serialNumber
  249. } else {
  250. err = errors.New("请求信息序列化失败")
  251. return
  252. }
  253. if b, e := proto.Marshal(&m); e != nil {
  254. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  255. err = errors.New("请求信息序列化失败")
  256. return
  257. } else {
  258. bytes = &b
  259. }
  260. case global.PerformanceDelayApplyReq:
  261. m := pb.PerformanceDelayApplyReq{}
  262. if err = r.reflect(data, &m); err != nil {
  263. return
  264. }
  265. if m.Header != nil {
  266. m.Header.RequestID = serialNumber
  267. } else {
  268. err = errors.New("请求信息序列化失败")
  269. return
  270. }
  271. if b, e := proto.Marshal(&m); e != nil {
  272. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  273. err = errors.New("请求信息序列化失败")
  274. return
  275. } else {
  276. bytes = &b
  277. }
  278. case global.PerformanceManualConfirmReq:
  279. m := pb.PerformanceManualConfirmReq{}
  280. if err = r.reflect(data, &m); err != nil {
  281. return
  282. }
  283. if m.Header != nil {
  284. m.Header.RequestID = serialNumber
  285. } else {
  286. err = errors.New("请求信息序列化失败")
  287. return
  288. }
  289. if b, e := proto.Marshal(&m); e != nil {
  290. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  291. err = errors.New("请求信息序列化失败")
  292. return
  293. } else {
  294. bytes = &b
  295. }
  296. case global.PerformanceModifyContactReq:
  297. m := pb.PerformanceModifyContactReq{}
  298. if err = r.reflect(data, &m); err != nil {
  299. return
  300. }
  301. if m.Header != nil {
  302. m.Header.RequestID = serialNumber
  303. } else {
  304. err = errors.New("请求信息序列化失败")
  305. return
  306. }
  307. if b, e := proto.Marshal(&m); e != nil {
  308. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  309. err = errors.New("请求信息序列化失败")
  310. return
  311. } else {
  312. bytes = &b
  313. }
  314. case global.GoodsFavoriteOperateReq:
  315. reg, _ := regexp.Compile(`"[0-9]{19}"`)
  316. all := reg.FindAll([]byte(data), -1)
  317. for _, i := range all {
  318. r := strings.Replace(string(i), "\"", "", -1)
  319. data = strings.Replace(data, string(i), r, -1)
  320. }
  321. m := pb.GoodsFavoriteOperateReq{}
  322. if err = r.reflect(data, &m); err != nil {
  323. return
  324. }
  325. if m.Header != nil {
  326. m.Header.RequestID = serialNumber
  327. } else {
  328. err = errors.New("请求信息序列化失败")
  329. return
  330. }
  331. if b, e := proto.Marshal(&m); e != nil {
  332. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  333. err = errors.New("请求信息序列化失败")
  334. return
  335. } else {
  336. bytes = &b
  337. }
  338. case global.ZSBuyOrderListingReq:
  339. m := pb.ZSBuyOrderListingReq{}
  340. if err = r.reflect(data, &m); err != nil {
  341. return
  342. }
  343. if m.Header != nil {
  344. m.Header.RequestID = serialNumber
  345. } else {
  346. err = errors.New("请求信息序列化失败")
  347. return
  348. }
  349. if b, e := proto.Marshal(&m); e != nil {
  350. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  351. err = errors.New("请求信息序列化失败")
  352. return
  353. } else {
  354. bytes = &b
  355. }
  356. case global.ZSSellOrderListingReq:
  357. m := pb.ZSSellOrderListingReq{}
  358. if err = r.reflect(data, &m); err != nil {
  359. return
  360. }
  361. if m.Header != nil {
  362. m.Header.RequestID = serialNumber
  363. } else {
  364. err = errors.New("请求信息序列化失败")
  365. return
  366. }
  367. if b, e := proto.Marshal(&m); e != nil {
  368. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  369. err = errors.New("请求信息序列化失败")
  370. return
  371. } else {
  372. bytes = &b
  373. }
  374. case global.ZSBuyOrderDestingReq:
  375. m := pb.ZSBuyOrderDestingReq{}
  376. if err = r.reflect(data, &m); err != nil {
  377. return
  378. }
  379. if m.Header != nil {
  380. m.Header.RequestID = serialNumber
  381. } else {
  382. err = errors.New("请求信息序列化失败")
  383. return
  384. }
  385. if b, e := proto.Marshal(&m); e != nil {
  386. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  387. err = errors.New("请求信息序列化失败")
  388. return
  389. } else {
  390. bytes = &b
  391. }
  392. case global.ZSSellOrderDestingApplyReq:
  393. m := pb.ZSSellOrderDestingApplyReq{}
  394. if err = r.reflect(data, &m); err != nil {
  395. return
  396. }
  397. if m.Header != nil {
  398. m.Header.RequestID = serialNumber
  399. } else {
  400. err = errors.New("请求信息序列化失败")
  401. return
  402. }
  403. if b, e := proto.Marshal(&m); e != nil {
  404. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  405. err = errors.New("请求信息序列化失败")
  406. return
  407. } else {
  408. bytes = &b
  409. }
  410. case global.ZSSellOrderDestingApplyOperateReq:
  411. m := pb.ZSSellOrderDestingApplyOperateReq{}
  412. if err = r.reflect(data, &m); err != nil {
  413. return
  414. }
  415. if m.Header != nil {
  416. m.Header.RequestID = serialNumber
  417. } else {
  418. err = errors.New("请求信息序列化失败")
  419. return
  420. }
  421. if b, e := proto.Marshal(&m); e != nil {
  422. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  423. err = errors.New("请求信息序列化失败")
  424. return
  425. } else {
  426. bytes = &b
  427. }
  428. case global.ZSBuyOrderDestingNegPriceReq:
  429. m := pb.ZSBuyOrderDestingNegPriceReq{}
  430. if err = r.reflect(data, &m); err != nil {
  431. return
  432. }
  433. if m.Header != nil {
  434. m.Header.RequestID = serialNumber
  435. } else {
  436. err = errors.New("请求信息序列化失败")
  437. return
  438. }
  439. if b, e := proto.Marshal(&m); e != nil {
  440. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  441. err = errors.New("请求信息序列化失败")
  442. return
  443. } else {
  444. bytes = &b
  445. }
  446. case global.ZSBuyOrderDestingNegPriceOperateReq:
  447. m := pb.ZSBuyOrderDestingNegPriceOperateReq{}
  448. if err = r.reflect(data, &m); err != nil {
  449. return
  450. }
  451. if m.Header != nil {
  452. m.Header.RequestID = serialNumber
  453. } else {
  454. err = errors.New("请求信息序列化失败")
  455. return
  456. }
  457. if b, e := proto.Marshal(&m); e != nil {
  458. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  459. err = errors.New("请求信息序列化失败")
  460. return
  461. } else {
  462. bytes = &b
  463. }
  464. case global.WRListingCancelOrderReq:
  465. m := pb.WRListingCancelOrderReq{}
  466. if err = r.reflect(data, &m); err != nil {
  467. return
  468. }
  469. if m.Header != nil {
  470. m.Header.RequestID = serialNumber
  471. } else {
  472. err = errors.New("请求信息序列化失败")
  473. return
  474. }
  475. if b, e := proto.Marshal(&m); e != nil {
  476. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  477. err = errors.New("请求信息序列化失败")
  478. return
  479. } else {
  480. bytes = &b
  481. }
  482. case global.ReceiptZSOutApplyReq:
  483. m := pb.ReceiptZSOutApplyReq{}
  484. if err = r.reflect(data, &m); err != nil {
  485. return
  486. }
  487. if m.Header != nil {
  488. m.Header.RequestID = serialNumber
  489. } else {
  490. err = errors.New("请求信息序列化失败")
  491. return
  492. }
  493. if b, e := proto.Marshal(&m); e != nil {
  494. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  495. err = errors.New("请求信息序列化失败")
  496. return
  497. } else {
  498. bytes = &b
  499. }
  500. }
  501. return
  502. }
  503. func (r *MQBodyReq) reflect(data string, m proto.Message) (err error) {
  504. if e := jsonpb.UnmarshalString(data, m); e != nil {
  505. global.M2A_LOG.Error(e.Error(), zap.Error(e))
  506. err = errors.New("业务数据装箱失败")
  507. return
  508. }
  509. return
  510. }