zj
2024-06-03 561ca040085b6fd6766e471a70b4a3c682deadc2
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
<template>
  <div class="c2cBuy w-full h-full">
    <c2c-trade :title="$t('购买') + ' ' + symbol" :detail="detail" @refresh="fetchDetail" @payType="payType = $event"
      v-if="detail.id">
      <template #trade>
        <div class="buy-item mt-40 w-full mainBackground c2cColor">
          <div class="buy-item-title flex justify-between font-28 py-34 border-b-1 border-light-grey c2cColor">
            <div class="flex-1 text-center" :class="{ 'text-grey': type === 'num' }" @click="typeSwitch('amount')">
              {{ $t('按金额购买') }}
            </div>
            <div class="flex-1 text-center" :class="{ 'text-grey': type === 'amount' }" @click="typeSwitch('num')">
              {{ $t('按数量购买') }}
            </div>
          </div>
          <div class="px-32 mt-40">
            <div class="w-full buy-item-input relative box-border" v-show="type === 'amount'">
              <span class="font-28 absolute left-28 text font-700 c2cColor">{{ currencySymbol }}</span>
              <input class="w-full font-36 h-110 border-none box-border pl-96 c2cColor" type="number"
                :placeholder="$t('请输入金额')" v-model="money" @input="changeVal">
              <span class="font-28 absolute right-19 text c2cColor" @click="all">{{ $t('全部') }}</span>
            </div>
            <div class="w-full buy-item-input relative box-border" v-show="type === 'num'">
              <input class="w-full font-36 h-110 border-none box-border pl-30 c2cColor" type="number"
                :placeholder="$t('请输入数量')" v-model="quantity" @input="changeValNum">
              <span class="font-28 absolute right-19 text">
                <span class="mr-16" style="color:#B8BCC5;">{{ symbol }}</span>
                <span class="c2cColor" @click="all">{{ $t('全部') }}</span>
              </span>
            </div>
            <div v-if="tips" class="flex mt-12 text-red font-28">
              {{ tips }}
            </div>
            <div class="flex justify-between mt-40 mb-12 c2cColor">
              <span class="text-grey">{{ $t('数量') }}</span>
              <span>{{ totalQuantity }} {{ symbol }}</span>
            </div>
            <div class="flex justify-between c2cColor">
              <span class="text-grey">{{ $t('总额') }}</span>
              <span>{{ totalMoney }} {{ currencySymbol }}</span>
            </div>
            <div class="w-full mt-24 pb-30">
              <van-button @click.native="buyClick" class="w-full font-30 buy-button" :disabled="loading" type="primary">
                <span v-if="!loading">{{ $t('购买') }}&nbsp;{{ symbol }}</span>
                <van-loading v-else type="spinner" />
              </van-button>
            </div>
          </div>
        </div>
      </template>
      <template #desc>
        <div class="mt-36">
          <h2 class="font-28 font-400 c2cColor">{{ $t('交易信息') }}</h2>
          <div class="mt-30 font-26 text-grey">
            <p class="lh-45">{{ $t('请先阅读以下内容:') }}</p>
            <p class="lh-45">{{ $t('银行卡转账切勿备注,不然不给予放币和直接封其账号。付款后 需要提供打款后新的交易明细图(如果P图或者隐藏交易明细上报平台冻结账户)') }}</p>
          </div>
        </div>
      </template>
    </c2c-trade>
  </div>
</template>
 
