lxf
2025-07-05 8c51de06d839339f428ca0691099fe60740765cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<template>
    <div :key="currentSymbol" class="pb-108 ">
        <dev  class="status_bar fixed w-full top-0 left-0 h-44 flex items-center justify-between bg-white z-99">
            <dev  class="status_bar absolute top-0 left-0 w-full h-44 flex items-center justify-center z--1">
                <dev  class="p-3 bg-#EEE rounded-full flex text-12 font-medium">
                    <span  
                    :class="['block h-24 w-84.5 text-center leading-24 rounded-full', currentTab === 'spot' ? 'bg-#3640f0 text-white' : '']"
                        @click="goToTab('spot')"
                        style="cursor:pointer;">
                        <span>{{ t('现货') }}</span>
                    </span>
                    <span   
                    :class="['block h-24 w-84.5 text-center leading-24 rounded-full', currentTab === 'delivery' ? 'bg-#3640f0 text-white' : '']"
                        @click="goToTab('delivery')"
                        style="cursor:pointer;"
           >
                        <span>{{ t('交割') }}</span>
                    </span>
                </dev>
            </dev>
        </dev>
        <div v-show="currentTab === 'delivery'">
            <!-- 头部区 -->
            <ContractHeader :symbol="currentSymbol" :range="range" :selectIndex="selectIndex"
                :balance="userStore.userInfo.balance" @update-coin="onUpdate" page="perpetualContract">
            </ContractHeader>
            <JGKline></JGKline>
            <div>
                <div class="mainBackground rounded-view">
                    <PerpetualOpen class="pl-15 pr-15" :key="keyIndex + 'c'" :selectIndex="selectIndex" :symbol="currentSymbol"
                        :green-data="bids" :red-data="asks" :price="price" :init-open="initOpen" :init-futrue="initFutrue"
                        @ordered="onOrdered" :currentType="currentType" @changeCurrentType="changeCurrentType">
                    </PerpetualOpen>
                    <div class="line"></div>
                    <PerpetualOrder class="pl-15 pr-15" :key="keyIndex + 'd'" :symbol="currentSymbol" :order-cur="orderCur"
                        :order-hold="orderHold" :price="price" :topIndex="selectIndex" :futrue-hold="futrueHold"
                        :futrue-histroy="futrueHistroy" @tab="onTab" @recall="onRecall">
                    </PerpetualOrder>
                </div>
            </div>
        </div>
        <div v-show="currentTab === 'spot'">
            <CoinChart></CoinChart>
        </div>
    </div>
</template>
 
<script setup>
import CoinChart from '@/views/foreign/CoinChart.vue';
import JGKline from './jg_kline.vue';
import PerpetualOpen from '@/components/foreign/foreign-perpetual-open/index.vue'
import PerpetualOrder from '@/components/foreign/foreign-perpetual-order/index.vue'
import { ref, onMounted, onActivated, onDeactivated, onBeforeUnmount } from 'vue';
import ContractHeader from '@/components/foreign/foreign-contract-header/index.vue'
import { _getRealtime } from '@/service/quotes.api'
import { useUserStore } from '@/store/user';
import { useQuotesStore } from '@/store/quotes.store';
import { useRoute, useRouter } from 'vue-router'
import { WS_URL } from '@/config'
import { SET_COIN_LIST, SET_USERINFO } from '@/store/types.store'
import { _getBalance, _c2cPaymentMethodList } from '@/service/user.api.js'
import { _futrueOrderInit, _futrueOrderList, _getDeepData, contractOrder } from "@/service/trade.api.js";
import { useI18n } from 'vue-i18n';
const userStore = useUserStore()
const quotesStore = useQuotesStore()
const router = useRouter()
const route = useRoute()
const { t } = useI18n()
 
