From 802fba84f2a31820a305db3c449f82044f7fc324 Mon Sep 17 00:00:00 2001
From: lxf <1371462558@qq.com>
Date: Sun, 15 Jun 2025 14:36:28 +0800
Subject: [PATCH] 金额计算

---
 src/components/constract/PerpetualContract/orderCom/amountSlider.vue |   65 ++++++++++++++++-----
 src/utils/http.js                                                    |    4 
 src/components/constract/PerpetualContract/createOrder.vue           |  106 +++++++++++++++++++++-------------
 3 files changed, 117 insertions(+), 58 deletions(-)

diff --git a/src/components/constract/PerpetualContract/createOrder.vue b/src/components/constract/PerpetualContract/createOrder.vue
index c243946..8880f65 100644
--- a/src/components/constract/PerpetualContract/createOrder.vue
+++ b/src/components/constract/PerpetualContract/createOrder.vue
@@ -104,6 +104,7 @@
             :symbol="newPageData.symbol"
             :lever_rate="current_lever_rate_num"
             :typeNum="isActive"
+            :initOpen="initOpen"
             @getAmount="getAmount"
           ></amount-slider>
           <!-- 操作按钮-->
@@ -152,7 +153,7 @@
             <!-- 合约金额=保证金-->
             <div class="submit-info-item">
               <div>{{ $t("message.home.heyuejine") }}:</div>
-              <div>{{ getMargin }} {{ unit }}</div>
+              <div>{{ getContractAmount }} {{ unit }}</div>
             </div>
             <!-- 保证金 -->
             <div class="submit-info-item">
@@ -195,6 +196,7 @@
       ref="openDialog"
       :type="activeName"
       :sellOrBuy="directionName"
+      this.maxAmount
       :info="getSubmitInfo('open')"
     ></open-dialog>
   </div>
@@ -245,7 +247,7 @@
       lever_rate: ["1.00X"], // 后端返回的杠杆 ['1.00X','2.00X',...]
       origin_lever: [], // 支持的杠杆 [1,2,3]
       current_lever_rate: "", //当前选中
-      current_lever_rate_num: 1, //当前选中的杠杆数字 1
+      current_lever_rate_num: 200, //当前选中的杠杆数字 1
 
       initClose: {},
       initOpen: {},
@@ -286,7 +288,9 @@
       }, 2000); //延迟调用
     });
     bus.on("moneyContract", (val) => {
-      this.availableMoney = val;
+      if (val) {
+        this.availableMoney = val;
+      }
     }); //合约可用余额
   },
   //销毁定时器
@@ -319,24 +323,25 @@
       let data = "0.00";
       if (amount != undefined && this.lever_rate != undefined) {
         if (this.lever_rate?.length > 0) {
-          data = this.sessionObj?.amount * amount * this.current_lever_rate_num;
+          data = amount * this.initOpen.amount;
         } else if (this.lever_rate.length == 0) {
           data = this.sessionObj?.amount * amount * 1;
         }
       }
