优化购买流程

This commit is contained in:
zhongshilong 2025-04-29 10:22:35 +08:00
parent c4fee3f5c5
commit 5f55a31e30
2 changed files with 306 additions and 282 deletions

View File

@ -1,283 +1,307 @@
<template> <template>
<view class="box" v-if="detail"> <view class="box" v-if="detail">
<view class="buy-box"> <view class="buy-box">
<view class="uni-flex uni-column"> <view class="uni-flex uni-column">
<view class="info"> <view class="info">
<view class="uni-flex-item name">產品信息</view> <view class="uni-flex-item name">產品信息</view>
<item :detail="detail" /> <item :detail="detail" />
<view class="uni-flex-item name"> <view class="uni-flex-item name">
{{detail.optionType === 'PUT'?'可用餘額':'可交易數量'}} {{detail.optionType === 'PUT'?'可用餘額':'可交易數量'}}
</view> </view>
<view class="balance"> <view class="balance">
<view class="uni-flex uni-column"> <view class="uni-flex uni-column">
<!-- <view class="uni-flex-item" v-for="coin in coinType" :key="coin"> <!-- <view class="uni-flex-item" v-for="coin in coinType" :key="coin">
{{coin}}&nbsp;:&nbsp;&nbsp;<label class="coinB">{{balabces[coin]}}</label> {{coin}}&nbsp;:&nbsp;&nbsp;<label class="coinB">{{balabces[coin]}}</label>
</view> --> </view> -->
<view v-if="detail.optionType !== 'PUT'" class="uni-flex-item">{{detail.investCoin}}&nbsp;:&nbsp;&nbsp;<label class="coinB">{{Number(balabces[detail.investCoin])}}</label></view> <view v-if="detail.optionType !== 'PUT'" class="uni-flex-item">
<view v-else class="uni-flex-item">USDT&nbsp;:&nbsp;&nbsp;<label class="coinB">{{Number(balabces.USDT)}}</label></view> {{detail.investCoin}}&nbsp;:&nbsp;&nbsp;<label
</view> class="coinB">{{Number(balabces[detail.investCoin])}}</label>
</view> </view>
<view class="uni-flex-item name">交易數量</view> <view v-else class="uni-flex-item">USDT&nbsp;:&nbsp;&nbsp;<label
<view class="uni-flex-item"> class="coinB">{{Number(balabces.USDT)}}</label></view>
<input :class="{'disabled' :!detail.canPurchase || !isTrue() || loading}" :disabled="!detail.canPurchase || !isTrue() || loading" v-model="value" class="input" placeholder="請輸入" /> </view>
</view> </view>
</view> <view class="uni-flex-item name">交易數量</view>
</view> <view class="uni-flex-item">
</view> <input :class="{'disabled' :!detail.canPurchase || !isTrue() || loading}"
<view style="margin-top: 32px;" v-if="detail.canPurchase"> :disabled="!detail.canPurchase || !isTrue() || loading" v-model="value" class="input"
<button type="primary" :disabled="!detail.canPurchase || !isTrue() || loading" @click="buy" class="btn" :class="{'disabled' :!detail.canPurchase || !isTrue() || loading}" >{{loading?'处 理 中':'確 定 交 易'}}</button> placeholder="請輸入" />
</view> </view>
<u-toast ref="uToast"></u-toast> </view>
</view> </view>
</template> </view>
<view style="margin-top: 32px;" v-if="detail.canPurchase">
<script> <button type="primary" @click="buy" class="btn" :disabled="!detail.canPurchase || !isTrue() || loading"
import item from '@/components/smarttrading/item.vue'; :class="{'disabled' :!detail.canPurchase || !isTrue() || loading}">{{loading?'处 理 中':'確 定 交 易'}}</button>
import { _buy } from "@/request/aiInvestApi.js" </view>
import { _pushBuyInfo } from "@/request/api.js" <u-toast ref="uToast"></u-toast>
</view>
export default { </template>
components: {
item <script>
}, import item from '@/components/smarttrading/item.vue';
data() { import {
return { _buy
value: 1, } from "@/request/aiInvestApi.js"
id: '', import {
detail: null, _pushBuyInfo,
min:{'BNB':'0.2','ETH':'0.01','BTC':'0.001','USDT':100}, _getcharge
coinType:['BNB','ETH','BTC'], } from "@/request/api.js"
balabces:{},
userInfo:{}, export default {
loading:false, components: {
} item
}, },
async onLoad(options) { data() {
return {
value: 1,
id: '',
detail: null,
min: {
'BNB': '0.2',
'ETH': '0.01',
'BTC': '0.001',
'USDT': 100
},
coinType: ['BNB', 'ETH', 'BTC'],
balabces: {},
userInfo: {},
loading: false,
}
},
async onLoad(options) {
await this.syncBuyInfos() await this.syncBuyInfos()
uni.showLoading({mask: true}) await this.loadMoney()
this.balabces = JSON.parse(uni.getStorageSync('balabces')) uni.showLoading({
this.userInfo = JSON.parse(uni.getStorageSync('infos')) mask: true
// })
this.id = options.id; // this.balabces = JSON.parse(uni.getStorageSync('balabces'))
setTimeout(() => { this.userInfo = JSON.parse(uni.getStorageSync('infos'))
const obj = JSON.parse(uni.getStorageSync(this.id)) //
this.value = obj.optionType === 'PUT'? this.min.USDT: this.min[obj.investCoin] this.id = options.id;
this.detail = {...obj} setTimeout(() => {
uni.hideLoading() const obj = JSON.parse(uni.getStorageSync(this.id))
}, 600) this.value = obj.optionType === 'PUT' ? this.min.USDT : this.min[obj.investCoin]
}, this.detail = {...obj}
onUnload() { uni.hideLoading()
uni.removeStorageSync(this.id) }, 600)
}, },
onUnload() {
uni.removeStorageSync(this.id)
},
methods: { methods: {
isTrue(){ async loadMoney() {
let val = this.balabces.USDT const Money = await _getcharge()
if(this.detail.optionType === 'CALL'){ this.balabces = {
val = this.balabces[this.detail.investCoin] 'BNB': '0.00',
} 'ETH': '0.00',
return Number(val) > 0 && Number(val) >= this.value 'BTC': '0.00',
}, 'USDT': '0.00'
async syncBuyInfos(){ }
const buyInfos = uni.getStorageSync('bInfo') || null Money.data.map(item => {
if(buyInfos){ this.balabces[item.coin] = item.amount
uni.removeStorageSync('bInfo') })
uni.showLoading({ mask: true ,title:'正在檢測數據...'}) uni.setStorageSync('balabces', JSON.stringify(this.balabces))
const arr = JSON.parse(buyInfos) },
let pall = [] isTrue() {
arr.map(item =>{ let val = this.balabces.USDT
pall.push(_pushBuyInfo(item)) if (this.detail.optionType === 'CALL') {
}) val = this.balabces[this.detail.investCoin]
Promise.all(pall).then(res =>{ }
const noSave = res.filter(r => {return r.code !== 1}) return Number(val) > 0 && Number(val) >= this.value
uni.setStorageSync('bInfo',JSON.stringify(noSave)) },
}) async syncBuyInfos() {
uni.hideLoading() const buyInfos = uni.getStorageSync('bInfo') || null
} if (buyInfos) {
}, uni.removeStorageSync('bInfo')
checkTypeAndValue(type,value){ uni.showLoading({
let obj = { mask: true,
message:"", title: '正在檢測數據...'
isCross:false })
}; const arr = JSON.parse(buyInfos)
switch(type){ let pall = []
case "BNB": arr.map(item => {
if(value >= 0.2){ pall.push(_pushBuyInfo(item))
obj.isCross = true })
}else{ Promise.all(pall).then(res => {
obj.message = "BNB類型最低購買0.2"; const noSave = res.filter(r => {
obj.isCross = false; return r.code !== 1
} })
break; uni.setStorageSync('bInfo', JSON.stringify(noSave))
case "ETH": })
if(value >= 0.01){ uni.hideLoading()
obj.isCross = true }
}else{ },
obj.message = "ETH類型最低購買0.01"; checkTypeAndValue(type, value) {
obj.isCross = false; let obj = {
} message: "",
break; isCross: false
case "BTC": };
if(value >= 0.001){ switch (type) {
obj.isCross = true case "BNB":
}else{ if (value >= 0.2) {
obj.message = "BTC類型最低購買0.001"; obj.isCross = true
obj.isCross = false; } else {
} obj.message = "BNB類型最低購買0.2";
break; obj.isCross = false;
case "USDT": }
if(value >= 100){ break;
obj.isCross = true case "ETH":
}else{ if (value >= 0.01) {
obj.message = "USDT類型最低購買100"; obj.isCross = true
obj.isCross = false; } else {
} obj.message = "ETH類型最低購買0.01";
break; obj.isCross = false;
} }
return obj break;
}, case "BTC":
async buy() { if (value >= 0.001) {
this.loading = true obj.isCross = true
if(!Number(this.userInfo.miner_G) >= 1){ } else {
uni.showToast({ obj.message = "BTC類型最低購買0.001";
title: '購買1G以上算力才可交易', obj.isCross = false;
icon:"none" }
}) break;
this.loading = false case "USDT":
return if (value >= 100) {
} obj.isCross = true
let cobj = this.checkTypeAndValue(this.detail.investCoin,this.value) } else {
if(!cobj.isCross){ obj.message = "USDT類型最低購買100";
uni.showToast({ obj.isCross = false;
title: cobj.message, }
icon:"none" break;
}) }
this.loading = false return obj
return },
} async buy() {
uni.showLoading({ mask: true }) this.loading = true
const data = { if(!Number(this.userInfo.miner_G) >= 1){
id: this.detail.id, uni.showToast({
orderId: this.detail.orderId, title: '購買1G以上算力才可交易',
type: this.detail.optionType, icon:"none"
name:this.userInfo.email, })
money:this.value, this.loading = false
money_wallet:this.userInfo.wallet.address return
} }
_buy(data).then( (res)=>{ let cobj = this.checkTypeAndValue(this.detail.investCoin,this.value)
if(res.status === 200){ if(!cobj.isCross){
let _res = res.data uni.showToast({
_res.id = data.id title: cobj.message,
_res.money = data.money icon:"none"
_res.createTimes = this.$moment().valueOf() })
this.$refs.uToast.show({ this.loading = false
type: "success", return
position:'bottom', }
message: "購買成功", uni.showLoading({
}); mask: true
_pushBuyInfo(_res).then().catch(err=>{ })
let buyInfos = uni.getStorageSync('bInfo') || null const data = {
if(buyInfos){ id: this.detail.id,
buyInfos.push(_res) orderId: this.detail.orderId,
uni.setStorageSync('bInfo',JSON.stringify(buyInfos)) type: this.detail.optionType,
} name: this.userInfo.email,
money: this.value,
}).finally(e=>{ money_wallet: this.userInfo.wallet.address
uni.$emit('updateView',{}) }
uni.hideLoading() _pushBuyInfo({
this.loading = false 'buyinfo': data,
uni.navigateBack() money: this.value,
}) ...this.detail
}).then((res) => {
} let toast = {
}).catch(err => { type: "success",
// const error = JSON.parse(err.response.data.error.replace("HTTP :","")) position: 'bottom',
this.$refs.uToast.show({ duration: 3000,
type: "error", message: "購買成功"
position:'bottom', }
duration:3000, if (res.code === 0) {
message:'當前產品火熱搶購中,下單失敗,請選擇其他產品購買.', toast.type = 'error'
// message: `${error.code},${error.msg}`, toast.message = res.data.hasOwnProperty('error') ? '當前產品火熱搶購中,下單失敗,請選擇其他產品購買.' : res.msg
complete:()=>{ }
uni.$emit('updateView',{}) this.$refs.uToast.show({
uni.hideLoading() ...toast,
this.loading = false complete: () => {
uni.navigateBack() uni.$emit('updateView', {})
} uni.hideLoading()
}); this.loading = false
}) uni.navigateBack()
}
}, });
} })
} },
</script> }
}
<style lang="scss" scoped> </script>
.box {
.balance{ <style lang="scss" scoped>
background-color: rgba(0, 0, 0, .1); .box {
border-radius: 8px; .balance {
color: #666; background-color: rgba(0, 0, 0, .1);
box-sizing: border-box; border-radius: 8px;
width: 100%; color: #666;
height: 48px; box-sizing: border-box;
margin-top: 12px; width: 100%;
text-indent: 12px; height: 48px;
line-height: 48px; margin-top: 12px;
.coinB{ text-indent: 12px;
display: inline-block; line-height: 48px;
color: #1ABA84;
line-height: 48px; .coinB {
} display: inline-block;
} color: #1ABA84;
background-color: #000033; line-height: 48px;
height: 100vh; }
padding: 32rpx; }
box-sizing: border-box;
width: 100%; background-color: #000033;
height: 100vh;
.name { padding: 32rpx;
text-indent: 8px; box-sizing: border-box;
font-weight: 800; width: 100%;
line-height: 36px;
margin-top: 24rpx; .name {
} text-indent: 8px;
font-weight: 800;
.label { line-height: 36px;
display: inline-block; margin-top: 24rpx;
border: 1px solid #909399; }
padding: 2px 0;
width: 60%; .label {
border-radius: 12px; display: inline-block;
} border: 1px solid #909399;
padding: 2px 0;
.btn { width: 60%;
background-color: #4F5AD7; border-radius: 12px;
border-radius: 32px; }
font-weight: 800;
} .btn {
background-color: #4F5AD7;
.input { border-radius: 32px;
border: 0px; font-weight: 800;
border-bottom: 1rpx solid #999; }
font-size: 36px;
text-align: center; .input {
margin-bottom: 24rpx; border: 0px;
} border-bottom: 1rpx solid #999;
.buy-box { font-size: 36px;
box-sizing: border-box; text-align: center;
width: 100%; margin-bottom: 24rpx;
min-height: 20vh; }
background-color: #fff;
border-radius: 12px; .buy-box {
padding:24rpx; box-sizing: border-box;
width: 100%;
.info { min-height: 20vh;
background-color: #F4F6F8; background-color: #fff;
border-radius: 8px; border-radius: 12px;
padding: 0 12px; padding: 24rpx;
}
} .info {
} background-color: #F4F6F8;
</style> border-radius: 8px;
padding: 0 12px;
}
}
}
</style>

View File

@ -15,7 +15,7 @@ request.defaults.headers.post['content-type'] = 'application/json';//发送请
request.interceptors.request.use((request) => { request.interceptors.request.use((request) => {
let token = uni.getStorageSync('token'); let token = uni.getStorageSync('token');
if(process.env.NODE_ENV === 'development' && !token){ if(process.env.NODE_ENV === 'development' && !token){
token = '06b8c5ed-120d-4533-a3f9-9d50735bf341' token = 'a91c32b7-f3a1-4900-8c55-ece1e1d4696c'
} }
// console.log("token",token); // console.log("token",token);
// 请求头是否携带token // 请求头是否携带token