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
<template>
    <view class="coupon-select__inner" @tap="open">
        <template v-if="checked">
            <cl-text :size="28" color="error">-</cl-text>
            <cl-text :size="28" type="price" color="error" :value="checked.amount" />
        </template>
 
        <cl-text color="info" v-else>选择优惠券</cl-text>
    </view>
 
    <cl-popup
        v-model="visible"
        direction="bottom"
        :border-radius="[32, 32, 0, 0]"
        :padding="0"
        title="选择优惠券"
        show-close-btn
    >
        <view class="coupon-select">
            <scroll-view class="scroller" scroll-y>
                <view class="list">
                    <view class="item" v-for="item in checkList" :key="item.id" @tap="select(item)">
                        <coupon-item
                            :item="item"
                            :checked="checked?.id == item.id"
                            :disabled="item.disabled"
                        />
                    </view>
                </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 { computed, ref } from "vue";
import { useCool } from "/@/cool";
import { isEmpty } from "lodash-es";
import CouponItem from "./item/index.vue";
import { useUi } from "/$/cool-ui";
 
const props = defineProps({
    totalAmount: {
        type: Number,
        default: 0,
    },
});
 
const emit = defineEmits(["success"]);
 
const { service } = useCool();
const ui = useUi();
 
// 是否可见
const visible = ref(false);
 
// 选中值
const checked = ref<CouponInfo>();
 
// 优惠券列表
const list = ref<CouponInfo[]>();
 
// 待选列表
const checkList = computed(() => {
    return list.value?.map((e) => {
        let disabled = true;
 
        switch (e.type) {
            case 0:
                if (props.totalAmount >= e.condition?.fullAmount!) {
                    disabled = false;
                }
                break;
        }
 
        return {
            ...e,
            disabled,
        };
    });
});
 
// 打开弹窗
function open() {
    visible.value = true;
 
    // 获取未使用的优惠券
    service.market.coupon.user.page({ page: 1, size: 100, status: 0 }).then((res) => {
        list.value = res.list;
    });
}
 
// 关闭弹窗
function close() {
    visible.value = false;
}
 
// 选择
function select(item: CouponInfo) {
    if (item.disabled) {
        ui.showToast("优惠券不可用");
    } else {
        if (checked.value?.id == item.id) {
            checked.value = undefined;
        } else {
            checked.value = item;
        }
 
        close();
    }
}
 
defineExpose({
    open,
    close,
    checked,
});
</script>
 
<style lang="scss" scoped>
.coupon-select {
    height: 60vh;
 
    .scroller {
        height: 100%;
 
        .list {
            padding: 0 32rpx;
        }
    }
}
</style>