绿满眶商城微信小程序-uniapp
peng
9 小时以前 426b946268fcd30280512ad46993735f42d5f666
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<template>
    <view class="container">
        <!-- 优惠券卡片 -->
        <view class="coupon-card" v-if="couponInfo.storeCoupRef">
            <u-card :border="false" :head-style="{ padding: '30rpx' }" :body-style="{ padding: '0 30rpx 30rpx' }">
                <!-- 头部:店铺信息 -->
                <view slot="head" class="card-head">
                    <view class="store-info">
                        <u-icon name="home" size="36" color="#999"></u-icon>
                        <text class="store-name">{{ couponInfo.storeName }}</text>
                    </view>
                    <u-tag v-if="couponInfo.claimStatus === 'NOT_CLAIM'" text="未领取" type="warning" mode="plain" size="mini" />
                    <u-tag v-else-if="couponInfo.claimStatus === 'CLAIM'" text="已领取" type="success" mode="plain" size="mini" />
                    <u-tag v-else text="已过期" type="info" mode="plain" size="mini" />
                </view>
                
                <!-- 主体:优惠券信息 -->
                <view slot="body" class="card-body">
                    <view class="coupon-title">
                        <text class="title">{{ couponInfo.couponName }}</text>
                        <text class="coupon-no">编号:{{ couponInfo.couponNo }}</text>
                    </view>
                    
                    <view class="coupon-desc" v-if="couponInfo.couponDesc">
                        <text class="desc">{{ couponInfo.couponDesc }}</text>
                    </view>
                    
                    <view class="coupon-rule" v-if="couponInfo.couponRule">
                        <text class="rule-title">使用规则:</text>
                        <text class="rule-content">{{ couponInfo.couponRule }}</text>
                    </view>
                    
                    <view class="coupon-time" v-if="couponInfo.startTime && couponInfo.endTime">
                        <u-icon name="clock" size="28" color="#999"></u-icon>
                        <text class="time-text">有效期:{{ formatDate(couponInfo.startTime) }} 至 {{ formatDate(couponInfo.endTime) }}</text>
                    </view>
                    
                    <view class="coupon-condition" v-if="couponInfo.consumeThreshold > 0">
                        <u-icon name="coupon" size="28" color="#999"></u-icon>
                        <text class="condition-text">满{{ formatAmount(couponInfo.consumeThreshold) }}元可用</text>
                    </view>
                </view>
                
                <!-- 底部:操作按钮 -->
                <view slot="foot" class="card-foot">
                    <u-button 
                        :type="couponInfo.claimStatus === 'NOT_CLAIM' ? 'primary' : 'default'" 
                        :disabled="couponInfo.claimStatus !== 'NOT_CLAIM'"
                        :loading="loading"
                        @click="claimCoupon"
                        :ripple="true"
                        :hair-line="false"
                    >
                        {{ couponInfo.claimStatus === 'NOT_CLAIM' ? '立即领取' : couponInfo.claimStatus === 'CLAIM' ? '已领取' : '已过期' }}
                    </u-button>
                </view>
            </u-card>
        </view>
        
        <!-- 空状态 -->
        <view class="empty-state" v-else>
            <u-empty text="优惠券信息不存在" mode="coupon" :icon-size="200"></u-empty>
        </view>
        
        <!-- 使用说明 -->
        <view class="instructions">
            <view class="instructions-title">
                <u-icon name="info-circle" size="28" color="#999"></u-icon>
                <text class="title-text">使用说明</text>
            </view>
            <view class="instructions-content">
                <text class="content-text">1. 优惠券仅限在指定店铺使用\n2. 每个用户限领一张\n3. 优惠券不可兑换现金\n4. 请在有效期内使用</text>
            </view>
        </view>
        
        <!-- 空白占位 -->
        <view class="placeholder"></view>
    </view>
</template>
 