const currentTab = ref('delivery')
const goToTab = (val) => {
    currentTab.value = val
}
 
 
const initTimer = ref(null)
let keyIndex = ref(0)
let timeout = ref(null)
let timer = ref(null) // 底部持仓等的公用定时器
let timer2 = ref(null) // 交割合约底部持仓等的公用定时器
let balance = ref(0)
let currentSymbol = ref('')
let price = ref('')
let range = ref('')
let initOpen = ref({})
const initArr = []
for (let i = 0; i < 6; i++) {
    initArr.push({
        amount: '--',
        price: '--'
    })
}
let asks = ref(initArr)  // 卖单
let bids = ref(initArr) // 买单
let orderCur = ref([]) // 当前委托
let orderHold = ref([]) // 持有仓位
let futrueHold = ref([]) // 交割持仓
let futrueHistroy = ref([]) // 交割历史
let sockets = ref({
    quotes: null, // 行情
    deep: null /// 深度
})
let curTab = ref('fetchFutrueHoldList')
let selectIndex = ref(2) // 当前tab
let initFutrue = ref({}) /// 交割合约
let show = ref(false) // popup
let animated1 = ref(false)
let animated2 = ref(false)
let currentType = ref('')
 
const onUpdate = (symbol) => { // 更新
    currentSymbol.value = symbol
    closeSocket()
    init(symbol)
}
 
const onRecall = () => { // 撤单or 平仓 evt
    clearTimer()
    contractOrder({
        symbol: currentSymbol.value,
        type: 'orders',
        page_no: 1,
        symbolType: 'cryptos'
    }).then(data => {
        orderHold.value = data
    })
    if (curTab.value == 'fetchFutrueHoldList') {
        fetchFutrueHoldList(currentSymbol.value)
    } else {
        fetchFutrueHistory(currentSymbol.value)
    }
}
 
const onOrdered = (evt) => { // 下单过后的回调
    clearTimer()
    // this.clearTimeout()
    console.log('下单过后的回调onOrdered:',evt)
    initParam(currentSymbol.value, evt) // 重新初始化
    // TODO: 这里要做判断
    if (curTab.value == 'fetchFutrueHoldList') {
        fetchFutrueHoldList(currentSymbol.value)
    } else {
        fetchFutrueHistory(currentSymbol.value)
    }
}
 
const onTab = (evt) => { // 点击tab后的回调
    console.log('evt1', evt)
    clearTimer()
    curTab.value = evt
    if (curTab.value == 'fetchFutrueHoldList') {
        fetchFutrueHoldList(currentSymbol.value)
    } else {
        fetchFutrueHistory(currentSymbol.value)
    }
}
 
const fetchQoutes = (symbol) => { // 获取当前行情
    _getRealtime(symbol).then(data => { // 获取行情
        handleQoutes(data)
        startQuoteSocket() // socket
    })
}
 
const handleQoutes = (data) => {
    if (data && data.length) {
        const cur = data[0]
        price.value = cur.close
        range.value = cur.change_ratio + ''
    }
}
 
const fetchDeepData = (symbol) => {
    console.log('sd')
    _getDeepData(symbol).then(data => { // 获取深度
        handleDeep(data)
        startDeepSocket() // socket
    })
}
 
const handleDeep = (data) => {
    deepData.value = data
    const { asks, bids } = data
    asks.value = asks.sort((a, b) => a.price - b.price).slice(0, 6)
    bids.value = bids.sort((a, b) => a.price - b.price).slice(-6)
}
 
const startQuoteSocket = () => { // 行情socket
    sockets.value.quotes = new WebSocket(`${WS_URL}/1/${currentSymbol.value}`)
    sockets.value.quotes.onmessage = (evt) => {
        const { data } = evt
        const { code, data: _data } = JSON.parse(data)
        if (code / 1 === 0) {
            handleQoutes(_data)
        }
    }
}
 
const startDeepSocket = () => {
    sockets.value.deep = new WebSocket(`${WS_URL}/3/${currentSymbol.value}`)
    sockets.value.deep.onmessage = (evt) => {
        const { data } = evt
        const { code, data: _data } = JSON.parse(data)
        if (code / 1 === 0) {
            handleDeep(_data)
        }
    }
}
const initParam = (symbol, type) => { // 初始化参数
    if (type === 'futrue' || !type) {
        _futrueOrderInit(symbol).then(data => {
            initFutrue.value = data
        })
    }
}
 
