|
|
@@ -1,315 +0,0 @@
|
|
|
-import { reactive, computed, toRefs } from 'vue'
|
|
|
-import { handlePriceColor } from '@/filters'
|
|
|
-import { queryErmcpGoods, queryQuoteDay } from '@/services/api/goods'
|
|
|
-import { defineStore } from '../store'
|
|
|
-import { timerTask } from '@/utils/timer'
|
|
|
-import eventBus from '@/services/bus'
|
|
|
-import moment from 'moment'
|
|
|
-
|
|
|
-/**
|
|
|
- * 期货存储对象
|
|
|
- * @returns
|
|
|
- */
|
|
|
-export const useFuturesStore = defineStore(() => {
|
|
|
- const state = reactive({
|
|
|
- loading: false,
|
|
|
- goodsList: <Model.GoodsRsp[]>[], // 商品列表
|
|
|
- quotes: <Partial<Model.QuoteDayRsp>[]>[], // 行情数据
|
|
|
- goodsQuoteList: <Model.GoodsQuote[]>[], // 商品行情列表
|
|
|
- })
|
|
|
-
|
|
|
- // 获取商品列表
|
|
|
- const getGoodsList = () => {
|
|
|
- timerTask.clearTimeout('quoteDay')
|
|
|
- queryErmcpGoods().then((res) => {
|
|
|
- state.goodsList = res.data
|
|
|
- const codes = res.data.map((e) => e.goodscode)
|
|
|
- if (codes.length) {
|
|
|
- getQuoteDay(codes)
|
|
|
- }
|
|
|
- })
|
|
|
- // 获取商品盘面信息
|
|
|
- const getQuoteDay = (codes: string[]) => {
|
|
|
- state.loading = true
|
|
|
- queryQuoteDay({
|
|
|
- data: {
|
|
|
- goodsCodes: codes.join(',')
|
|
|
- }
|
|
|
- }).then((res) => {
|
|
|
- res.data.forEach((item) => {
|
|
|
- updateGoodsQuote(item)
|
|
|
- })
|
|
|
- }).finally(() => {
|
|
|
- state.loading = false
|
|
|
- // 每5分钟获取一次盘面
|
|
|
- timerTask.setTimeout(() => getQuoteDay(codes), 5 * 60 * 1000, 'quoteDay')
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 通过 goodscode 获取实时盘面
|
|
|
- const getGoodsQuote = (code: string | number) => {
|
|
|
- return computed(() => state.goodsQuoteList.find((e) => e.goodscode === code || e.goodsid === code))
|
|
|
- }
|
|
|
-
|
|
|
- // 通过 goodscode 获取实时行情报价
|
|
|
- const getQuotePrice = (goodsCode: string) => {
|
|
|
- return computed(() => {
|
|
|
- const quote = getGoodsQuote(goodsCode)
|
|
|
- return quote.value?.last ?? 0
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- // 通过 goodscode 获取实时行情
|
|
|
- const getQuote = (goodsCode: string) => {
|
|
|
- return computed(() => {
|
|
|
- const quote = state.quotes.find((e) => e.goodscode === goodsCode)
|
|
|
- return {
|
|
|
- ...quote
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- // 获取商品名称
|
|
|
- const getGoodsName = (code: string | number) => {
|
|
|
- const quote = state.goodsList.find((e) => e.goodscode === code || e.goodsid === code)
|
|
|
- return quote?.goodsname ?? ''
|
|
|
- }
|
|
|
-
|
|
|
- // 获取商品市场ID
|
|
|
- const getGoodsMarket = (code: string | number) => {
|
|
|
- const quote = state.goodsList.find((e) => e.goodscode === code || e.goodsid === code)
|
|
|
- return quote?.marketid ?? 0
|
|
|
- }
|
|
|
-
|
|
|
- // 接收行情推送通知
|
|
|
- const quotePushNotify = eventBus.$on('QuotePushNotify', (res) => {
|
|
|
- const data = res as Proto.Quote[]
|
|
|
- data.forEach((item) => {
|
|
|
- const quote = handleQuote(item)
|
|
|
- const index = state.quotes.findIndex((e) => e.goodscode === item.goodscode)
|
|
|
- if (index > -1) {
|
|
|
- state.quotes[index] = quote
|
|
|
- } else {
|
|
|
- state.quotes.push(quote)
|
|
|
- }
|
|
|
- if (!state.loading) {
|
|
|
- updateGoodsQuote(quote)
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
- // 更新商品行情数据
|
|
|
- const updateGoodsQuote = (quote: Partial<Model.QuoteDayRsp>) => {
|
|
|
- // 查找对应的商品行情
|
|
|
- const item: Model.GoodsQuote = state.goodsQuoteList.find((e) => e.goodscode === quote.goodscode) ?? {
|
|
|
- goodsid: 0,
|
|
|
- goodscode: quote.goodscode ?? '',
|
|
|
- goodsname: '',
|
|
|
- goodsgroupid: 0,
|
|
|
- goodunitid: 0,
|
|
|
- marketid: 0,
|
|
|
- trademode: 0,
|
|
|
- agreeunit: 0,
|
|
|
- decimalplace: 0,
|
|
|
- quoteminunit: 0,
|
|
|
- quotegear: 0,
|
|
|
- last: quote.last ?? 0,
|
|
|
- lasttime: quote.lasttime ?? '',
|
|
|
- bid: quote.bid ?? 0,
|
|
|
- bid2: quote.bid2 ?? 0,
|
|
|
- bid3: quote.bid3 ?? 0,
|
|
|
- bid4: quote.bid4 ?? 0,
|
|
|
- bid5: quote.bid5 ?? 0,
|
|
|
- bidvolume: quote.bidvolume ?? 0,
|
|
|
- bidvolume2: quote.bidvolume2 ?? 0,
|
|
|
- bidvolume3: quote.bidvolume3 ?? 0,
|
|
|
- bidvolume4: quote.bidvolume4 ?? 0,
|
|
|
- bidvolume5: quote.bidvolume5 ?? 0,
|
|
|
- ask: quote.ask ?? 0,
|
|
|
- ask2: quote.ask2 ?? 0,
|
|
|
- ask3: quote.ask3 ?? 0,
|
|
|
- ask4: quote.ask4 ?? 0,
|
|
|
- ask5: quote.ask5 ?? 0,
|
|
|
- askvolume: quote.askvolume ?? 0,
|
|
|
- askvolume2: quote.askvolume2 ?? 0,
|
|
|
- askvolume3: quote.askvolume3 ?? 0,
|
|
|
- askvolume4: quote.askvolume4 ?? 0,
|
|
|
- askvolume5: quote.askvolume5 ?? 0,
|
|
|
- lastvolume: quote.lastvolume ?? 0,
|
|
|
- presettle: quote.presettle ?? 0,
|
|
|
- opened: quote.opened ?? 0,
|
|
|
- highest: quote.highest ?? 0,
|
|
|
- lowest: quote.lowest ?? 0,
|
|
|
- limitup: quote.limitup ?? 0,
|
|
|
- limitdown: quote.limitdown ?? 0,
|
|
|
- averageprice: quote.averageprice ?? 0,
|
|
|
- totalvolume: quote.totalvolume ?? 0,
|
|
|
- totalturnover: quote.totalturnover ?? 0,
|
|
|
- holdvolume: quote.holdvolume ?? 0,
|
|
|
- rise: 0,
|
|
|
- change: 0,
|
|
|
- amplitude: 0,
|
|
|
- bidColor: '',
|
|
|
- bid2Color: '',
|
|
|
- bid3Color: '',
|
|
|
- bid4Color: '',
|
|
|
- bid5Color: '',
|
|
|
- askColor: '',
|
|
|
- ask2Color: '',
|
|
|
- ask3Color: '',
|
|
|
- ask4Color: '',
|
|
|
- ask5Color: '',
|
|
|
- lastColor: '',
|
|
|
- averagepriceColor: '',
|
|
|
- openedColor: '',
|
|
|
- highestColor: '',
|
|
|
- lowestColor: '',
|
|
|
- }
|
|
|
-
|
|
|
- if (item.goodsid) {
|
|
|
- // 更新对象属性
|
|
|
- Object.entries(quote).forEach(([key, value]) => {
|
|
|
- if (value !== undefined) {
|
|
|
- type TKey = keyof Model.GoodsQuote
|
|
|
- (<K extends TKey>(prop: K, value: Model.GoodsQuote[K]) => {
|
|
|
- item[prop] = value
|
|
|
- })(key as TKey, value)
|
|
|
- }
|
|
|
- })
|
|
|
- } else {
|
|
|
- const goods = state.goodsList.find((e) => e.goodscode === quote.goodscode)
|
|
|
- if (goods) {
|
|
|
- ({
|
|
|
- goodsid: item.goodsid,
|
|
|
- goodsname: item.goodsname,
|
|
|
- goodsgroupid: item.goodsgroupid,
|
|
|
- goodunitid: item.goodunitid,
|
|
|
- marketid: item.marketid,
|
|
|
- trademode: item.trademode,
|
|
|
- agreeunit: item.agreeunit,
|
|
|
- decimalplace: item.decimalplace,
|
|
|
- quoteminunit: item.quoteminunit,
|
|
|
- quotegear: item.quotegear,
|
|
|
- } = goods)
|
|
|
-
|
|
|
- // 向列表添加新数据
|
|
|
- state.goodsQuoteList.push(item)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- item.opened = item.opened || item.last // 没有开盘价默认取最新价
|
|
|
- item.highest = item.highest || item.last // 没有最高价默认取最新价
|
|
|
- item.lowest = item.lowest || item.last // 没有最低价价默认取最新价
|
|
|
-
|
|
|
- // 处理最高最低价
|
|
|
- if (item.last) {
|
|
|
- if (item.last > item.highest) {
|
|
|
- item.highest = item.last
|
|
|
- }
|
|
|
- if (item.last < item.lowest) {
|
|
|
- item.lowest = item.last
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- item.averageprice = item.totalvolume ? item.totalturnover / (item.totalvolume * item.agreeunit) : 0 // 计算均价
|
|
|
- item.rise = item.last ? item.last - item.presettle : 0 // 涨跌额/涨跌: 最新价 - 昨结价
|
|
|
- item.change = item.presettle ? item.rise / item.presettle : 0 // 涨跌幅/幅度: (最新价 - 昨结价) / 昨结价
|
|
|
- item.amplitude = item.presettle ? (item.highest - item.lowest) / item.presettle : 0 // 振幅: (最高价 - 最低价 ) / 最新价
|
|
|
-
|
|
|
- // 处理行情价格颜色
|
|
|
- const handleColor = (value: number) => handlePriceColor(value, item.presettle)
|
|
|
-
|
|
|
- item.bidColor = handleColor(item.bid)
|
|
|
- item.bid2Color = handleColor(item.bid2)
|
|
|
- item.bid3Color = handleColor(item.bid3)
|
|
|
- item.bid4Color = handleColor(item.bid4)
|
|
|
- item.bid5Color = handleColor(item.bid5)
|
|
|
- item.askColor = handleColor(item.ask)
|
|
|
- item.ask2Color = handleColor(item.ask2)
|
|
|
- item.ask3Color = handleColor(item.ask3)
|
|
|
- item.ask4Color = handleColor(item.ask4)
|
|
|
- item.ask5Color = handleColor(item.ask5)
|
|
|
- item.lastColor = handleColor(item.last)
|
|
|
- item.averagepriceColor = handleColor(item.averageprice)
|
|
|
- item.openedColor = handleColor(item.opened)
|
|
|
- item.highestColor = handleColor(item.highest)
|
|
|
- item.lowestColor = handleColor(item.lowest)
|
|
|
- }
|
|
|
-
|
|
|
- // 处理行情数据
|
|
|
- const handleQuote = (quote: Proto.Quote): Partial<Model.QuoteDayRsp> => {
|
|
|
- const goods = state.goodsList.find((e) => e.goodscode.toUpperCase() === quote.goodscode?.toUpperCase())
|
|
|
- // 处理报价小数位
|
|
|
- const handleDeimalplace = (value?: number) => {
|
|
|
- if (goods && value) {
|
|
|
- const decimal = Math.pow(10, goods.decimalplace)
|
|
|
- return value / decimal
|
|
|
- }
|
|
|
- return value
|
|
|
- }
|
|
|
- return {
|
|
|
- goodscode: quote.goodscode,
|
|
|
- last: handleDeimalplace(quote.last),
|
|
|
- lasttime: (quote.date && quote.time) ? moment(quote.date + quote.time, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss') : undefined,
|
|
|
- ask: handleDeimalplace(quote.ask),
|
|
|
- ask2: handleDeimalplace(quote.ask2),
|
|
|
- ask3: handleDeimalplace(quote.ask3),
|
|
|
- ask4: handleDeimalplace(quote.ask4),
|
|
|
- ask5: handleDeimalplace(quote.ask5),
|
|
|
- askvolume: quote.askvolume,
|
|
|
- askvolume2: quote.askvolume2,
|
|
|
- askvolume3: quote.askvolume3,
|
|
|
- askvolume4: quote.askvolume4,
|
|
|
- askvolume5: quote.askvolume5,
|
|
|
- bid: handleDeimalplace(quote.bid),
|
|
|
- bid2: handleDeimalplace(quote.bid2),
|
|
|
- bid3: handleDeimalplace(quote.bid3),
|
|
|
- bid4: handleDeimalplace(quote.bid4),
|
|
|
- bid5: handleDeimalplace(quote.bid5),
|
|
|
- bidvolume: quote.bidvolume,
|
|
|
- bidvolume2: quote.bidvolume2,
|
|
|
- bidvolume3: quote.bidvolume3,
|
|
|
- bidvolume4: quote.bidvolume4,
|
|
|
- bidvolume5: quote.bidvolume5,
|
|
|
- calloptionpremiums: quote.calloptionpremiums,
|
|
|
- calloptionpremiums2: quote.calloptionpremiums2,
|
|
|
- calloptionpremiums3: quote.calloptionpremiums3,
|
|
|
- calloptionpremiums4: quote.calloptionpremiums4,
|
|
|
- calloptionpremiums5: quote.calloptionpremiums5,
|
|
|
- exchangecode: quote.exchangecode,
|
|
|
- exchangedate: quote.exchangedate,
|
|
|
- highest: handleDeimalplace(quote.highest),
|
|
|
- holdvolume: quote.holdvolume,
|
|
|
- inventory: quote.inventory,
|
|
|
- lastvolume: quote.lastvolume,
|
|
|
- limitdown: handleDeimalplace(quote.limitlow),
|
|
|
- limitup: handleDeimalplace(quote.limithigh),
|
|
|
- lowest: handleDeimalplace(quote.lowest),
|
|
|
- opened: handleDeimalplace(quote.opened),
|
|
|
- preclose: handleDeimalplace(quote.preclose),
|
|
|
- preholdvolume: quote.preholdvolume,
|
|
|
- presettle: handleDeimalplace(quote.presettle),
|
|
|
- putoptionpremiums: handleDeimalplace(quote.putoptionpremiums),
|
|
|
- putoptionpremiums2: handleDeimalplace(quote.putoptionpremiums2),
|
|
|
- putoptionpremiums3: handleDeimalplace(quote.putoptionpremiums3),
|
|
|
- putoptionpremiums4: handleDeimalplace(quote.putoptionpremiums4),
|
|
|
- putoptionpremiums5: handleDeimalplace(quote.putoptionpremiums5),
|
|
|
- settle: handleDeimalplace(quote.settle),
|
|
|
- totalturnover: handleDeimalplace(quote.totalturnover),
|
|
|
- totalvolume: quote.totalvolume,
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return {
|
|
|
- ...toRefs(state),
|
|
|
- getQuote,
|
|
|
- getQuotePrice,
|
|
|
- getGoodsList,
|
|
|
- getGoodsQuote,
|
|
|
- getGoodsName,
|
|
|
- getGoodsMarket,
|
|
|
- updateGoodsQuote,
|
|
|
- quotePushNotify,
|
|
|
- }
|
|
|
-})
|