-      return bigDecimal.round(data, 6);
+      return bigDecimal.round(data, 2);
     },
     // 获取保证金,开仓才需要
     getMargin: function () {
+      // if (this.availableMoney > 0) return;
       const amount = this.inputAmount;
       let data = "0.000000";
       console.log(
         "getMargin",
-        this.isActive,
         amount,
-        this.lever_rate,
-        this.sessionObj.amount,
-        this.current_lever_rate_num
+        parseInt(amount / this.current_lever_rate_num),
+        this.availableMoney, //
+        this.sessionObj.amount, //
+        this.current_lever_rate_num // 倍数
       );
       if (
         this.isActive === 0 &&
@@ -345,24 +350,26 @@
         this.sessionObj.amount
       ) {
         if (this.lever_rate.length > 0) {
-          data = this.sessionObj.amount * amount * this.current_lever_rate_num;
+          data =
+            parseInt(amount * this.initOpen.amount) /
+            this.current_lever_rate_num;
         } else if (this.lever_rate.length == 0) {
           data = this.sessionObj.amount * amount * 1;
         }
       }
       // 校验保证金不能大于余额
-      if (
-        Number(this.availableMoney) > 0 &&
-        Number(data) > Number(this.availableMoney)
-      ) {
-        // 自动限制为最大可用余额
-        data = this.availableMoney;
-        // 可选:提示
-        this.$nextTick(() => {
-          ElMessage.warning(this.$t("message.home.baozhengjinbuzu")); // 保证金不足
-        });
-      }
-      return bigDecimal.round(data, 6);
+      // if (
+      //   Number(this.availableMoney) > 0 &&
+      //   Number(data) > Number(this.availableMoney)
+      // ) {
+      // 自动限制为最大可用余额
+      // data = this.availableMoney;
+      // 可选:提示
+      // this.$nextTick(() => {
+      //   ElMessage.warning(this.$t("message.home.baozhengjinbuzu")); // 保证金不足
+      // });
+      // }
+      return bigDecimal.round(data, 2);
     },
     // 获取手续费
     getFee: function () {
@@ -374,6 +381,8 @@
       // }
       // return bigDecimal.round(data);
       const amount = this.inputAmount;
+      let data = 0;
+      // if(amount) {return bigDecimal.round(data, 2);};
       let rate = 0;
       // 根据当前杠杆设置费率
       switch (Number(this.current_lever_rate_num)) {
@@ -392,9 +401,11 @@
         default:
           rate = 0;
       }
-      let data = 0;
       if (amount && this.isActive == 0) {
-        data = amount * rate * 100; // 乘以100显示百分比
+        data =
+          (parseInt(amount * this.initOpen.amount) /
+            this.current_lever_rate_num) *
+          rate; // 乘以100显示百分比
       }
       return bigDecimal.round(data, 2);
     },
@@ -433,6 +444,7 @@
     },
     //获取张数
     getAmount(val) {
+      console.log("val: ", val);
       this.inputAmount = val;
     },
     // 切换限价单和市价单
@@ -454,27 +466,39 @@
     // 获取张数,数据转换
     getVolumnByLever() {
       let amount = this.initOpen.volume;
-      console.log("amount: ", amount);
-      if (this.isActive == 1) {
-        amount = Math.max(this.closeSellAmount, this.closeBuyAmount);
-      }
+      // console.log("amount: ", amount);
+      // if (this.isActive == 1) {
+      //   amount = Math.max(this.closeSellAmount, this.closeBuyAmount);
+      // }
       if (!amount) {
         return 0;
       } else {
-        if (this.lever_rate.length > 0) {
-          return parseInt(amount / this.current_lever_rate_num);
-          // const contractSize = 1;
-          // const price = this.price || this.newPageData.close || 1;
-          // const availableMargin = amount; // 可用保证金
-          // const lever = this.current_lever_rate_num;
+        // if (this.lever_rate.length > 0) {
+        const price = this.initOpen.amount;
+        // console.log(
+        //   "price: ",
+        //   price,
+        //   this.current_lever_rate_num,
+        //   this.availableMoney,
+        //   (this.availableMoney * this.current_lever_rate_num) / price
+        // );
 
-          // const maxVolume = parseInt(
-          //   (availableMargin * lever) / (contractSize * price)
-          // );
-          // return maxVolume > 0 ? maxVolume : 0;
-        } else if (this.lever_rate.length == 0) {
-          return parseInt(amount / 1);
-        }
+        // 最大可开张数
+        return parseInt(
+          (this.availableMoney * this.current_lever_rate_num) / price
+        );
+        // }
+
+        // const contractSize = 1;
+        // const availableMargin = amount; // 可用保证金
+        // const lever = this.current_lever_rate_num;
+        // const maxVolume = parseInt(
+        //   (availableMargin * lever) / (contractSize * price)
+        // );
+        // return maxVolume > 0 ? maxVolume : 0;
+        // } else if (this.lever_rate.length == 0) {
+        //   return parseInt(amount / 1);
+        // }
       }
     },
 
