Explorar o código

优惠劵id修改

sunshibo hai 1 mes
pai
achega
4cdd1257bb
Modificáronse 1 ficheiros con 14 adicións e 29 borrados
  1. 14 29
      pages/placeOrder/index.vue

+ 14 - 29
pages/placeOrder/index.vue

@@ -206,6 +206,7 @@ function normalizeCouponItem(item) {
   else if (couponType === 2 && discountRate > 0) amountDisplay = (discountRate % 1 === 0 ? discountRate : discountRate.toFixed(1)) + '折';
   return {
     id: raw.userCouponId ?? raw.id ?? raw.couponId ?? '',
+    couponId: raw.couponId ?? raw.id ?? raw.userCouponId ?? '',
     amount: nominalValue,
     minAmount,
     name: raw.name ?? raw.title ?? raw.couponName ?? '',
@@ -240,7 +241,7 @@ const openCouponModal = async () => {
 // 选择优惠券后更新订单优惠与应付金额(折扣券按折扣率计算优惠金额)
 const handleCouponSelect = ({ coupon, selectedId }) => {
   selectedCouponId.value = selectedId != null && selectedId !== '' ? String(selectedId) : null;
-  orderInfo.value.couponId = selectedCouponId.value;
+  orderInfo.value.couponId = coupon ? (coupon.couponId ?? coupon.id ?? selectedCouponId.value) : null;
   if (!coupon) {
     selectedCouponDisplay.value = '';
     orderInfo.value.discountAmount = 0;
@@ -268,29 +269,6 @@ const updatePayAmount = () => {
   orderInfo.value.payAmount = Math.max(0, dish + utensil - discount);
 };
 
-// 进入页面时调一次优惠券接口:默认不选中,显示请选择
-const fetchCouponsOnEnter = async () => {
-  const storeId = uni.getStorageSync('currentStoreId') || '';
-  try {
-    const res = await diningApi.GetUserOwnedCouponList({ storeId });
-    const list = Array.isArray(res) ? res : (res?.data ?? res?.records ?? res?.list ?? []);
-    const normalized = (Array.isArray(list) ? list : []).map(normalizeCouponItem).filter(Boolean);
-    couponList.value = normalized;
-    selectedCouponId.value = null;
-    selectedCouponDisplay.value = '';
-    orderInfo.value.couponId = null;
-    orderInfo.value.discountAmount = 0;
-    updatePayAmount();
-  } catch (err) {
-    console.error('获取优惠券失败:', err);
-    selectedCouponDisplay.value = '';
-    orderInfo.value.discountAmount = 0;
-    orderInfo.value.couponId = null;
-    selectedCouponId.value = null;
-    updatePayAmount();
-  }
-};
-
 // 将 tags 统一为 [{ text, type }] 格式
 function normalizeTags(raw) {
   if (raw == null) return [];
@@ -346,18 +324,22 @@ const handleConfirmOrder = async () => {
     const tablewareFee = Number(orderInfo.value.utensilFee) || 0;
     const payAmount = Number((Number(orderInfo.value.payAmount) ?? 0).toFixed(2));
     const totalAmount = Number((Number(dishTotal.value) ?? 0).toFixed(2));
-    const res = await diningApi.PostOrderCreate({
+    const couponIdVal = orderInfo.value.couponId ?? selectedCouponId.value;
+    const couponIdToSend = (couponIdVal != null && couponIdVal !== '') ? String(couponIdVal) : null;
+    const createParams = {
       tableId,
       contactPhone,
       totalAmount,
-      couponId: (selectedCouponId.value ?? orderInfo.value.couponId) || null,
+      couponId: couponIdToSend,
+      userCouponId: couponIdToSend,
       discountAmount: orderInfo.value.discountAmount ?? 0,
       tablewareFee,
       payAmount,
       dinerCount: dinerCount || undefined,
       immediatePay: 0,
       remark: remark || undefined
-    });
+    };
+    const res = await diningApi.PostOrderCreate(createParams);
     uni.hideLoading();
     const orderId = res?.id ?? res?.orderId ?? res?.data?.id ?? res?.data?.orderId;
     const orderNo = res?.orderNo ?? res?.data?.orderNo ?? orderId ?? '';
@@ -408,14 +390,17 @@ onLoad((options) => {
       const fee = data.utensilFee ?? data.tablewareFee;
       if (fee != null && fee !== '') orderInfo.value.utensilFee = Number(fee) || 0;
       if (data.discountAmount != null) orderInfo.value.discountAmount = data.discountAmount;
-      if (data.couponId != null) selectedCouponId.value = data.couponId;
+      if (data.couponId != null) {
+        selectedCouponId.value = data.couponId;
+        orderInfo.value.couponId = data.couponId;
+      }
       updatePayAmount();
     } catch (e) {
       console.error('解析购物车数据失败:', e);
     }
   }
   if (orderInfo.value.tableId) uni.setStorageSync('currentTableId', String(orderInfo.value.tableId));
-  fetchCouponsOnEnter();
+  // 不再主动调用 userOwnedByStore,优惠券在点击「优惠券」行打开弹窗时再拉取
 });