Quellcode durchsuchen

feat(coupon): 新增优惠券创建功能

- 修改 couponManagement.ts 中的 saveDraft 方法为 addDiscountCoupon,并更新其请求路径
- 在 eleValidate.ts 中优化日期范围验证逻辑,支持自动识别开始和结束日期字段
- 在 newCoupon.vue 中引入并使用新的 addDiscountCoupon 接口实现优惠券提交功能
- 表单提交逻辑中增加对优惠券名称的校验以及状态、类型的默认赋值
- 修复日期比较时的时间精度问题,确保日期对比准确性
congxuesong vor 4 Wochen
Ursprung
Commit
795ab4c85e

+ 2 - 2
src/api/modules/couponManagement.ts

@@ -14,8 +14,8 @@ export const updateStatus = params => {
 export const updateNum = params => {
   return http.get(PORT_NONE + `/PcGroupBuy/updateNum1`, params);
 };
-export const saveDraft = params => {
-  return http.post(PORT_NONE + `/PcGroupBuy/saveDraft`, params);
+export const addDiscountCoupon = params => {
+  return http.post(PORT_NONE + `/discountCouponPlatform/addDiscountCoupon`, params);
 };
 export const getMenuByStoreId = params => {
   return http.get(PORT_NONE + `/menuPlatform/getMenuByStoreId`, params);

+ 30 - 1
src/utils/eleValidate.ts

@@ -205,12 +205,37 @@ export function validateDateRange(
   rangeErrorMessage: string = "开始时间必须早于结束时间",
   checkToday: boolean = true
 ) {
-  return (rule: any, value: any, callback: any, isStartDate: boolean = true) => {
+  return (rule: any, value: any, callback: any) => {
     if (!value) {
       callback();
       return;
     }
     const selectedDate = new Date(value);
+    selectedDate.setHours(0, 0, 0, 0);
+
+    // 自动判断是开始日期还是结束日期
+    // 优先通过值比较判断(最可靠),其次通过字段名判断
+    const startDateValue = getStartDate();
+    const endDateValue = getEndDate ? getEndDate() : null;
+    const fieldName = rule?.field || "";
+
+    let isStartDate = false;
+
+    // 优先通过值比较判断
+    if (value === startDateValue) {
+      isStartDate = true;
+    } else if (endDateValue && value === endDateValue) {
+      isStartDate = false;
+    } else if (fieldName) {
+      // 如果值比较无法判断,则通过字段名判断
+      // 如果字段名包含 "begin"、"start"、"开始" 等关键词,认为是开始日期
+      const lowerFieldName = fieldName.toLowerCase();
+      isStartDate = lowerFieldName.includes("begin") || lowerFieldName.includes("start") || lowerFieldName.includes("开始");
+      // 如果字段名包含 "end"、"结束" 等关键词,明确认为是结束日期
+      if (lowerFieldName.includes("end") || lowerFieldName.includes("结束")) {
+        isStartDate = false;
+      }
+    }
 
     // 验证不能早于今天
     if (checkToday) {
@@ -224,18 +249,22 @@ export function validateDateRange(
 
     // 验证日期范围
     if (isStartDate && getEndDate) {
+      // 验证开始日期:开始日期必须早于结束日期
       const endDate = getEndDate();
       if (endDate) {
         const end = new Date(endDate);
+        end.setHours(0, 0, 0, 0);
         if (selectedDate >= end) {
           callback(new Error(rangeErrorMessage));
           return;
         }
       }
     } else if (!isStartDate && getStartDate) {
+      // 验证结束日期:结束日期必须晚于开始日期
       const startDate = getStartDate();
       if (startDate) {
         const start = new Date(startDate);
+        start.setHours(0, 0, 0, 0);
         if (selectedDate <= start) {
           callback(new Error(rangeErrorMessage));
           return;

+ 25 - 22
src/views/ticketManagement/newCoupon.vue

@@ -107,7 +107,7 @@ import { ref, reactive, watch, nextTick, onMounted } from "vue";
 import { ElMessage } from "element-plus";
 import { useRoute, useRouter } from "vue-router";
 import type { FormInstance } from "element-plus";
-import { getCouponDetail } from "@/api/modules/couponManagement";
+import { getCouponDetail, addDiscountCoupon } from "@/api/modules/couponManagement";
 import { validatePositiveNumber, validatePositiveInteger, validateDateRange } from "@/utils/eleValidate";
 import { localGet } from "@/utils";
 // ==================== 响应式数据定义 ====================
@@ -304,30 +304,33 @@ const ruleFormRef = ref<FormInstance>(); // 表单引用
  * 提交数据(新增)
  * 验证表单,通过后调用相应的API接口
  */
-const handleSubmit = (submitType?: string) => {
+const handleSubmit = async (submitType?: string) => {
+  // 组装提交参数
+  let params: any = { ...couponModel.value };
+  params.storeId = localGet("createdId");
+  params.couponId = "";
+  params.couponStatus = submitType ? 0 : 1;
+  params.restrictedQuantity = 0;
+  params.type = 1;
+  if (submitType) {
+    if (!couponModel.value.name) {
+      ElMessage.warning("请填写优惠券名称");
+      return;
+    }
+    let res: any = await addDiscountCoupon(params);
+    if (res && res.code == 200) {
+      ElMessage.success("保存成功");
+      goBack();
+    }
+    return;
+  }
   // 验证表单
   ruleFormRef.value!.validate(async (valid: boolean) => {
     if (!valid) return;
-
-    // 组装提交参数
-    let params: any = { ...couponModel.value };
-    params.storeId = localGet("createdId");
-    params.status = 3;
-    params.couponId = "couponId";
-    console.log("提交参数:", params);
-
-    // TODO: 调用API保存数据
-    // if (submitType === 'draft') {
-    //   await saveCouponDraft(params);
-    // } else {
-    //   await saveCoupon(params);
-    // }
-
-    if (submitType === "draft") {
-      ElMessage.success("草稿保存成功");
-    } else {
-      ElMessage.success("优惠券创建成功");
-      router.go(-1);
+    let res: any = await addDiscountCoupon(params);
+    if (res && res.code == 200) {
+      ElMessage.success("创建成功");
+      goBack();
     }
   });
 };