绿满眶商城微信小程序-uniapp
peng
1 天以前 89efee9a2e20fc04b4537d859917b47cf68a814c
pages/order/claim-coupon/claim-coupon.vue
@@ -2,8 +2,8 @@
   <view class="container">
      <!-- 顶部标题 -->
      <view class="header">
         <text class="title">优惠券领取</text>
         <text class="subtitle">领取您的专属优惠券</text>
         <text class="title">商品兑换券领取</text>
         <text class="subtitle">领取您的专属商品兑换券</text>
      </view>
      
      <!-- 单个优惠券展示 -->
@@ -89,8 +89,12 @@
            couponInfo: null, // 单个优惠券信息
            loading: false,
            showShoppingModal: false ,// 控制去购物弹窗显示
            couponId:''
            couponId:'',
            cardId:''
         }
      },
      onShow() {
         this.loadCouponInfo(this.cardId);
      },
      onLoad(options) {
         // 通过参数传递优惠券ID
@@ -98,16 +102,44 @@
         
         this.couponId = ''
         if (options.id) {
            this.cardId = options.id
            this.loadCouponInfo(options.id);
         } else {
         }else if(options.q) {
               // 双重解码:微信对URL进行了两次编码
               const decodedUrl = decodeURIComponent(decodeURIComponent(options.q));
               console.log('原始URL:', decodedUrl);
               // 解析URL中的查询参数
               const params = this.parseUrlParams(decodedUrl);
               this.cardId =  params.id;
               this.loadCouponInfo(this.cardId);
            }
          else {
            // 如果没有传递ID,显示空状态
            uni.showToast({
               title: '缺少优惠券ID参数',
               title: '缺少兑换券ID参数',
               icon: 'none'
            });
         }
      },
      methods: {
         // 解析URL参数
         parseUrlParams(url) {
            const params = {};
            // 处理可能存在的hash(如果有的话)
            const cleanUrl = url.split('#')[0];
            const queryStr = cleanUrl.split('?')[1] || '';
            queryStr.split('&').forEach(pair => {
               const [key, value] = pair.split('=');
               if (key) {
                  // 如果值存在,则解码,否则设为空字符串
                  params[key] = value ? decodeURIComponent(value) : '';
               }
            });
            return params;
         },
         // 加载指定ID的优惠券信息
         async loadCouponInfo(id) {
            if (this.loading) return;
@@ -120,12 +152,12 @@
               if (res.data.code===200) {
                  this.couponInfo = res.data.data;
               } else {
                  throw new Error(res.message || '获取优惠券失败');
                  throw new Error(res.message || '获取兑换券失败');
               }
            } catch (err) {
               console.error('获取优惠券信息失败:', err);
               console.error('获取兑换券信息失败:', err);
               uni.showToast({
                  title: err.message || '获取优惠券失败',
                  title: err.message || '获取兑换券失败',
                  icon: 'none'
               });
            } finally {
@@ -137,7 +169,7 @@
         async claimCoupon() {
            if (!this.couponInfo || this.couponInfo.claimStatus === 'CLAIM') {
               uni.showToast({
                  title: '该优惠券已领取',
                  title: '该兑换券已领取',
                  icon: 'none'
               });
               return;
@@ -169,7 +201,7 @@
               }
            } catch (err) {
               uni.hideLoading();
               console.error('领取优惠券失败:', err);
               console.error('领取兑换券失败:', err);
               uni.showToast({
                  title: err.message || '领取失败',
                  icon: 'none'