交易所前端蓝色ui, 4.5 jiem
lxf
2025-04-18 66a33e936d39ec4db7fdffed5d646e044ccc43e9
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
<template>
    <div :key="symbol" class="pb-108 no_touch">
        <!-- 头部区 -->
        <ContractHeader  :symbol="symbol" :range="range" :selectIndex="selectIndex" :balance="userInfo.balance"
            @tab="onTopTab" @update-coin="onUpdate">
        </ContractHeader>
 
        <div :class="{ slide2: animated1 }" v-if="selectIndex === 1">
            <div class="mainBackground" key="x">
                <PerpetualOpen class="pl-30 pr-30" :key="keyIndex + 'a'" :selectIndex="selectIndex" :symbol="symbol"
                    :green-data="bids" :red-data="asks" :price="price" :init-open="initOpen" :init-close="initClose"
                    :init-futrue="initFutrue" @ordered="onOrdered" :currentType="currentType"
                    @changeCurrentType="changeCurrentType" @changeValueBack="changeValueBack">
                </PerpetualOpen>
                <div class="line"></div>
                <!-- 委托/持仓-->
                <PerpetualOrder class="pl-30 pr-30" :key="keyIndex + 'b'" :symbol="symbol" :order-cur="orderCur"
                    :order-hold="orderHold" :topIndex="selectIndex" :futrue-hold="futrueHold"
                    :futrue-histroy="futrueHistroy" @tab="onTab" @recall="onRecall">
                </PerpetualOrder>
            </div>
        </div>
        <div :class="{ slide1: animated2 }" v-else>
            <div class="mainBackground" key="y">
                <PerpetualOpen class="pl-30 pr-30" :key="keyIndex + 'c'" :selectIndex="selectIndex" :symbol="symbol"
                    :green-data="bids" :red-data="asks" :price="price" :init-open="initOpen" :init-close="initClose"
                    :init-futrue="initFutrue" @ordered="onOrdered" @changeValueBack="changeValueBack">
                </PerpetualOpen>
                <div class="line"></div>
                <!-- 委托/持仓-->
                <PerpetualOrder class="pl-30 pr-30" :key="keyIndex + 'd'" :symbol="symbol" :order-cur="orderCur"
                    :order-hold="orderHold" :price="price" :topIndex="selectIndex" :futrue-hold="futrueHold"
                    :futrue-histroy="futrueHistroy" @tab="onTab" @recall="onRecall">
                </PerpetualOrder>
            </div>
        </div>
        <!-- <div class="fixed w-full shadow z-10 tabBackground1"> -->
            <div class="flex justify-between px-30 py-10">
                <span class="font-24 textColor2" >{{ symbolname }}&nbsp;{{ $t('k线图表') }}</span>
                <!-- <van-icon class="textColor" @click.stop="showCharts = !showCharts"
                    :name="showCharts ? 'arrow-down' : 'arrow-up'"></van-icon> -->
            </div>
            <k-line-charts :update-key="updateKey" :update-data="quote" :symbol="symbol" 
                :showBottom="false" />
        <!-- </div> -->
    </div>
</template>
 
<script>
import ContractHeader from '@/components/contract-header/index.vue'
import PerpetualOpen from '@/components/perpetual-open/index.vue'
import PerpetualOrder from '@/components/perpetual-order/index.vue'
 
