绿满眶商城微信小程序-uniapp
zxl
2025-09-12 814f5915fde05ecf5e565a07bc9d611cf6202505
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
<template>
    <view class="container">
        <!-- 顶部标题 -->
        <view class="header">
            <text class="title">优惠券领取</text>
            <text class="subtitle">领取您的专属优惠券</text>
        </view>
        
        <!-- 单个优惠券展示 -->
        <view class="coupon-container" v-if="couponInfo">
            <view class="coupon-card">
                <!-- 商品图片 -->
                <view class="goods-image-container">
                    <u-image 
                        :src="couponInfo.goodsUrl"
                        width="100%" 
                        height="300rpx" 
                        mode="aspectFit"
                        border-radius="16"
                        @click="previewImage(couponInfo.goodsUrl)"
                    ></u-image>
                </view>
                
                <!-- 优惠券信息 -->
                <view class="coupon-info">
                    <view class="coupon-name">{{ couponInfo.couponName || couponInfo.name }}</view>
                    <view class="goods-name">{{ couponInfo.skuName }}</view>
                    <view class="coupon-no">编号:{{ couponInfo.couponNo }}</view>
                    <view class="time-info">
                        <text class="create-time">创建时间:{{ formatDate(couponInfo.createTime) }}</text>
                    </view>
                    <view class="status">
                        <u-tag 
                            :text="couponInfo.claimStatus === 'CLAIM' ? '已领取' : '未领取'" 
                            :type="couponInfo.claimStatus === 'CLAIM' ? 'success' : 'warning'" 
                            size="mini"
                        />
                    </view>
                </view>
                
                <!-- 领取按钮 -->
                <view class="claim-btn-container">
                    <button 
                        class="claim-btn" 
                        :class="{ 'claimed': couponInfo.claimStatus === 'CLAIM' }"
                        :disabled="couponInfo.claimStatus === 'CLAIM'"
                        @click="claimCoupon"
                    >
                        {{ couponInfo.claimStatus === 'CLAIM' ? '已领取' : '立即领取' }}
                    </button>
                </view>
            </view>
        </view>
        
        <!-- 加载中状态 -->
        <view class="loading-container" v-else-if="loading">
            <u-loading mode="circle" size="40"></u-loading>
            <text class="loading-text">加载中...</text>
        </view>
        
        <!-- 空状态 -->
        <u-empty 
            v-else 
            text="暂无优惠券" 
            mode="coupon" 
            icon-size="120"
            margin-top="200"
        ></u-empty>
        
        <!-- 去购物弹窗 -->
        <u-modal 
            v-model="showShoppingModal" 
            :content="'恭喜您领取成功,是否立即使用优惠券去购物?'"
            :show-cancel-button="true"
            confirm-text="去购物"
            cancel-text="稍后再说"
            @confirm="goShopping"
            @cancel="closeModal"
        ></u-modal>
    </view>
</template>
 