diff --git a/src/components/constract/PerpetualContract/orderCom/amountSlider.vue b/src/components/constract/PerpetualContract/orderCom/amountSlider.vue
index f4c0b21..46a622a 100644
--- a/src/components/constract/PerpetualContract/orderCom/amountSlider.vue
+++ b/src/components/constract/PerpetualContract/orderCom/amountSlider.vue
@@ -87,6 +87,10 @@
       type: Number,
       default: 0,
     },
+    initOpen: {
+      type: Object,
+      default: {},
+    },
   },
   data() {
     return {
@@ -115,6 +119,12 @@
       }, 2000);
     }
   },
+  unmounted() {
+    if (this.timer) {
+      clearInterval(this.timer);
+      this.timer = null;
+    }
+  },
   computed: {
     ...mapState(useUserStore, ["existToken"]),
     //保证金余额
@@ -130,10 +140,10 @@
         const { total, money_wallet, money_contract, money_contract_profit } =
           res.data;
         this.totalAsset = total; //总资产
-        this.walletMoney = bigDecimal.round(money_wallet, 6); //钱包余额
-        this.profitLoss = bigDecimal.round(money_contract_profit, 6); //永续合约总未实现盈亏
-        bus.emit("moneyContract", money_contract); //合约可用余额
-        // bus.emit("moneyContract", this.walletMoney); //合约可用余额
+        this.walletMoney = bigDecimal.round(money_wallet, 2); //钱包余额
+        this.profitLoss = bigDecimal.round(money_contract_profit, 2); //永续合约总未实现盈亏
+        // bus.emit("moneyContract", money_contract); //合约可用余额
+        bus.emit("moneyContract", money_wallet); //合约可用余额
       });
     },
     //输入框事件
@@ -148,18 +158,43 @@
     sliderAmountChange(val) {
       let data;
       if (this.maxAmount) {
-        if (val == 0) {
-          this.amount = undefined;
-        } else {
-          const rate = val / 100; //如0.25
-          data = this.maxAmount * rate;
-          // if (this.lever_rate) {
-          //   data = math.format((this.maxAmount * rate), 2);
-          // } else {
-          //   data = math.format((this.maxAmount * rate) / 1, 2);
-          // }
-          this.amount = parseInt(data);
+        // if (val == 0) {
+        //   this.amount = undefined;
+        // } else {
+
+        const rate = val / 100; //如0.25
+        let sxf = 0;
+        switch (Number(this.lever_rate)) {
+          case 25:
+            sxf = 0.0375;
+            break;
+          case 50:
+            sxf = 0.075;
+            break;
+          case 100:
+            sxf = 0.15;
+            break;
+          case 200:
+            sxf = 0.3;
+            break;
+          default:
+            sxf = 0;
         }
+        const fl =
+          this.walletMoney * sxf * this.initOpen.amount * this.lever_rate * rate;
+        const maxAmount =
+          this.walletMoney * this.initOpen.amount * this.lever_rate;
+        console.log(maxAmount * rate, fl);
+
+        data = maxAmount * rate - fl;
+
+        // if (this.lever_rate) {
+        //   data = math.format((this.maxAmount * rate), 2);
+        // } else {
+        //   data = math.format((this.maxAmount * rate) / 1, 2);
+        // }
+        this.amount = parseInt(data);
+        // }
         this.$emit("getAmount", this.amount);
       }
     },
diff --git a/src/utils/http.js b/src/utils/http.js
index 8f1574e..5c63ab1 100644
--- a/src/utils/http.js
+++ b/src/utils/http.js
@@ -19,8 +19,8 @@
 var lang = JSON.parse(localStorage.getItem("lang"));
 axios.defaults.timeout = 15000;
 // axios.defaults.baseURL = URL.BASE_URL;
-// axios.defaults.baseURL = 'https://coin.usadeepcoin.com/';
-axios.defaults.baseURL = 'https://btc.btcwapo.com/';
+axios.defaults.baseURL = 'https://coin.usadeepcoin.com/';
+// axios.defaults.baseURL = 'https://btc.btcwapo.com/';
 
 axios.interceptors.request.use(
   (config) => {

--
Gitblit v1.9.3