lxf
2025-07-04 5ef5f50d09b49795c4cc9ca017294cbb74083cae
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
<template>
  <section>
    <fx-header></fx-header>
    <div class="open-index">
      <!-- 币种基本信息 -->
      <div class="base-wrapper">
        <div class="name">{{ route.query.symbol }}</div>
        <div class="font-700">{{ quoteData?.close }}</div>
      </div>
      <!-- tab 栏 -->
      <div class="tab-wrapper flex-all-center">
        <div :class="{ 'active': activeTab === 1 }" class="flex-all-center flex-1" @click="changeTab(1)">
          {{ t('marketPrice') }} </div>
        <div :class="{ 'active': activeTab === 2 }" class="flex-all-center flex-1" @click="changeTab(2)">
          {{ t('限价') }} </div>
      </div>
      <!-- 开仓价格 -->
      <div class="volumn-wrapper" v-if="activeTab == 2">
        <div class="title">{{ t('price') }} {{ `(${route.query.closePrice})` }}</div>
        <div class="operator">
          <div class="minus btn flex-all-center" @click="handleChangePrice('0.1', '-')">-</div>
          <div class="number  flex-all-center">
            <input style="text-align: center;" class="btn" v-model="inputPrice" type="number" />
          </div>
          <div class="add btn flex-all-center" @click="handleChangePrice('0.1', '+')">+</div>
        </div>
      </div>
      <!-- 开仓数量 -->
      <div class="volumn-wrapper">
        <div class="title">{{ t('可开张数') }}
          <!-- <span v-if="initOpen.volume > 1">{{ `(1 ~ ${initOpen.volume})` }}</span> -->
          <span>{{ `(${initOpen.volume})` }}</span>
        </div>
        <div class="operator">
          <div class="minus btn flex-all-center" @click="handleChangeVolumn(1, '-')">-</div>
          <div class="number  flex-all-center">
            <input style="text-align: center;" @input="changeVolumn" class="btn" v-model="inputVolumn" type="number" />
          </div>
          <div class="add btn flex-all-center" @click="handleChangeVolumn(1, '+')">+</div>
        </div>
        <div class="mainBox">
          <div class="slider" v-if="initOpen.volume > 1">
            <vue-slider @change="onSliderChange" class="mainBox" :marks="marks" v-model="inputVolumnSlider"
              :hide-label="true" width="98%" tooltip="hover" :tooltip-formatter="'{value}%'"
              :railStyle="{ background: '#417EF6', height: '4px' }"
              :processStyle="{ background: '#266BFF', height: '4px' }">
              <template v-slot:step="{ active }">
                <div :class="['custom-step', { active }]"></div>
              </template>
            </vue-slider>
          </div>
        </div>
 
      </div>
      <!-- 下单信息 -->
      <div class="open-wrapper">
        <div class="open-item mb-10" v-for="(item, index) in infoList" :key="index">
          <span class="label">{{ item.label }}</span>
          <!-- 杠杆下拉框 -->
          <van-dropdown-menu class="value" v-if="item.value == 'lever'">
            <van-dropdown-item v-model="curLevel" :options="levelList" /></van-dropdown-menu>
          <span class="value" v-else>{{ getInfoValue(item.value) }}</span>
        </div>
      </div>
      <!-- 下单按钮 -->
      <div :class="[inputVolumn > 0 ? route.query.type : 'disabled']" class="btn-wrapper flex-all-center"
        @click="handleSubmit">{{ t(route.query.type) }} </div>
      <!-- 下单的弹窗 -->
      <van-popup round v-model:show="showPop" position="bottom" :close-on-click-overlay="false" @close="onClose">
        <div class="pop-box mainBackground">
          <img src="@/assets/image/wallet/close3.png" class="top-46 close-btn" @click="onClose" />
          <div class="title">{{ t('Opening') + t('success') }}</div>
          <div class="content flex-all-center">
            <img class="w-12 h-12 mb-4" src="@/assets/image/choice-blue.png" alt="avatar" />
            <div class="text1">{{ t('Finish') }}</div>
            <div class="text2">{{ route.query.symbol }}</div>
            <div class="text3" :class="route.query.type + '-text'">{{ activeTab === 1 ? t('marketPrice') :
              t('限价')
            }}
              <span>{{ t('volumn') }} {{ inputVolumn }}</span>
            </div>
            <div :class="route.query.type" class="btn-wrapper flex-all-center see-btn" @click="gotoPosition">{{
              t('viewposition')
            }} </div>
          </div>
        </div>
      </van-popup>
    </div>
  </section>
</template>
 
<script setup>
import { onMounted, ref, watch } from 'vue';
import { showToast } from 'vant';
import { _getAllAssets } from "@/service/user.api.js";
import { _initOpen, _orderOpen, _initClose } from "@/service/trade.api.js";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from 'vue-router';
import VueSlider from "vue-slider-component";
 
