瀏覽代碼

修复计算方法

HHE-5476 6 天之前
父節點
當前提交
fc6c5008a6
共有 3 個文件被更改,包括 44 次插入51 次删除
  1. 11 7
      components/payment/WechatPayment.vue
  2. 4 4
      config/app.js
  3. 29 40
      utils/util.js

+ 11 - 7
components/payment/WechatPayment.vue

@@ -11,7 +11,6 @@
 <script setup>
 import { ref, getCurrentInstance } from "vue";
 import { createPaymentOrder, queryPaymentStatus } from "@/api/payment";
-import { APP_CODE } from "@/config/app.js";
 import { generateCustomId } from "@/utils/util.js";
 
 // 组件实例
@@ -38,6 +37,7 @@ const callbacks = ref({
  * @param {string} options.description - 商品描述
  * @param {string} options.openId - 用户openId
  * @param {string} options.orderPrefix - 订单号前缀
+ * @param {string} options.orderNo - 订单号
  * @param {Function} options.onUniPayCreate - 订单创建成功回调
  * @param {Function} options.onUniPaySuccess - 支付成功回调
  * @param {Function} options.onUniPayCancel - 取消支付回调
@@ -59,21 +59,25 @@ const createUniPay = async (options) => {
           : () => {};
     });
 
-    // 生成订单号(使用公共组件)
-    orderNo.value = generateCustomId(options.orderPrefix || "WX");
+    if (options.orderNo) {
+      // 若传入订单号则使用传入的订单号
+      orderNo.value = options.orderNo;
+    } else {
+      // 生成订单号(使用公共组件)
+      orderNo.value = generateCustomId(options.orderPrefix || "WX_");
+    }
 
     // 调用后端创建支付订单
     const { data: payRes } = await createPaymentOrder({
       amount: options.amount,
       description: options.description,
       openId: options.openId,
-      orderNo: orderNo.value,
-      appCode: APP_CODE,
+      orderNo: options.orderNo,
     });
 
     // 订单创建成功,触发回调
     callbacks.value.onUniPayCreate({
-      out_trade_no: orderNo.value,
+      out_trade_no: options.orderNo,
       ...payRes,
     });
 
@@ -121,7 +125,7 @@ const confirmPaymentStatus = async () => {
         showPayLoading.value = false;
         paymentInProgress.value = false;
         callbacks.value.onUniPayFail(
-          new Error("支付结果查询超时,请稍后查看订单状态")
+          new Error("支付结果查询超时,请稍后查看订单状态"),
         );
         return;
       }

+ 4 - 4
config/app.js

@@ -1,17 +1,17 @@
 // let domain = "https://www.shuibeibyg.com/front-api"; // 正式环境IP
-// let domain = "https://test.shuibeibyg.com/front-api"; // 测试环境IP
+let domain = "https://test.shuibeibyg.com/front-api"; // 测试环境IP
 // let domain = 'http://192.168.100.199:8081' // 晋守桦IP
 // let domain = 'http://192.168.100.246:8081' // 韩朝龙IP
-let domain = "http://192.168.3.29:8081"; // 宏广IP
+// let domain = "http://192.168.3.29:8081"; // 宏广IP
 let share = "https://www.shuibeibyg.com";
 
 let imgUrl =
   "https://sb-admin.oss-cn-shenzhen.aliyuncs.com/shuibei-mini/new-mini/"; //服務器上图片地址
 
-export const H5_BASE_URL = "http://192.168.3.10:5174"; // 本地测试
+// export const H5_BASE_URL = "http://192.168.3.10:5174"; // 本地测试
 // export const H5_BASE_URL = "http://192.168.3.16:5173"; // 本地测试
 // export const H5_BASE_URL = "http://192.168.100.35:5175"; // 本地测试
-// export const H5_BASE_URL = "https://test.shuibeibyg.com/web-h5/"; // 测试环境H5地址
+export const H5_BASE_URL = "https://test.shuibeibyg.com/web-h5/"; // 测试环境H5地址
 // export const H5_BASE_URL = "https://www.shuibeibyg.com/web-h5/"; // 正式环境H5地址
 
 // PC 后台 API 地址(用于上传图片)

+ 29 - 40
utils/util.js

@@ -104,7 +104,7 @@ export default {
         const CONTENT_ROW_LENGTH = 20;
         let [contentLeng, contentArray, contentRows] = that.textByteLength(
           store_name,
-          CONTENT_ROW_LENGTH
+          CONTENT_ROW_LENGTH,
         );
         if (contentRows > 2) {
           contentRows = 2;
@@ -254,7 +254,7 @@ export default {
     people,
     count,
     num,
-    successFn
+    successFn,
   ) {
     let that = this;
     let rain = 2;
@@ -280,14 +280,14 @@ export default {
           110 * rain,
           230 * rain,
           30 * rain,
-          1
+          1,
         );
         context.drawImage(
           arrImages[2],
           68 * rain,
           194 * rain,
           160 * rain,
-          160 * rain
+          160 * rain,
         );
         context.save();
 
@@ -314,7 +314,7 @@ export default {
           94 * rain,
           75 * rain,
           75 * rain,
-          6 * rain
+          6 * rain,
         );
         context.clip();
         context.drawImage(
@@ -322,7 +322,7 @@ export default {
           27 * rain,
           94 * rain,
           75 * rain,
-          75 * rain
+          75 * rain,
         );
         context.draw(true, function () {
           uni.canvasToTempFilePath({
@@ -693,7 +693,7 @@ export default {
       let status = permision.isIOS
         ? await permision.requestIOS("location")
         : await permision.requestAndroid(
-            "android.permission.ACCESS_FINE_LOCATION"
+            "android.permission.ACCESS_FINE_LOCATION",
           );
 
       if (status === null || status === 1) {
@@ -910,7 +910,7 @@ export async function getSceneInfo(e, index) {
         merchantId: params.merchantId,
         userId: appStore.userInfo.userId,
       };
-	  appStore.setIndexRefersh(true)
+      appStore.setIndexRefersh(true);
       await footprintScan(obj);
       await appStore.USERINFO();
     }
@@ -1034,17 +1034,18 @@ export const Calc = {
    * @returns {Object} 解析结果
    */
   _parseNumStr(num) {
-    // 第一步:先校验并转换为有效数字(修复 NaN 问题)
     const validNum = this._validateNum(num);
-    const str = String(validNum).trim().replace(/^\+/, ""); // 移除正号
+    const str = String(validNum).trim().replace(/^\+/, "");
 
     const isNegative = str.startsWith("-");
     const absStr = isNegative ? str.slice(1) : str;
-    const [integerPart = "0", decimalPart = "0"] = absStr.split(".");
+    const parts = absStr.split(".");
+
+    const integerPart = parts[0] || "0";
+    const decimalPart = parts.length > 1 ? parts[1] : "";
 
-    // 处理纯小数(如.123)或纯整数(如123.)的情况
     const cleanInteger = integerPart || "0";
-    const cleanDecimal = decimalPart || "0";
+    const cleanDecimal = decimalPart;
 
     return {
       isNegative,
@@ -1083,7 +1084,7 @@ export const Calc = {
   },
 
   /**
-   * 加法运算(修复精度问题 + 入参校验)
+   * 加法运算
    */
   add(a, b) {
     // 链式调用/直接调用参数处理
@@ -1099,7 +1100,7 @@ export const Calc = {
   },
 
   /**
-   * 减法运算(修复精度问题 + 入参校验)
+   * 减法运算
    */
   sub(a, b) {
     const [arg1, arg2] =
@@ -1113,7 +1114,7 @@ export const Calc = {
   },
 
   /**
-   * 乘法运算(修复精度问题 + 入参校验)
+   * 乘法运算
    */
   mul(a, b) {
     const [arg1, arg2] =
@@ -1140,13 +1141,12 @@ export const Calc = {
   },
 
   /**
-   * 除法运算(修复精度问题 + 入参校验)
+   * 除法运算
    */
   div(a, b) {
     const [arg1, arg2] =
       this.currentValue === null ? [a, b] : [this.currentValue, a];
 
-    // 先校验除数是否为有效数字(避免 0/NaN 混淆)
     const validArg2 = this._validateNum(arg2);
     if (validArg2 === 0) {
       throw new Error("除数不能为0");
@@ -1155,26 +1155,15 @@ export const Calc = {
     const n1 = this._parseNumStr(arg1);
     const n2 = this._parseNumStr(arg2);
 
-    // 转换为整数(移除小数点)
     const n1Int =
       BigInt(n1.integerPart + n1.decimalPart) * (n1.isNegative ? -1n : 1n);
     const n2Int =
       BigInt(n2.integerPart + n2.decimalPart) * (n2.isNegative ? -1n : 1n);
 
-    // 计算缩放比例:10^(n2小数位数 - n1小数位数)
-    const decimalDiff = n2.decimalLength - n1.decimalLength;
-    const scale = BigInt(10 ** Math.abs(decimalDiff));
-    let divResult;
-
-    if (decimalDiff >= 0) {
-      divResult = (n1Int * scale) / n2Int;
-    } else {
-      divResult = n1Int / (n2Int * scale);
-    }
+    const scaleFactor = 10 ** (n2.decimalLength - n1.decimalLength);
+    const divResult = (Number(n1Int) / Number(n2Int)) * scaleFactor;
 
-    // 处理除法精度(保留15位小数,避免无限循环)
-    this.currentValue =
-      Number(divResult) / Number(BigInt(10 ** Math.max(0, -decimalDiff)));
+    this.currentValue = parseFloat(divResult.toPrecision(15));
 
     return this;
   },
@@ -1285,13 +1274,14 @@ export const Calc = {
     return result;
   },
 };
+
 export async function getPhoneNumber(e, inviteCode) {
   const { Toast } = useToast();
-  console.log('getPhoneNumber123', e);
+  console.log("getPhoneNumber123", e);
 
   // 用户拒绝授权
-  if (e.detail.errMsg !== 'getPhoneNumber:ok') {
-    Toast({ title: '请授权手机号以完成登录' });
+  if (e.detail.errMsg !== "getPhoneNumber:ok") {
+    Toast({ title: "请授权手机号以完成登录" });
     return null;
   }
 
@@ -1300,25 +1290,24 @@ export async function getPhoneNumber(e, inviteCode) {
     try {
       // 2. 将授权码发送到后端换取手机号
       const res = await wxLogin({ code: e.detail.code });
-      console.log('wxLogin接口res=', res);
+      console.log("wxLogin接口res=", res);
 
       // 3. 处理后端响应
       if (res.code === 200) {
         return res; // 返回数据
       } else {
-        Toast({ title: res.message || '获取失败' });
+        Toast({ title: res.message || "获取失败" });
         return null;
       }
     } catch (error) {
       Toast({ title: "获取手机号失败:" + error });
-      console.error('获取手机号失败:', error);
+      console.error("获取手机号失败:", error);
       return null;
     } finally {
-
     }
   } else {
     // 授权失败
     Toast({ title: e.detail?.errorMessage || "请允许授权以获取手机号" });
     return null;
   }
-};
+}