import { _getBalance, _getDeepData, _initOpen, _initClose, _orderListCur, _orderListHold, _futrueOrderInit, _futrueOrderList } from '@/API/trade.api'
import { _getHomeList } from '@/API/home.api'
import { Popup, Swipe, SwipeItem } from 'vant';
import { mapActions, mapGetters } from 'vuex'
import { SET_COIN_LIST } from '@/store/const.store'
import { WS_URL } from '@/config'
import { getStorage } from '@/utils/utis'
import KLineCharts from '@/components/kline-charts'
var showLength = 7
export default {
    name: "perpetualContract",
    components: {
        ContractHeader,
        PerpetualOpen,
        PerpetualOrder,
        [Popup.name]: Popup,
        [Swipe.name]: Swipe,
        [SwipeItem.name]: SwipeItem,
        KLineCharts
    },
    computed: {
        ...mapGetters('user', ['userInfo']),
        ...mapGetters({
            coinList: 'home/coinList',
        }),
    },
    watch: {
        selectIndex(val) {
            showLength = 7
        }
    },
    data() {
        const initArr = []
        for (let i = 0; i < showLength; i++) {
            initArr.push({
                amount: '--',
                price: '--'
            })
        }
        return {
            quote: {},
            initTimer: null,
            keyIndex: 0,
            timeout: null,
            timer: null, // 底部持仓等的公用定时器
            timer1: null, // tab切换动画
            timer2: null, // 交割合约底部持仓等的公用定时器
            balance: 0,
            symbol: '',
            price: '',
            range: '',
            stop_price_profit:'',
            stop_price_loss:'',
            initOpen: {},
            initClose: {},
            asks: initArr, // 卖单
            bids: initArr, // 买单
            orderCur: [], // 当前委托
            orderHold: [], // 持有仓位
            futrueHold: [], // 交割持仓
            futrueHistroy: [], // 交割历史
            sockets: {
                quotes: null, // 行情
                deep: null /// 深度
            },
            curTab: '', // 当前委托还是持有仓位
            selectIndex: 1, // 当前tab
            initFutrue: {}, /// 交割合约
            show: false, // popup
            animated1: false,
            animated2: false,
            currentType: '',
            showCharts: false,
            symbolname:'',
            // curRequest: [], //
        }
    },
    async created() {
 
        if (!this.coinList.length) {
            await this.SET_COIN_LIST()
        }
        this.symbolname = getStorage('symbolname')
        _getBalance().then(data => { // 获取用户余额
            this.$store.commit('user/SET_USERINFO', { balance: data.money })
            // const { money } = data
            // this.balance = money
        })
    },
    methods: {
        ...mapActions('home', [SET_COIN_LIST]),
        onUpdate(symbol) { // 更新
            this.symbol = symbol
            this.closeSocket()
            this.init(symbol)
        },
        onRecall() { // 撤单or 平仓 evt
            this.clearTimer()
            _orderListHold(this.symbol).then(data => {
 
                this.orderHold = data
            })
            this[this.curTab](this.symbol)
            _initOpen(this.symbol).then(data => {
                this.initOpen = data
            })
        },
        onTopTab(evt) { // 当前tab 永续/交割
            this.keyIndex += 1
            this.selectIndex = evt
            this.clearTimer()
            if (this.selectIndex / 1 === 1) {
                //this.curTab = 'fetchOrderListCur'
                this.curTab = 'fetchOrderListHold'
                // this.animated1 = true
                // this.timer = setTimeout(() => {
                //     this.animated1 = false
                //     clearTimeout(this.timer)
                // }, 200)
                this.animated2 = true
                this.timer1 = setTimeout(() => {
                    this.animated2 = false
                    clearTimeout(this.timer1)
                }, 200)
            } else {
                // this.animated2 = true
                // this.timer = setTimeout(() => {
                //     this.animated2 = false
                //     clearTimeout(this.timer)
                // }, 200)
                this.animated1 = true
                this.timer1 = setTimeout(() => {
                    this.animated1 = false
                    clearTimeout(this.timer1)
                }, 200)
            }
            console.log(this.curTab)
            this[this.curTab](this.symbol)
        },
        onOrdered(evt) { // 下单过后的回调
            this.clearTimer()
            // this.clearTimeout()
            this.initParam(this.symbol, evt) // 重新初始化
            // TODO: 这里要做判断
            if (this.selectIndex / 1 === 1) {
                this[this.curTab](this.symbol) // 重新调取记录
                console.log('this.curTab', this.curTab)
            } else { // 交割合约
                this[this.curTab](this.symbol)
                // this.show = true
                console.log('curTab', evt, this.curTab)
 
            }
            //console.log('下单后更新数据')
        },
        onTab(evt) { // 点击tab后的回调
            console.log('evt', evt)
            this.clearTimer()
            // this.clearTimeout()
            this.curTab = evt
            this[evt](this.symbol)
        },
        fetchQoutes(symbol) { // 获取当前行情
            _getHomeList(symbol).then(data => { // 获取行情
                this.handleQoutes(data)
                this.startQuoteSocket() // socket
            })
        },
        handleQoutes(data) {
            if (data && data.length) {
                const cur = data[0]
                this.price = cur.close
                this.range = cur.change_ratio + ''
                this.quote = cur
                this.updateKey++
            }
        },
        fetchDeepData(symbol) {
            console.log('sd')
            _getDeepData(symbol).then(data => { // 获取深度
                console.log(data)
                this.handleDeep(data)
                this.startDeepSocket() // socket
            })
        },
        handleDeep(data) {
            this.deepData = data
            const { asks, bids } = data
            this.asks = asks.sort((a, b) => a.price - b.price).slice(0, showLength)
            this.bids = bids.sort((a, b) => a.price - b.price).slice(-showLength)
        },
        startQuoteSocket() { // 行情socket
            this.sockets.quotes = new WebSocket(`${WS_URL}/1/${this.symbol}`)
            // socket.onopen =  () => {
            //     console.log('open')
            //     socket.send('hello')
            // }
            this.sockets.quotes.onmessage = (evt) => {
                const { data } = evt
                const { code, data: _data } = JSON.parse(data)
                if (code / 1 === 0) {
                    this.handleQoutes(_data)
                }
            }
        },
        startDeepSocket() {
            this.sockets.deep = new WebSocket(`${WS_URL}/3/${this.symbol}`)
            this.sockets.deep.onmessage = (evt) => {
                const { data } = evt
                const { code, data: _data } = JSON.parse(data)
                if (code / 1 === 0) {
                    this.handleDeep(_data)
                }
            }
        },
        initParam(symbol, type) { // 初始化参数
            if (type === 'open' || type === 'long' || type === 'short' || !type) {
                let initFunTimer = null;
                let initFun = () => {
                    _initOpen(symbol).then(data => {
                        this.initOpen = data
                        clearTimeout(initFunTimer)
                        initFunTimer = null
                    }).catch(err => {
                        initFunTimer = setTimeout(() => {
                            initFun()
                        }, 3000);
                    })
                }
                initFun()
            }
            if (type === 'close' || !type) {
                let initFunTimer = null;
                let initFun = () => {
                    _initClose(symbol).then(data => {
                        this.initClose = data
                        console.log(this.initClose)
                        clearTimeout(initFunTimer)
                        initFunTimer = null
                    }).catch(err => {
                        initFunTimer = setTimeout(() => {
                            initFun()
                        }, 3000);
                    })
                }
                initFunTimer = setTimeout(() => {
                    initFun()
                }, 600);
            }
            if (type === 'futrue' || !type) {
                _futrueOrderInit(symbol).then(data => {
                    this.initFutrue = data
                })
            }
        },
        fetchOrderListCur(symbol) { // 当前委托
            //console.log('当前委托')
            if (this.userInfo.token) {
                _orderListCur(symbol).then(data => {
                    this.orderCur = data
                })
                this.clearTimer()
                this.timer = setInterval(() => {
                    _orderListCur(symbol).then(data => {
                        // if (typeof this.timer === 'string') {
                        //   this.timer = null
                        //   return
                        // }
                        this.orderCur = data
                    })
                }, 1000)
            } else {
                this.orderCur = []
            }
        },
        fetchOrderListHold(symbol) { // 当前持仓
            if (this.userInfo.token) {
                _orderListHold(symbol).then(data => {
                    // this.orderHold = data
                    this.orderHold = data.sort(this.sortData);
                })
                this.timer = setInterval(() => {
                    _orderListHold(symbol).then(data => {
                        // if (typeof this.timer === 'string') {
                        //   this.timer = null
                        //   return
                        // }
                        // this.orderHold = data
                        this.orderHold = data.sort(this.sortData);
                    })
                }, 1000)
            } else {
                this.orderHold = []
            }
        },
        fetchFutrueHoldList(symbol) { // 交割持仓
            if (this.userInfo.token) {
                let length = 0
                _futrueOrderList(symbol).then(data => {
                    // this.futrueHold = data
                    this.futrueHold = data.sort(this.sortData);
                    length = data.length
                })
                this.timer = setInterval(() => {
                    _futrueOrderList(symbol).then(data => {
                        // if (typeof this.timer === 'string') {
                        //   this.timer = null
                        //   return
                        // }
 
                        this.futrueHold = data.sort(this.sortData);
                        if (length != data.length) {
                            length = data.length
                            _futrueOrderInit(symbol).then(dataList => {
                                this.initFutrue = dataList
                            })
                        }
                    })
                }, 1000)
            } else {
                this.futrueHold = []
            }
        },
        fetchFutrueHistory(symbol) { // 交割历史持仓
            _futrueOrderList(symbol, 'hisorders').then(data => {
                this.futrueHistroy = data
                // this.clearTimeout()
            })
        },
        init(symbol) { // 初始化页面
            this.symbol = symbol.toLowerCase()
            this.fetchQoutes(symbol)
            this.fetchDeepData(symbol)
            this.initParam(symbol) // 'open'
            this.clearTimer()
            //if (this.curTab === 'fetchOrderListCur') {
            if (this.curTab === 'fetchOrderListHold') {
                //this.fetchOrderListCur(symbol)
                this.fetchOrderListHold(symbol)
            } else {
                this.fetchFutrueHoldList(symbol)
            }
        },
        closeSocket() {
            this.sockets.quotes && this.sockets.quotes.close()
            this.sockets.deep && this.sockets.deep.close()
            this.sockets.quotes = null
            this.sockets.deep = null
        },
        // clearTimeout(noNeed) {
        //     clearTimeout(this.timeout)
        //     if (noNeed) { // 防止timout定时器因接口返回继续触发
        //       this.timeout = ''
        //       return
        //     }
        //     this.timeout = null
        // },
        clearTimer() {
            clearInterval(this.timer)
            // if (isNeed) {
            //   this.timer = ''
            //   return
            // }
            this.timer = null
        },
        changeCurrentType(type) {
            this.currentType = type
        },
        changeValueBack(val) {
            if (val == 0) {
                showLength = 7
            } else {
                showLength = 13
            }
        },
        sortData(a, b) {
            return new Date(b.open_time).getTime() - new Date(a.open_time).getTime()
        }
    },
    mounted() {
        let symbol = this.$route.params.symbol
        let catchSymbol = getStorage('symbol')
        if (!symbol && catchSymbol) {
            symbol = catchSymbol
        }
        if (this.selectIndex / 1 === 2) {
            this.curTab = 'fetchFutrueHoldList'
        } else {
            this.curTab = 'fetchOrderListHold'
        }
        this.symbol = symbol
        this.init(symbol)
    },
    activated() {
        let symbol = this.$route.params.symbol
        let catchSymbol = getStorage('symbol')
        if (!symbol && catchSymbol) {
            symbol = catchSymbol
        }
        if (this.selectIndex / 1 === 2) {
            this.curTab = 'fetchFutrueHoldList'
        } else {
            this.curTab = 'fetchOrderListHold'
        }
        this.symbol = symbol
        this.init(symbol)
    },
    // beforeRouteEnter(to, from, next) {
    //     let { params: { symbol }, query: { selectIndex } } = to
    //     // if(this.$route.query.selectIndex) {
    //     //   this.selectIndex = this.$route.query.selectIndex
    //     // }
    //     let catchSymbol = getStorage('symbol')
    //     if (!symbol && catchSymbol) {
    //         symbol = catchSymbol
    //     }
    //     if (symbol) {
    //         next(vm => {
    //             if (selectIndex) {
    //                 vm.selectIndex = selectIndex
    //                 if (vm.selectIndex / 1 === 2) {
    //                     vm.curTab = 'fetchFutrueHoldList'
    //                 } else {
    //                     //vm.curTab = 'fetchOrderListCur'
    //                     vm.curTab = 'fetchOrderListHold'
    //                 }
    //             } else {
    //                 //vm.curTab = 'fetchOrderListCur'
    //                 vm.curTab = 'fetchOrderListHold'
    //             }
    //             vm.symbol = symbol
    //             vm.init(symbol)
    //         })
    //     } else {
    //         next()
    //     }
    // },
    deactivated() {
        this.closeSocket()
        this.clearTimer()
    },
    activated() {
        this.currentType = this.$route.query.currentType ? this.$route.query.currentType : 'long'
    },
    beforeDestroy() {
        this.closeSocket()
        // this.clearTimeout(true)
        this.clearTimer()
    }
}
 
</script>
 
<style lang="scss" scoped>
//.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)
//}
.no_touch {
    -webkit-user-select: none;
    user-select: none;
    -ms-user-select: none;
    -moz-user-select: none;
    padding-bottom: 150px !important;
}
 
.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: 13px;
 
    @include themify() {
        background: themed("tab_background");
    }
}
 
@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);
    }
}
 
.slide1 {
    animation: animate1 200ms linear;
}
 
.slide2 {
    animation: animate2 200ms linear;
}
 
.line {
    @include themify() {
        background: themed("divi_line");
    }
}
 
::v-deep .contract-header {
    @include themify() {
        background: themed("main_background");
    }
}
 
.tabBackground1 {
    @include themify() {
        border: 1px solid themed("border_color1");
    }
}
 
.shadow {
    bottom: 108px;
    box-sizing: border-box;
}
</style>