<script>
    import { getStoreCouponDetail, claimStoreCoupon } from '@/api/store-coupon.js';
    import { formatPrice } from '@/utils/Foundation.js';
    
    export default {
        data() {
            return {
                loading: false,
                storeCoupRef: '', // 店铺优惠券关联ID
                couponInfo: {
                    id: "",
                    storeCoupRef: "",
                    storeId: "",
                    storeName: "",
                    couponId: "",
                    couponName: "",
                    couponNo: "",
                    couponAmount: 0, // 优惠金额
                    couponDesc: "", // 优惠券描述
                    couponRule: "", // 使用规则
                    startTime: "", // 开始时间
                    endTime: "", // 结束时间
                    consumeThreshold: 0, // 消费门槛
                    claimStatus: "NOT_CLAIM", // 领取状态
                }
            }
        },
        onShow() {
        this.getCouponDetail(this.storeCoupRef);    
        },
        onLoad(options) {
            // 获取传递的店铺优惠券关联ID
            if (options.storeCoupRef) {
                this.storeCoupRef = options.storeCoupRef;
                this.getCouponDetail(options.storeCoupRef);
            } else if (options.id) {
                // 兼容旧参数
                this.storeCoupRef = options.id;
                this.getCouponDetail(options.id);
            }else if(options.q){
                // 双重解码:微信对URL进行了两次编码
                const decodedUrl = decodeURIComponent(decodeURIComponent(options.q));
                console.log('原始URL:', decodedUrl);
                                
                // 解析URL中的查询参数
                const params = this.parseUrlParams(decodedUrl);
                this.storeCoupRef = params.id;
                this.getCouponDetail(this.storeCoupRef);
            }
             else {
                this.$u.toast('参数错误');
            }
        },
        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;
            },
            // 格式化金额
            formatAmount(amount) {
                if (!amount) return '0';
                return formatPrice(parseFloat(amount));
            },
            
            // 格式化日期
            formatDate(dateStr) {
                if (!dateStr) return '';
                return dateStr.split(' ')[0];
            },
            
            // 获取优惠券详情
            async getCouponDetail(storeCoupRef) {
                uni.showLoading({
                    title: '加载中...'
                });
                
                try {
                    const res = await getStoreCouponDetail(storeCoupRef);
                    console.log(JSON.stringify(res))
                    if (res.data.code === 200) {
                        this.couponInfo = {
                            ...this.couponInfo,
                            ...res.data.data,
                            couponAmount: res.data.data.couponAmount || 0,
                            consumeThreshold: res.data.data.consumeThreshold || 0
                        };
                    } else {
                        this.$u.toast(res.data.message || '获取优惠券详情失败');
                    }
                } catch (err) {
                    this.$u.toast('获取优惠券详情失败,请稍后重试');
                    console.error('获取优惠券详情失败:', err);
                } finally {
                    uni.hideLoading();
                }
            },
            
            // 领取优惠券
            async claimCoupon() {
                if (this.couponInfo.claimStatus !== 'NOT_CLAIM') {
                    return;
                }
                
                // 确认领取
                uni.showModal({
                    title: '提示',
                    content: `确定要领取"${this.couponInfo.couponName}"优惠券吗?`,
                    success: (res) => {
                        if (res.confirm) {
                            this.doClaimCoupon();
                        }
                    }
                });
            },
            
            // 执行领取优惠券操作
            async doClaimCoupon() {
                this.loading = true;
                try {
                    // 调用领取优惠券接口
                    const res = await claimStoreCoupon(this.storeCoupRef);
                    if (res.data.code === 200) {
                        this.$u.toast('领取成功');
                        this.couponInfo.claimStatus = 'CLAIM';
                        
                        // 延迟返回上一页,让用户看到领取成功的提示
                        setTimeout(() => {
                            uni.navigateBack();
                        }, 1500);
                    } else {
                        this.$u.toast(res.data.message || '领取失败');
                    }
                } catch (err) {
                    this.$u.toast('领取失败,请稍后重试');
                    console.error('领取优惠券失败:', err);
                } finally {
                    this.loading = false;
                }
            }
        }
    }
</script>
 
<style lang="scss" scoped>
.container {
    background-color: #f5f5f5;
    min-height: 100vh;
}
 
.coupon-card {
    margin: 20rpx;
}
 
.card-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
 
.store-info {
    display: flex;
    align-items: center;
}
 
.store-name {
    margin-left: 10rpx;
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
}
 
.card-body {
    padding-top: 20rpx;
}
 
.coupon-title {
    margin-bottom: 30rpx;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
}
 
.title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
}
 
.coupon-no {
    font-size: 24rpx;
    color: #999;
    margin-left: 20rpx;
}
 
.coupon-desc {
    margin-bottom: 30rpx;
}
 
.desc {
    font-size: 28rpx;
    color: #666;
}
 
.coupon-rule {
    margin-bottom: 30rpx;
}
 
.rule-title {
    font-size: 28rpx;
    color: #333;
    font-weight: bold;
}
 
.rule-content {
    font-size: 26rpx;
    color: #666;
}
 
.coupon-time, .coupon-condition {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;
}
 
.time-text, .condition-text {
    font-size: 24rpx;
    color: #999;
    margin-left: 10rpx;
}
 
.card-foot {
    padding: 20rpx 0;
}
 
.instructions {
    background-color: #ffffff;
    margin: 20rpx;
    padding: 30rpx;
    border-radius: 16rpx;
}
 
.instructions-title {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;
}
 
.title-text {
    margin-left: 10rpx;
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
}
 
.instructions-content {
    padding-left: 40rpx;
}
 
.content-text {
    font-size: 26rpx;
    color: #666;
    line-height: 1.6;
}
 
.placeholder {
    height: 40rpx;
}
 
.empty-state {
    margin-top: 200rpx;
}
</style>