const { t } = useI18n()
const route = useRoute()
const router = useRouter()
const activeTab = ref(1)
const inputVolumn = ref('1') //数量输入
const inputPrice = ref(route.query.closePrice) //价格输入
const showPop = ref(false)
const levelList = ref([])
const curLevel = ref(1) //杠杆初始化为1倍
const initOpen = ref({})
const quoteData = ref({})
const inputVolumnSlider = ref('1')
const marks = (val) => val % 25 === 0
 
const infoList = [{
  label: t('lever'),
  value: 'lever',
}, {
  label: t('margin'),
  value: 'margin',
}, {
  label: t('money'), //合约金额
  value: 'money',
}, {
  label: t('fee'),
  value: 'fee',
}];
 
// 下单的参数
const formData = ref({
  symbol: '', // 币种
  session_token: '',
  direction: 'buy', // 买or卖
  price_type: 'opponent', // 市价or限价
  lever_rate: 1, // 杠杆
  price: '',
  amount: '', // 数量
})
 
// TODO 路由参数不刷新
watch(route, (newval, oldval) => {
  console.log('开仓页面watch', route.query.closePrice)
}, { deep: true })
 
onMounted(async () => {
  await initParams()
  innitLever(initOpen.value)
})
 
const initParams = async () => {
  const { symbol, type } = route.query
  const res = await _initOpen({ symbol })
  initOpen.value = res;
}
 
const innitLever = (res) => {
  if (res.lever && res.lever.length) {
    levelList.value = res.lever?.map(it => {
      return {
        text: `${it.lever_rate}x`,
        value: it.lever_rate
      }
    });
    curLevel.value = levelList.value[0].value //默认选中第一个
  } else {
    levelList.value = [{
      text: "1x",
      value: 1
    }]
  }
}
 
 
const onSliderChange = (value) => {
  let data;
  if (initOpen.value.volume) {
    if (value == 0) {
      inputVolumn.value = 1;
    } else {
      const rate = value / 100; //如0.25
      data = initOpen.value.volume * rate;
      inputVolumn.value = parseInt(data);
    }
  }
};
 
const handleSubmit = async () => {
  if (inputVolumn.value === 0) {
    return
  }
  // 下单之前,刷新token
  await initParams()
  const { symbol, type } = route.query;
  formData.value.symbol = symbol;
  formData.value.direction = type;
  formData.value.lever_rate = curLevel.value;
  formData.value.price_type = activeTab.value === 2 ? 'limit' : 'opponent'; //
  const closePrice = route.query.closePrice;
  formData.value.price = activeTab.value === 2 ? inputPrice.value : closePrice;
  formData.value.session_token = initOpen.value.session_token;
  formData.value.amount = inputVolumn.value;  //不用乘以一手的数量,服务端乘了
  _orderOpen(formData.value).then(res => {
    showPop.value = true
  }).catch(error => {
    // TODO:
    // router.go(-1)
    showToast(t(error))
  })
}
 
const getInfoValue = (val) => {
  switch (val) {
    case 'fee':
      return inputVolumn.value * initOpen.value.fee;
      break;
    case 'lever':
      return `${curLevel.value}x`;
      break;
    default:
      // 合约和保证金
      return inputVolumn.value * initOpen.value.amount * curLevel.value;
  }
}
 
const changeTab = (type) => {
  inputVolumnSlider.value = 1
  inputVolumn.value = 1
  activeTab.value = type
}
 
const handleChangePrice = (step, type) => {
  if (type === '-') {
    if (Number(inputPrice.value) > 0) {
      inputPrice.value = (Number(inputPrice.value) - Number(step)).toFixed(4)
    } else {
      inputPrice.value = 0
    }
  } else {
    inputPrice.value = (Number(inputPrice.value) + Number(step)).toFixed(4)
  }
}
 
const handleChangeVolumn = (step, type) => {
  if (type === '-') {
    if (Number(inputVolumn.value) > 0) {
      inputVolumn.value = Number(inputVolumn.value) - Number(step)
    } else {
      inputVolumn.value = 0
    }
  } else {
    inputVolumn.value = Number(inputVolumn.value) + Number(step)
  }
}
//滑块处理
const changeVolumn = () => {
  inputVolumnSlider.value = ((inputVolumn.value / initOpen.value.volume) * 100).toFixed(2)
}
const onClose = () => {
  showPop.value = false
}
 
const gotoPosition = () => {
  router.push('/position/index')
}
</script>
 
<style lang="scss" scoped>
:deep(.van-dropdown-menu__bar) {
  height: 30px !important;
  width: 90px;
  background: $selectSymbol_background;
  border: 1px solid $border_color;
}
 
