futures.ts 8.6 KB

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