websocket.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import socket from "@/utils/websocket.js"
  2. let socket_api = 'https://doc.okenx.com:2000'
  3. const state = {
  4. bids: [],
  5. asks: [],
  6. daymarket:{}
  7. }
  8. const mutations = {
  9. // SET_CONTRACT: (state, contract) => {
  10. // // 合约账户
  11. // state.contractAccount = contract;
  12. // },
  13. // SET_BIBI: (state, bibi) => {
  14. // // 币币账户
  15. // state.bibiAccount = bibi;
  16. // },
  17. // SET_LEGAL: (state, legal) => {
  18. // // 法币账户
  19. // state.legalAccount = legal;
  20. // },
  21. // SET_GIFTMONEY: (state, GiftMoney) => {
  22. // // 赠金账户
  23. // state.GiftMoneyAccount = GiftMoney;
  24. // },
  25. // SET_STOCKS_COLOR: (state, code) => {
  26. // state.stocksColor = code
  27. // },
  28. // SET_TOKEN: (state, token) => {
  29. // setToken(token)
  30. // state.token = token
  31. // }
  32. }
  33. const nums = (arr) => {
  34. const att = arr.map(el => {
  35. return el[0]
  36. })
  37. const num = Math.max.apply(null, att)
  38. arr.forEach(el => {
  39. let n = Math.round(el[0] / num * 10000) / 100
  40. el[2] = n > 100 ? 100 : n;
  41. })
  42. return arr
  43. }
  44. const actions = {
  45. openSocket({
  46. commit,
  47. state
  48. }, data) {
  49. var sk = socket(socket_api);
  50. sk.on(data.type, function(msg) {
  51. if (data.legal_id == msg.legal_id && data.currency_id == msg.currency_id) {
  52. //btc 匹配
  53. // var buyIn = JSON.parse(msg.bids);
  54. // var out = JSON.parse(msg.asks).reverse();
  55. if (msg.type == 'market_depth') {
  56. var buyIn = msg.bids.slice(0, 7);
  57. state.bids = nums(buyIn);
  58. var out = msg.asks.slice(0, 7);
  59. // state.bids = msg.bids
  60. state.asks = nums(out);
  61. }
  62. if (msg.type == 'daymarket') {
  63. state.daymarket = msg
  64. console.log('daymarket = ', msg)
  65. // var buyIn = msg.bids.slice(0, 7);
  66. // state.bids = nums(buyIn);
  67. // var out = msg.asks.slice(0, 7);
  68. // // state.bids = msg.bids
  69. // state.asks = nums(out);
  70. }
  71. }
  72. })
  73. },
  74. }
  75. export default {
  76. namespaced: true,
  77. state,
  78. mutations,
  79. actions
  80. }