<script>
import { formatTime } from "@/utils/utis";
import { Button, Cell, DropdownItem, DropdownMenu, Field, Icon, Popup, Switch } from "vant";
// import { SET_COUNT, SET_CREATE_ORDER_TIME, SET_ORDER_NUMBER, SET_TOTAL_PRICE, } from "@/store/const.store";
import C2cTrade from "@/page/c2c-trade/components/C2cTrade";
import otcApi from "@/API/otc";
import { mapGetters } from "vuex";
export default {
  name: "c2cBuy",
  data() {
    return {
      type: 'amount', // 按金额/数量购买
      detail: {},
      payType: '',
      tips: '',
      money: '',
      quantity: '',
      totalMoney: '-',
      totalQuantity: '-',
      loading: false
    }
  },
  created() {
    this.fetchDetail()
  },
  methods: {
    fetchDetail() { // 获取详情
      const id = this.$store.state.c2c.adv_id
      otcApi.ctcAdvertGetDetail({ id, language: this.$i18n.locale }).then(res => {
        if (res.data.symbol_value == this.detail.symbol_value) {
          this.$toast(this.$t('无价格更新'))
        }
        this.detail = res.data
      })
    },
    async submitOrder() { // 发起订单
      this.loading = true
      const data = await otcApi.getSessionToken({ currency: this.exchangeCurrency })
      const params = {
        session_token: data.data.session_token, // session_token
        c2c_advert_id: this.detail.id,
        payment_method_id: this.payType,
        direction: 'buy',
        order_type: this.type === 'amount' ? 'by_amount' : 'by_num', // 'by_num'
        amount: this.type === 'amount' ? this.totalMoney / 1 : '', // 金额
        coin_amount: this.type === 'amount' ? '' : this.totalQuantity / 1 // 数量
      }
      otcApi.ctcOrderOpen(params).then(res => {
        this.loading = false
        this.$store.commit('c2c/SET_ORDER_NO', res.data.order_no)
        this.$router.push('/orderGeneration')
      }).catch(err => {
        this.loading = false
      })
    },
    // 切换类型
    typeSwitch(type) {
      this.type = type;
      this.tips = ''
      this.money = ''
      this.quantity = ''
      this.totalMoney = '-'
      this.totalQuantity = '-'
    },
    changeVal(e) {
      // e.target.value = e.target.value.replace('-', ''); 不能输入小数了
      //this[this.type] = e.target.value;
      if (this.money == '') {
        this.tips = ''
        this.totalMoney = '-'
        this.totalQuantity = '-'
      } else {
        if (this.money * 1 < this.detail.investment_min * 1) {
          this.tips = this.$t('最小金额') + this.detail.investment_min + ' ' + this.exchangeCurrency
          this.totalMoney = '-'
          this.totalQuantity = '-'
        } else if (this.money * 1 > this.detail.investment_max * 1) {
          this.tips = this.$t('最大金额') + this.detail.investment_max + ' ' + this.exchangeCurrency
          this.totalMoney = '-'
          this.totalQuantity = '-'
        } else {
          this.tips = ''
          this.totalMoney = this.money
          if (this.symbol !== "USDT") {
            this.totalQuantity = Math.floor((this.totalMoney / this.detail.symbol_value) * 1000000) / 1000000
          } else {
            this.totalQuantity = Math.floor((this.totalMoney / this.detail.symbol_value) * 100) / 100;
          }
        }
      }
    },
    changeValNum() {
      if (this.quantity == '') {
        this.tips = ''
        this.totalMoney = '-'
        this.totalQuantity = '-'
      } else {
        let minNum = Math.floor((this.detail.investment_min / this.detail.symbol_value) * 1000000) / 1000000;
        let maxNum = Math.floor((this.detail.investment_max / this.detail.symbol_value) * 1000000) / 1000000;
        if (this.quantity * 1 < minNum * 1) {
          this.tips = this.$t('最小数量') + minNum + ' '
          this.totalMoney = '-'
          this.totalQuantity = '-'
        } else if (this.quantity * 1 > maxNum * 1) {
          this.tips = this.$t('最大数量') + maxNum + ' '
          this.totalMoney = '-'
          this.totalQuantity = '-'
        } else {
          this.tips = ''
          this.totalQuantity = this.quantity
          if (this.symbol !== "USDT") {
            this.totalMoney = (this.quantity * this.detail.symbol_value).toFixed(6)
          } else {
            this.totalMoney = (this.quantity * this.detail.symbol_value).toFixed(2)
          }
        }
      }
    },
    // 购买
    async buyClick() {
      if (this.type == 'amount') {
        if (this.money == '') {
          this.$toast(this.$t('请输入金额'))
        } else {
          this.submitOrder()
        }
      } else {
        console.log(this.quantity)
        if (this.quantity == '') {
          this.$toast(this.$t('请输入数量'))
        } else {
          this.submitOrder()
        }
      }
    },
    all() {
      this.tips = ''
      this.money = this.detail.investment_max
      this.totalMoney = this.detail.investment_max
      if (this.symbol !== "USDT") {
        this.quantity = Math.floor((this.detail.investment_max / this.detail.symbol_value) * 1000000) / 1000000
        this.totalQuantity = Math.floor((this.detail.investment_max / this.detail.symbol_value) * 1000000) / 1000000
      } else {
        this.quantity = Math.floor((this.detail.investment_max / this.detail.symbol_value) * 100) / 100
        this.totalQuantity = Math.floor((this.detail.investment_max / this.detail.symbol_value) * 100) / 100
      }
    }
  },
  computed: {
    ...mapGetters('c2c', ['symbol', "currencySymbol", "exchangeCurrency"]),
  },
  components: {
    [Icon.name]: Icon,
    [Field.name]: Field,
    [Popup.name]: Popup,
    [Cell.name]: Cell,
    [Switch.name]: Switch,
    [DropdownMenu.name]: DropdownMenu,
    [DropdownItem.name]: DropdownItem,
    [Button.name]: Button,
    C2cTrade,
  },
}
</script>
 
<style lang="scss" scoped>
@import "~@/assets/mixin.scss";
 
.c2cBuy {
  ::v-deep .van-button {
    border-radius: 10px;
    background: #2EBD85;
  }
}
 
 
.buy-item {
 
  border-radius: 25px;
  box-shadow: 0 0 8px rgba(0, 0, 0, .2);
}
 
.border-ra {
  border-radius: 10px;
}
 
.buy-item-title {
  position: relative;
 
  &:after {
    @include border-1px('bottom');
  }
}
 
.buy-item-input {
  input {
    border-radius: 10px;
 
    @include themify() {
      background: themed("input_background");
    }
  }
 
  .text {
    top: 50%;
    transform: translateY(-50%);
  }
}
 
 
 
 
.tips {
  border-radius: 8px;
}
</style>