mall-uniapp/pages/user/address/list.vue

143 lines
3.6 KiB
Vue
Raw Normal View History

2023-12-16 12:02:31 +00:00
<!-- 收件地址列表 -->
2022-11-22 07:45:36 +00:00
<template>
2023-12-05 06:27:36 +00:00
<s-layout title="收货地址" :bgStyle="{ color: '#FFF' }">
<view v-if="state.list.length">
<s-address-item hasBorderBottom v-for="item in state.list" :key="item.id" :item="item"
2023-12-16 12:02:31 +00:00
@tap="onSelect(item)" />
2023-12-05 06:27:36 +00:00
</view>
2022-11-22 07:45:36 +00:00
2023-12-05 06:27:36 +00:00
<su-fixed bottom placeholder>
<view class="footer-box ss-flex ss-row-between ss-p-20">
<!-- 微信小程序和微信H5 -->
<button v-if="['WechatMiniProgram', 'WechatOfficialAccount'].includes(sheep.$platform.name)"
@tap="importWechatAddress"
class="border ss-reset-button sync-wxaddress ss-m-20 ss-flex ss-row-center ss-col-center">
<text class="cicon-weixin ss-p-r-10" style="color: #09bb07; font-size: 40rpx"></text>
导入微信地址
</button>
<button class="add-btn ss-reset-button ui-Shadow-Main"
@tap="sheep.$router.go('/pages/user/address/edit')">
新增收货地址
</button>
</view>
</su-fixed>
<s-empty v-if="state.list.length === 0 && !state.loading" text="暂无收货地址" icon="/static/data-empty.png" />
</s-layout>
2022-11-22 07:45:36 +00:00
</template>
<script setup>
2023-12-16 12:02:31 +00:00
import { reactive, onBeforeMount } from 'vue';
import { onShow } from '@dcloudio/uni-app';
2023-12-05 06:27:36 +00:00
import sheep from '@/sheep';
2023-12-16 12:02:31 +00:00
import { isEmpty } from 'lodash';
import AreaApi from '@/sheep/api/system/area';
import AddressApi from '@/sheep/api/member/address';
2022-11-22 07:45:36 +00:00
2023-12-05 06:27:36 +00:00
const state = reactive({
2023-12-16 12:02:31 +00:00
list: [], // 地址列表
2023-12-05 06:27:36 +00:00
loading: true,
});
2022-11-22 07:45:36 +00:00
2023-12-05 06:27:36 +00:00
// 选择收货地址
const onSelect = (addressInfo) => {
uni.$emit('SELECT_ADDRESS', {
addressInfo,
});
sheep.$router.back();
};
2022-11-22 07:45:36 +00:00
2023-12-05 06:27:36 +00:00
// 导入微信地址
2023-12-16 12:02:31 +00:00
// TODO 芋艿:未测试
2023-12-05 06:27:36 +00:00
function importWechatAddress() {
let wechatAddress = {};
// #ifdef MP
uni.chooseAddress({
success: (res) => {
wechatAddress = {
consignee: res.userName,
mobile: res.telNumber,
province_name: res.provinceName,
city_name: res.cityName,
district_name: res.countyName,
address: res.detailInfo,
region: '',
is_default: false,
};
if (!isEmpty(wechatAddress)) {
sheep.$router.go('/pages/user/address/edit', {
data: JSON.stringify(wechatAddress),
});
}
},
fail: (err) => {
console.log('%cuni.chooseAddress,调用失败', 'color:green;background:yellow');
},
});
// #endif
// #ifdef H5
sheep.$platform.useProvider('wechat').jssdk.openAddress({
success: (res) => {
wechatAddress = {
consignee: res.userName,
mobile: res.telNumber,
province_name: res.provinceName,
city_name: res.cityName,
district_name: res.countryName,
address: res.detailInfo,
region: '',
is_default: false,
};
if (!isEmpty(wechatAddress)) {
sheep.$router.go('/pages/user/address/edit', {
data: JSON.stringify(wechatAddress),
});
}
},
});
// #endif
}
2022-11-22 07:45:36 +00:00
2023-12-05 06:27:36 +00:00
onShow(async () => {
2023-12-16 12:02:31 +00:00
state.list = (await AddressApi.getAddressList()).data;
2023-12-05 06:27:36 +00:00
state.loading = false;
});
2022-11-22 07:45:36 +00:00
2023-12-05 06:27:36 +00:00
onBeforeMount(() => {
if (!!uni.getStorageSync('areaData')) {
return;
}
// 提前加载省市区数据
2023-12-16 12:02:31 +00:00
AreaApi.getAreaTree().then((res) => {
if (res.code === 0) {
2023-12-05 06:27:36 +00:00
uni.setStorageSync('areaData', res.data);
}
});
});
2022-11-22 07:45:36 +00:00
</script>
<style lang="scss" scoped>
2023-12-05 06:27:36 +00:00
.footer-box {
.add-btn {
flex: 1;
background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
border-radius: 80rpx;
font-size: 30rpx;
font-weight: 500;
line-height: 80rpx;
color: $white;
position: relative;
z-index: 1;
}
2022-11-22 07:45:36 +00:00
2023-12-05 06:27:36 +00:00
.sync-wxaddress {
flex: 1;
line-height: 80rpx;
background: $white;
border-radius: 80rpx;
font-size: 30rpx;
font-weight: 500;
color: $dark-6;
margin-right: 18rpx;
}
}
</style>