index.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import APP from '@/services';
  2. import { funCode } from '@/services/funcode/index';
  3. import { buildSubscribePeq, parseSubscribeRsp } from '@/services/socket/quota/adapter/index';
  4. import { Callback } from '@/utils/websocket/index';
  5. import { SubscribeInfoType } from './interface';
  6. import { isLogin } from '@/services/bus/login';
  7. const subscribeGoodsList = new Map<string, SubscribeInfoType[]>(); // 订阅商品数据中心池
  8. /**
  9. * 重组订阅商品(包括去重,组合)
  10. */
  11. function recombinationSubscribeGoods(): SubscribeInfoType[] {
  12. const result: SubscribeInfoType[] = [];
  13. const goodsCodeSet = new Set<string>();
  14. for (const item of subscribeGoodsList.values()) {
  15. item.forEach((e) => {
  16. if (!goodsCodeSet.has(e.goodsCode)) {
  17. result.push(e);
  18. }
  19. });
  20. }
  21. return result;
  22. }
  23. /**
  24. * 开始商品订阅
  25. * @param subscribeInfos
  26. */
  27. function actionSubcribe() {
  28. const arr = recombinationSubscribeGoods();
  29. if (arr.length) {
  30. const req = buildSubscribePeq(arr);
  31. APP.sendQuoteServer(req, funCode.MainClassNumber_Quota_SubscriptRsp, {
  32. onSuccess: (res: any) => {
  33. parseSubscribeRsp(res)
  34. .then((value) => {
  35. if (value) {
  36. console.log('订阅成功!', value);
  37. }
  38. })
  39. .catch((err) => {
  40. console.log('解析失败:', err);
  41. });
  42. },
  43. onFail: (err) => {
  44. if (isLogin()) {
  45. console.log('订阅失败:', err.message)
  46. }
  47. },
  48. } as Callback);
  49. }
  50. }
  51. /**
  52. * 添加行情订阅
  53. * @param subscribeInfos
  54. */
  55. export function addSubscribeQuotation(uuid: string, subscribeInfos: SubscribeInfoType[]) {
  56. APP.connectQuote().then(() => {
  57. subscribeGoodsList.set(uuid, subscribeInfos);
  58. actionSubcribe();
  59. });
  60. }
  61. /**
  62. * 删除订阅
  63. */
  64. export function removeSubscribeQuotation(uuid: string) {
  65. if (subscribeGoodsList.has(uuid)) {
  66. subscribeGoodsList.delete(uuid);
  67. console.log('删除订阅', uuid)
  68. if (subscribeGoodsList.size) {
  69. actionSubcribe();
  70. } else {
  71. // 没有订阅商品时候,主动断开行情链路
  72. // APP.closeQuote();
  73. }
  74. }
  75. }