瀏覽代碼

feat(coupon): 优化最低消费金额验证规则

- 引入validatePriceFormat验证器替代原有复杂逻辑
- 支持整数部分最多6位,小数部分最多2位的金额格式
- 移除冗余的最低消费金额监听逻辑
- 简化表单验证代码结构,提高可维护性
congxuesong 3 周之前
父節點
當前提交
47b5e4c2e6
共有 1 個文件被更改,包括 2 次插入40 次删除
  1. 2 40
      src/views/ticketManagement/newCoupon.vue

+ 2 - 40
src/views/ticketManagement/newCoupon.vue

@@ -108,7 +108,7 @@ import { ElMessage } from "element-plus";
 import { useRoute, useRouter } from "vue-router";
 import type { FormInstance } from "element-plus";
 import { getCouponDetail, addDiscountCoupon, editDiscountCoupon } from "@/api/modules/couponManagement";
-import { validatePositiveNumber, validatePositiveInteger, validateDateRange } from "@/utils/eleValidate";
+import { validatePositiveNumber, validatePositiveInteger, validateDateRange, validatePriceFormat } from "@/utils/eleValidate";
 import { localGet } from "@/utils";
 import { getVoucherDetail } from "@/api/modules/voucherManagement";
 // ==================== 响应式数据定义 ====================
@@ -175,28 +175,7 @@ const rules = reactive({
   minimumSpendingAmount: [
     { required: true, message: "请输入最低消费金额" },
     {
-      validator: (rule: any, value: any, callback: any) => {
-        if (couponModel.value.hasMinimumSpend === 1) {
-          if (!value || value.toString().trim() === "") {
-            callback(new Error("请输入最低消费金额"));
-            return;
-          }
-          const strValue = value.toString().trim();
-          // 检查是否以小数点开头(如 ".1")
-          if (strValue.startsWith(".")) {
-            callback(new Error("最低消费金额格式不正确"));
-            return;
-          }
-          // 检查是否有前导零(除了单独的"0"或"0."开头的小数)
-          if (strValue.length > 1 && strValue.startsWith("0") && strValue !== "0" && !strValue.startsWith("0.")) {
-            callback(new Error("最低消费金额必须为正数"));
-            return;
-          }
-          validatePositiveNumber("最低消费金额必须为正数")(rule, value, callback);
-        } else {
-          callback();
-        }
-      },
+      validator: validatePriceFormat("整数部分最多6位,小数部分最多2位"),
       trigger: "blur"
     }
   ]
@@ -296,23 +275,6 @@ watch(
   }
 );
 
-/**
- * 监听最低消费金额变化
- * 当最低消费金额大于0时,自动设置为"是",否则设置为"否"
- */
-// watch(
-//   () => couponModel.value.minimumSpendingAmount,
-//   newVal => {
-//     if (isInitializing.value) return;
-//     const amount = Number(newVal);
-//     if (!isNaN(amount) && amount > 0) {
-//       couponModel.value.hasMinimumSpend = 1;
-//     } else {
-//       couponModel.value.hasMinimumSpend = 0;
-//     }
-//   }
-// );
-
 // ==================== 事件处理函数 ====================
 /**
  * 组件挂载时初始化