const fetchOrderListCur = (symbol) => { // 当前委托
    console.log('当前委托')
    if (userStore.userInfo.token) {
        _contractApplyOrderList({
            symbol,
            type: 'orders',
            page_no: 1
        }).then(data => {
            orderCur.value = data
        })
        clearTimer()
        timer.value = setInterval(() => {
            _contractApplyOrderList({
                symbol,
                type: 'orders',
                page_no: 1
            }).then(data => {
                orderCur.value = data
            })
        }, 1000)
    }
}
 
const fetchFutrueHoldList = (symbol) => { // 交割持仓
    if (userStore.userInfo.token) {
        _futrueOrderList(symbol).then(data => {
            futrueHold.value = data
        })
        timer.value = setInterval(() => {
            _futrueOrderList(symbol).then(data => {
                futrueHold.value = data
            })
        }, 1000)
    }
}
 
const fetchFutrueHistory = (symbol) => { // 交割历史持仓
    _futrueOrderList(symbol, 'hisorders').then(data => {
        futrueHistroy.value = data
    })
}
const init = (symbol) => { // 初始化页面
    currentSymbol.value = symbol
    fetchQoutes(symbol)
    // fetchDeepData(symbol)
    initParam(symbol) // 'open'
    clearTimer()
    fetchFutrueHoldList(symbol)
}
 
const closeSocket = () => {
    sockets.value.quotes && sockets.value.quotes.close()
    sockets.value.deep && sockets.value.deep.close()
    sockets.value.quotes = null
    sockets.value.deep = null
}
 
const clearTimer = () => {
    clearInterval(timer.value)
    timer.value = null
}
const changeCurrentType = (type) => {
    currentType.value = type
}
 
onMounted(async () => {
    let symbol = route.params.symbol
    if (symbol) {
        currentSymbol.value = symbol
        init(currentSymbol.value)
    }
    if (!quotesStore.coinList.length) {
        await quotesStore[SET_COIN_LIST]()
    }
    _getBalance().then(data => { // 获取用户余额
        quotesStore[SET_USERINFO, { balance: data.money }]
    })
    currentType.value = route.query.currentType ? route.query.currentType : 'long'
})
 
 
onDeactivated(() => {
    closeSocket()
    clearTimer()
})
 
onActivated(() => {
    currentType.value = route.query.currentType ? route.query.currentType : 'long'
})
 
onBeforeUnmount(() => {
    closeSocket()
    clearTimer()
})
 
 
 
 
 
 
 
 
 
</script>
 
<style lang="scss" scoped>
@import '@/assets/css/deepseek_css_20250625_30ff932.css';
 
//.list-enter-active, .list-leave-active {
//  transition: all .5s;
//  transform: translateY(30px)
//}
//.list-enter, .list-leave-to
//  /* .list-leave-active for below version 2.1.8 */ {
//  transform: translateY(0)
//}
 
.list-enter-active,
.list-leave-active {
    will-change: transform;
    transition: all 250ms;
}
 
.list-enter {
    opacity: 0;
    transform: translate3d(-100%, 0, 0);
}
 
.list-leave {
    opacity: 0;
    transform: translate3d(100%, 0, 0);
}
 
.rounded-view {
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    box-sizing: border-box;
}
 
.my-swipe {
    width: 100%;
}
 
.my-swipe .van-swipe-item {}
 
.line {
    height: 6px;
    background: #f5f5f5;
}
 
@keyframes animate1 {
    0% {
        opacity: 1;
        transform: translate3d(100%, 0, 0);
    }
 
    //   40% {
    //      opacity: 1;
    //     transform: translate3d(50%, 0, 0);
    //   }
    100% {
        opacity: 1;
        transform: translate3d(0%, 0, 0);
    }
}
 
@keyframes animate2 {
    0% {
        opacity: 1;
        transform: translate3d(-100%, 0, 0);
    }
 
    //   40% {
    //      opacity: 1;
    //     transform: translate3d(50%, 0, 0);
    //   }
    100% {
        opacity: 1;
        transform: translate3d(0%, 0, 0);
    }
}
 
 
.slide2 {
    animation: animate2 200ms linear;
}
 
.pb-108 {
    padding-bottom: 54px;
}
</style>