:deep(.van-dropdown-menu__title) {
  color: $text_color;
}
 
:deep(.van-cell.van-dropdown-item__option) {
  background: $selectSymbol_background;
  color: $text_color;
}
 
:deep(.van-dropdown-item__option--active) {
  color: $color_main !important;
}
 
:deep(.van-cell:after) {
  border-bottom: none;
}
 
// :deep(.van-dropdown-item){
//   left: 70%;
// }
// :deep(.van-dropdown-item--down){
//   width: 90px;
//   margin-top: 10px;
//   background: #fff;
// }
// // z-index高就可以了
// :deep(.van-dropdown-item__content){
//   box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.15);
//   border-radius: 6px;
 
// }
 
.open-index {
  padding: 0 20px;
 
  .base-wrapper {
    display: flex;
    justify-content: space-between;
    margin: 24px 0;
    font-size: 20px;
    font-weight: 600;
 
  }
 
  .tab-wrapper {
    width: 100%;
    height: 50px;
    background: #1B2134;
    border-radius: 22px;
    font-weight: 700;
    font-size: 16px;
 
    .active {
      width: 186px;
      height: 45px;
      background: $blue;
      border-radius: 22px;
      color: $text_color;
    }
  }
 
  .volumn-wrapper {
    margin-top: 24px;
 
    .title {
      font-weight: 600;
      font-size: 16px;
      color: #818181;
    }
 
    .operator {
      display: flex;
      margin-top: 28px;
 
      .btn {
        height: 48px;
        background: $selectSymbol_background;
        border-radius: 8px;
        color: $blue;
        font-size: 24px;
      }
 
      input.btn {
        color: $text_color !important;
      }
 
      .minus,
      .add {
        width: 48px;
      }
 
      .number {
        width: 260px;
      }
 
      .minus,
      .number {
        margin-right: 8px;
      }
    }
 
    .slider {
      margin-top: 40px;
    }
 
    .gears {
      margin-top: 10px;
      font-size: 12px;
    }
 
  }
 
  .open-wrapper {
    margin-top: 24px;
 
    .open-item {
      display: flex;
      justify-content: space-between;
      margin-bottom: 12px;
 
      .label {
        font-weight: 600;
        font-size: 16px;
        color: $text_color;
      }
 
      .value {
        font-weight: 600;
        font-size: 18px;
        color: $text_color;
      }
    }
  }
 
  .btn-wrapper {
    width: 100%;
    height: 50px;
    border-radius: 12px;
    margin-top: 24px;
    font-weight: 600;
    font-size: 22px;
    color: $text_color;
  }
 
  .sell {
    background: linear-gradient(90deg, #2C64D4 0%, #38AEEA 100%);
 
  }
 
  .disabled {
    background: #E5E5E5;
  }
 
  .buy {
    background: linear-gradient(90deg, #2C64D4 0%, #38AEEA 100%);
  }
 
  .flex-all-center {
    display: flex;
    align-items: center;
    justify-content: center;
  }
 
  .mainBackground {
    padding: 20px;
 
    .close-btn {
      background-color: #1B2134;
      width: 30px;
      height: 30px;
      border-radius: 4px;
    }
 
    .title {
      font-weight: 600;
      font-size: 25px;
      line-height: 30px;
      color: $text_color;
      margin: 20px 0;
    }
 
    .content {
      flex-direction: column;
 
      .text1 {
        font-size: 15px;
        line-height: 26px;
        color: #fff;
      }
 
      .text2 {
        font-weight: 800;
        font-size: 22px;
        line-height: 27px;
        color: $text_color;
        margin: 12px 0;
      }
 
      .text3 {
        font-size: 15px;
        color: #98B9FA !important;
      }
 
      .sell-text {
        color: $red;
      }
 
      .buy-text {
        color: $light-blue;
      }
    }
  }
}
 
.mainBox {
  :deep(.vue-slider-dot-tooltip-inner) {
    background-color: $US_tabActice_background !important;
    color: #118DEC !important;
    padding: 0 10px !important;
  }
 
  :deep(.vue-slider-dot) {
    background: #417EF6 !important;
    color: #fafafa !important;
    width: 16px !important;
    height: 16px !important;
    border-radius: 50%;
  }
 
 
  :deep(.vue-slider-dot-tooltip-inner-top::after) {
    display: none;
  }
 
  :deep(.vue-slider-marks) {
    background: #232C46;
  }
 
  :deep(.custom-step) {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    /* 没有选中时候圈圈的颜色 */
    background: #232C46;
    border: 1px solid #232C46;
    margin-top: -6px;
    position: relative;
    left: -6px;
  }
}
 
.custom-step.active {
  // box-shadow: 0 0 0 3px $color_main;
  background-color: #417EF6;
  border: 1px solid #417EF6;
}
</style>