wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
<template>
    <view class="coupon-get__btns">
        <cl-skeleton
            v-for="item in list"
            :key="item.id"
            :margin="[0, 10, 0, 0]"
            :height="46"
            :loading-style="{
                width: '150rpx',
            }"
            :loading="loading"
        >
            <view class="item" @tap="open">
                <text class="shop-icon-coupon"></text>
                <text>{{ item.text }}</text>
            </view>
        </cl-skeleton>
    </view>
 
    <cl-popup
        v-model="visible"
        direction="bottom"
        :border-radius="[32, 32, 0, 0]"
        :padding="0"
        title="领取优惠券"
        show-close-btn
    >
        <view class="coupon-get">
            <scroll-view class="scroller" scroll-y>
                <view class="list">
                    <coupon-item :item="item" v-for="item in list" :key="item.id">
                        <template #btn>
                            <cl-button
                                type="primary"
                                :height="46"
                                :width="116"
                                :font-size="22"
                                :disabled="!!item.userId"
                                size="small"
                                :margin="[0, 0, 0, 20]"
                                @tap="toGet(item)"
                            >
                                {{ item.userId ? "已领取" : "立即领取" }}
                            </cl-button>
                        </template>
                    </coupon-item>
                </view>
 
                <cl-empty :fixed="false" :margin="[-40, 0, 0, 0]" v-if="isEmpty(list)" />
            </scroll-view>
        </view>
    </cl-popup>
</template>
 
<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { useCool } from "/@/cool";
import { useUi } from "/$/cool-ui";
import CouponItem from "./item/index.vue";
import { isEmpty } from "lodash-es";
 
const emit = defineEmits(["success"]);
 
const { service } = useCool();
const ui = useUi();
 
// 是否可见
const visible = ref(false);
 
// 获取中
const loading = ref(false);
 
// 优惠券列表
const list = ref<CouponInfo[]>([{}, {}]);
 
// 获取列表
async function refresh() {
    await service.market.coupon.info.page({ page: 1, size: 100 }).then((res) => {
        list.value = res.list.map((e) => {
            switch (e.type) {
                case 0:
                    e.text = `满${e.condition?.fullAmount}减${Math.floor(e.amount!)}元`;
                    break;
                default:
                    e.text = "";
                    break;
            }
 
            return e;
        });
    });
}
 
// 打开弹窗
function open() {
    visible.value = true;
    refresh();
}
 
// 关闭弹窗
function close() {
    visible.value = false;
}
 
// 领券
async function toGet(item: CouponInfo) {
    item.userId = true;
 
    service.market.coupon.user
        .receive({
            couponId: item.id,
        })
        .then(() => {
            ui.showToast("领取成功");
        })
        .catch((err) => {
            ui.showToast(err.message);
            item.userId = null;
        });
}
 
onMounted(async () => {
    loading.value = true;
    await refresh();
    loading.value = false;
});
 
defineExpose({
    open,
    close,
});
</script>
 
<style lang="scss" scoped>
.coupon-get {
    &__btns {
        display: flex;
        flex-wrap: wrap;
 
        .item {
            display: inline-flex;
            align-items: center;
            padding: 0 6rpx;
            height: 100%;
            border: 2rpx solid $cl-color-primary;
            box-sizing: border-box;
            border-radius: 8rpx;
            font-size: 22rpx;
            line-height: 1;
 
            .shop-icon-coupon {
                font-size: 36rpx;
                margin-right: 2rpx;
            }
        }
    }
 
    .scroller {
        height: 60vh;
 
        .list {
            padding: 0 32rpx;
        }
    }
}
</style>