<script>
    import { getVirtualCouponDetail, claimVirtualCoupon } from '@/api/coupon.js';
    
    export default {
        data() {
            return {
                couponInfo: null, // 单个优惠券信息
                loading: false,
                showShoppingModal: false ,// 控制去购物弹窗显示
                couponId:''
            }
        },
        onLoad(options) {
            // 通过参数传递优惠券ID
            // 例如: /pages/order/claim-coupon/claim-coupon?id=123
            
            this.couponId = ''
            if (options.id) {
                this.loadCouponInfo(options.id);
            } else {
                // 如果没有传递ID,显示空状态
                uni.showToast({
                    title: '缺少优惠券ID参数',
                    icon: 'none'
                });
            }
        },
        methods: {
            // 加载指定ID的优惠券信息
            async loadCouponInfo(id) {
                if (this.loading) return;
                this.loading = true;
                
                try {
                    // 调用获取单个优惠券详情的接口
                    const res = await getVirtualCouponDetail(id);
                    
                    if (res.data.code===200) {
                        this.couponInfo = res.data.data;
                    } else {
                        throw new Error(res.message || '获取优惠券失败');
                    }
                } catch (err) {
                    console.error('获取优惠券信息失败:', err);
                    uni.showToast({
                        title: err.message || '获取优惠券失败',
                        icon: 'none'
                    });
                } finally {
                    this.loading = false;
                }
            },
            
            // 领取优惠券
            async claimCoupon() {
                if (!this.couponInfo || this.couponInfo.claimStatus === 'CLAIM') {
                    uni.showToast({
                        title: '该优惠券已领取',
                        icon: 'none'
                    });
                    return;
                }
                
                try {
                    uni.showLoading({
                        title: '领取中...'
                    });
                    
                    // 调用领取优惠券接口
                    const res = await claimVirtualCoupon(this.couponInfo.id);
                    
                    if (res.data.code===200) {
                        // 更新本地状态
                        this.couponId = res.data.data
                        this.$set(this.couponInfo, 'claimStatus', 'CLAIM');
                        this.$set(this.couponInfo, 'claimTime', new Date());
                        uni.hideLoading();
                        uni.showToast({
                            title: '领取成功',
                            icon: 'success'
                        });
                        
                        // 显示去购物弹窗
                        this.showShoppingModal = true;
                    } else {
                        throw new Error(res.message || '领取失败');
                    }
                } catch (err) {
                    uni.hideLoading();
                    console.error('领取优惠券失败:', err);
                    uni.showToast({
                        title: err.message || '领取失败',
                        icon: 'none'
                    });
                }
            },
            
            // 去购物
            goShopping() {
                // 关闭弹窗
                this.showShoppingModal = false;
                
                // 这里可以添加跳转到购物页面的逻辑
                // 例如跳转到商品详情页或购物车页面
                // 根据实际需求调整跳转路径
                if (this.couponInfo && this.couponInfo.skuId) {
                    // 如果有商品ID,跳转到商品详情页
                    uni.navigateTo({
                        url: `/pages/commodity-square/coups-goods-list?promotionsId=${this.couponId}&promotionType=COUPON`
                    });
                } else {
                    // 否则跳转到首页或其他页面
                    uni.switchTab({
                        url: '/pages/tabbar/index/home'
                    });
                }
            },
            
            // 关闭弹窗
            closeModal() {
                this.showShoppingModal = false;
                // 关闭弹窗后返回上一页
                uni.navigateBack();
            },
            
            // 格式化日期
            formatDate(date) {
                if (!date) return '';
                const d = new Date(date);
                return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')} ${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`;
            },
            
            // 预览图片
            previewImage(url) {
                if (url) {
                    uni.previewImage({
                        urls: [url]
                    });
                }
            }
        }
    }
</script>
 
<style lang="scss" scoped>
.container {
    padding: 20rpx;
    background-color: #f5f5f5;
    min-height: 100vh;
}
 
.header {
    background-color: #fff;
    padding: 30rpx;
    border-radius: 16rpx;
    margin-bottom: 20rpx;
    text-align: center;
    
    .title {
        font-size: 36rpx;
        font-weight: bold;
        color: #333;
        display: block;
        margin-bottom: 10rpx;
    }
    
    .subtitle {
        font-size: 28rpx;
        color: #999;
    }
}
 
.coupon-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 60vh;
}
 
.coupon-card {
    background-color: #fff;
    border-radius: 16rpx;
    padding: 30rpx;
    width: 90%;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
    text-align: center;
    
    .goods-image-container {
        margin-bottom: 30rpx;
        border-radius: 16rpx;
        overflow: hidden;
    }
    
    .coupon-info {
        margin-bottom: 30rpx;
        
        .coupon-name {
            font-size: 36rpx;
            font-weight: bold;
            color: #333;
            margin-bottom: 20rpx;
        }
        
        .goods-name {
            font-size: 30rpx;
            color: #666;
            margin-bottom: 20rpx;
            line-height: 1.5;
        }
        
        .coupon-no {
            font-size: 26rpx;
            color: #999;
            margin-bottom: 20rpx;
        }
        
        .time-info {
            font-size: 26rpx;
            color: #999;
            margin-bottom: 20rpx;
        }
        
        .status {
            margin-bottom: 20rpx;
        }
    }
    
    .claim-btn-container {
        .claim-btn {
            width: 80%;
            height: 80rpx;
            background-color: #ff6b6b;
            color: #fff;
            border-radius: 40rpx;
            font-size: 32rpx;
            border: none;
            margin: 0 auto;
            
            &:active {
                background-color: #ff5252;
            }
            
            &.claimed {
                background-color: #ccc;
            }
            
            &[disabled] {
                background-color: #ccc;
            }
        }
    }
}
 
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 60vh;
    
    .loading-text {
        margin-top: 20rpx;
        color: #999;
        font-size: 28rpx;
    }
}
</style>