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
<template>
    <view class="select-address" @tap="open()">
        <address-item :item="data || address.info">
            <template #icon>
                <text class="cl-icon-arrow-right" v-if="!disabled"></text>
            </template>
        </address-item>
    </view>
 
    <cl-popup
        v-model="visible"
        direction="bottom"
        :border-radius="[32, 32, 0, 0]"
        :padding="0"
        show-close-btn
    >
        <view class="select-address__popup">
            <cl-text :size="30" block bold :margin="[30, 0, 30, 30]">选择地址</cl-text>
 
            <cl-loading-mask :loading="loading">
                <scroll-view scroll-y class="scroller">
                    <view class="list">
                        <view
                            class="item"
                            v-for="item in list"
                            :key="item.id"
                            :class="{
                                'is-active': item.id == address.info?.id,
                            }"
                            @tap="select(item)"
                        >
                            <address-item :item="item" background-color="#f7f7f7">
                                <template #icon>
                                    <text class="cl-icon-check-border"></text>
                                </template>
                            </address-item>
                        </view>
                    </view>
 
                    <cl-empty icon="address" :fixed="false" text="暂无地址" v-if="isEmpty(list)" />
                </scroll-view>
            </cl-loading-mask>
 
            <cl-footer :fixed="false" :vt="[visible]">
                <cl-button custom type="primary" @tap="router.push('/pages/user/address-edit')"
                    >添加收货地址</cl-button
                >
            </cl-footer>
        </view>
    </cl-popup>
</template>
 
<script lang="ts" setup>
import { ref } from "vue";
import { useAddress } from "/@/hooks";
import { useCool } from "/@/cool";
import { onShow } from "@dcloudio/uni-app";
import AddressItem from "./item.vue";
import { isEmpty } from "lodash-es";
 
const props = defineProps({
    disabled: Boolean,
    data: Object,
});
 
const { service, router } = useCool();
const address = useAddress();
 
const visible = ref(false);
const loading = ref(false);
 
// 地址列表
const list = ref<Eps.UserAddressEntity[]>([]);
 
// 获取地址
async function refresh() {
    loading.value = true;
 
    await service.user.address.list().then((res) => {
        list.value = res;
    });
 
    loading.value = false;
}
 
function select(item: Eps.UserAddressEntity) {
    address.set(item);
    close();
}
 
function open() {
    if (!props.disabled) {
        visible.value = true;
    }
}
 
function close() {
    visible.value = false;
}
 
onShow(() => {
    refresh();
    address.getDefault();
});
 
defineExpose({
    open,
    close,
});
</script>
 
<style lang="scss" scoped>
.select-address {
    margin-bottom: 24rpx;
 
    &__popup {
        .scroller {
            height: 50vh;
 
            .list {
                padding: 0 32rpx;
 
                .item {
                    border: 2rpx solid #f7f7f7;
                    border-radius: 24rpx;
                    margin-bottom: 24rpx;
 
                    .cl-icon-check-border {
                        display: none;
                        color: $cl-color-primary;
                        font-size: 46rpx;
                    }
 
                    &.is-active {
                        border-color: $cl-color-primary;
 
                        .cl-icon-check-border {
                            display: block;
                        }
                    }
 
                    &:last-child {
                        margin-bottom: 0;
                    }
                }
            }
        }
    }
}
</style>