futures.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { reactive, computed, toRefs } from 'vue'
  2. import { timerInterceptor } from '@/utils/timer'
  3. import { queryGoodsList } from '@/services/api/goods'
  4. import { defineStore } from '../store'
  5. import eventBus from '@/services/bus'
  6. import moment from 'moment'
  7. /**
  8. * 期货存储对象
  9. */
  10. export const useFuturesStore = defineStore(() => {
  11. const state = reactive({
  12. loading: false,
  13. quotes: <Proto.Quote[]>[], // 行情数据
  14. goodsList: <Ermcp.GoodsRsp[]>[], // 商品列表
  15. quoteDayList: <Ermcp.QuoteDayRsp[]>[], // 盘面列表
  16. })
  17. // 获取商品列表
  18. const getGoodsList = async () => {
  19. try {
  20. state.loading = true
  21. const res = await queryGoodsList()
  22. state.goodsList = res.data
  23. } finally {
  24. state.loading = false
  25. }
  26. }
  27. // 通过 goodscode 获取盘面实时行情
  28. const getQuoteDayInfoByCode = (goodscode: string) => {
  29. return computed(() => state.quoteDayList.find((e) => e.goodscode.toUpperCase() === goodscode.toUpperCase()))
  30. }
  31. // 通过 goodscode 获取商品实时报价
  32. const getGoodsPriceByCode = (goodscode: string) => {
  33. return computed(() => {
  34. const quote = getQuoteDayInfoByCode(goodscode)
  35. return quote.value?.last ?? 0
  36. })
  37. }
  38. // 处理行情数据
  39. const handleQuote = timerInterceptor.setThrottle(() => {
  40. state.quotes.forEach((item) => {
  41. const quote = state.quoteDayList.find((e) => e.goodscode.toUpperCase() === item.goodscode?.toUpperCase())
  42. const last = item.last ?? 0
  43. const lasttime = (item.date && item.time) ? moment(item.date + item.time, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss') : ''
  44. if (quote) {
  45. Object.entries(item).forEach(([key, value]) => {
  46. // 只更新存在的属性
  47. if (Reflect.has(quote, key)) {
  48. Object.defineProperty(quote, key, { value })
  49. }
  50. })
  51. // 处理最高最低价
  52. if (last) {
  53. if (last > quote.highest) {
  54. quote.highest = last
  55. }
  56. if (last < quote.lowest) {
  57. quote.lowest = last
  58. }
  59. }
  60. // 处理最新时间
  61. if (lasttime) {
  62. quote.lasttime = lasttime
  63. }
  64. } else {
  65. console.warn('行情推送的商品 ' + item.goodscode + ' 缺少盘面信息')
  66. state.quoteDayList.push({
  67. Lastturnover: 0,
  68. ask: item.ask ?? 0,
  69. ask2: item.ask2 ?? 0,
  70. ask3: item.ask3 ?? 0,
  71. ask4: item.ask4 ?? 0,
  72. ask5: item.ask5 ?? 0,
  73. ask6: 0,
  74. ask7: 0,
  75. ask8: 0,
  76. ask9: 0,
  77. ask10: 0,
  78. askorderid: 0,
  79. askorderid2: 0,
  80. askorderid3: 0,
  81. askorderid4: 0,
  82. askorderid5: 0,
  83. askordervolume: 0,
  84. askordervolume2: 0,
  85. askordervolume3: 0,
  86. askordervolume4: 0,
  87. askordervolume5: 0,
  88. askordervolume6: 0,
  89. askordervolume7: 0,
  90. askordervolume8: 0,
  91. askordervolume9: 0,
  92. askordervolume10: 0,
  93. askqueueinfo: "",
  94. askvolume: item.askvolume ?? 0,
  95. askvolume2: item.askvolume2 ?? 0,
  96. askvolume3: item.askvolume3 ?? 0,
  97. askvolume4: item.askvolume4 ?? 0,
  98. askvolume5: item.askvolume5 ?? 0,
  99. askvolume6: 0,
  100. askvolume7: 0,
  101. askvolume8: 0,
  102. askvolume9: 0,
  103. askvolume10: 0,
  104. averageprice: 0,
  105. bid: item.bid ?? 0,
  106. bid2: item.bid2 ?? 0,
  107. bid3: item.bid3 ?? 0,
  108. bid4: item.bid4 ?? 0,
  109. bid5: item.bid5 ?? 0,
  110. bid6: 0,
  111. bid7: 0,
  112. bid8: 0,
  113. bid9: 0,
  114. bid10: 0,
  115. bidorderid: 0,
  116. bidorderid2: 0,
  117. bidorderid3: 0,
  118. bidorderid4: 0,
  119. bidorderid5: 0,
  120. bidordervolume: 0,
  121. bidordervolume2: 0,
  122. bidordervolume3: 0,
  123. bidordervolume4: 0,
  124. bidordervolume5: 0,
  125. bidordervolume6: 0,
  126. bidordervolume7: 0,
  127. bidordervolume8: 0,
  128. bidordervolume9: 0,
  129. bidordervolume10: 0,
  130. bidqueueinfo: "",
  131. bidvolume: item.bidvolume ?? 0,
  132. bidvolume2: item.bidvolume2 ?? 0,
  133. bidvolume3: item.bidvolume3 ?? 0,
  134. bidvolume4: item.bidvolume4 ?? 0,
  135. bidvolume5: item.bidvolume5 ?? 0,
  136. bidvolume6: 0,
  137. bidvolume7: 0,
  138. bidvolume8: 0,
  139. bidvolume9: 0,
  140. bidvolume10: 0,
  141. calloptionpremiums: item.calloptionpremiums ?? 0,
  142. calloptionpremiums2: item.calloptionpremiums2 ?? 0,
  143. calloptionpremiums3: item.calloptionpremiums3 ?? 0,
  144. calloptionpremiums4: item.calloptionpremiums4 ?? 0,
  145. calloptionpremiums5: item.calloptionpremiums5 ?? 0,
  146. cleartime: 0,
  147. exchangecode: item.exchangecode ?? 0,
  148. exchangedate: item.exchangedate ?? 0,
  149. goodscode: item.goodscode ?? '',
  150. grepmarketprice: 0,
  151. highest: item.highest ?? 0,
  152. holdincrement: 0,
  153. holdvolume: item.holdvolume ?? 0,
  154. iep: 0,
  155. iev: 0,
  156. inventory: item.inventory ?? 0,
  157. iscleared: 0,
  158. issettled: 0,
  159. last,
  160. lastlot: 0,
  161. lasttime,
  162. lastvolume: item.lastvolume ?? 0,
  163. limitdown: item.limitlow ?? 0,
  164. limitup: item.limithigh ?? 0,
  165. lowest: item.lowest ?? 0,
  166. nontotalholdervolume: 0,
  167. nontotallot: 0,
  168. nontotalturnover: 0,
  169. nontotalvolume: 0,
  170. opened: item.opened ?? 0,
  171. opentime: '',
  172. orderid: 0,
  173. preclose: item.preclose ?? 0,
  174. preholdvolume: item.preholdvolume ?? 0,
  175. presettle: item.presettle ?? 0,
  176. publictradetype: '',
  177. putoptionpremiums: item.putoptionpremiums ?? 0,
  178. putoptionpremiums2: item.putoptionpremiums2 ?? 0,
  179. putoptionpremiums3: item.putoptionpremiums3 ?? 0,
  180. putoptionpremiums4: item.putoptionpremiums4 ?? 0,
  181. putoptionpremiums5: item.putoptionpremiums5 ?? 0,
  182. settle: item.settle ?? 0,
  183. strikeprice: 0,
  184. totalaskvolume: 0,
  185. totalbidvolume: 0,
  186. totallot: 0,
  187. totalturnover: item.totalturnover ?? 0,
  188. totalvolume: item.totalvolume ?? 0,
  189. utclasttime: ''
  190. })
  191. }
  192. })
  193. }, 100)
  194. // 接收行情推送通知
  195. const quotePushNotify = eventBus.$on('QuotePushNotify', (res) => {
  196. const data = res as Proto.Quote[]
  197. data.forEach((item) => {
  198. const index = state.quotes.findIndex((e) => e.goodscode === item.goodscode)
  199. if (index > -1) {
  200. state.quotes[index] = item
  201. } else {
  202. state.quotes.push(item)
  203. }
  204. })
  205. handleQuote()
  206. })
  207. return {
  208. ...toRefs(state),
  209. getGoodsList,
  210. getQuoteDayInfoByCode,
  211. getGoodsPriceByCode,
  212. quotePushNotify,
  213. }
  214. })