list.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!-- 收件地址列表 -->
  2. <template>
  3. <s-layout title="收货地址" :bgStyle="{ color: '#FFF' }">
  4. <view v-if="state.list.length">
  5. <s-address-item hasBorderBottom v-for="item in state.list" :key="item.id" :item="item"
  6. @tap="onSelect(item)" />
  7. </view>
  8. <su-fixed bottom placeholder>
  9. <view class="footer-box ss-flex ss-row-between ss-p-20">
  10. <!-- 微信小程序和微信H5 -->
  11. <button v-if="['WechatMiniProgram', 'WechatOfficialAccount'].includes(sheep.$platform.name)"
  12. @tap="importWechatAddress"
  13. class="border ss-reset-button sync-wxaddress ss-m-20 ss-flex ss-row-center ss-col-center">
  14. <text class="cicon-weixin ss-p-r-10" style="color: #09bb07; font-size: 40rpx"></text>
  15. 导入微信地址
  16. </button>
  17. <button class="add-btn ss-reset-button ui-Shadow-Main"
  18. @tap="sheep.$router.go('/pages/user/address/edit')">
  19. 新增收货地址
  20. </button>
  21. </view>
  22. </su-fixed>
  23. <s-empty v-if="state.list.length === 0 && !state.loading" text="暂无收货地址" icon="/static/data-empty.png" />
  24. </s-layout>
  25. </template>
  26. <script setup>
  27. import { reactive, onBeforeMount } from 'vue';
  28. import { onShow } from '@dcloudio/uni-app';
  29. import sheep from '@/sheep';
  30. import { isEmpty } from 'lodash';
  31. import AreaApi from '@/sheep/api/system/area';
  32. import AddressApi from '@/sheep/api/member/address';
  33. const state = reactive({
  34. list: [], // 地址列表
  35. loading: true,
  36. });
  37. // 选择收货地址
  38. const onSelect = (addressInfo) => {
  39. uni.$emit('SELECT_ADDRESS', {
  40. addressInfo,
  41. });
  42. sheep.$router.back();
  43. };
  44. // 导入微信地址
  45. // TODO 芋艿:未测试
  46. function importWechatAddress() {
  47. let wechatAddress = {};
  48. // #ifdef MP
  49. uni.chooseAddress({
  50. success: (res) => {
  51. wechatAddress = {
  52. consignee: res.userName,
  53. mobile: res.telNumber,
  54. province_name: res.provinceName,
  55. city_name: res.cityName,
  56. district_name: res.countyName,
  57. address: res.detailInfo,
  58. region: '',
  59. is_default: false,
  60. };
  61. if (!isEmpty(wechatAddress)) {
  62. sheep.$router.go('/pages/user/address/edit', {
  63. data: JSON.stringify(wechatAddress),
  64. });
  65. }
  66. },
  67. fail: (err) => {
  68. console.log('%cuni.chooseAddress,调用失败', 'color:green;background:yellow');
  69. },
  70. });
  71. // #endif
  72. // #ifdef H5
  73. sheep.$platform.useProvider('wechat').jssdk.openAddress({
  74. success: (res) => {
  75. wechatAddress = {
  76. consignee: res.userName,
  77. mobile: res.telNumber,
  78. province_name: res.provinceName,
  79. city_name: res.cityName,
  80. district_name: res.countryName,
  81. address: res.detailInfo,
  82. region: '',
  83. is_default: false,
  84. };
  85. if (!isEmpty(wechatAddress)) {
  86. sheep.$router.go('/pages/user/address/edit', {
  87. data: JSON.stringify(wechatAddress),
  88. });
  89. }
  90. },
  91. });
  92. // #endif
  93. }
  94. onShow(async () => {
  95. state.list = (await AddressApi.getAddressList()).data;
  96. state.loading = false;
  97. });
  98. onBeforeMount(() => {
  99. if (!!uni.getStorageSync('areaData')) {
  100. return;
  101. }
  102. // 提前加载省市区数据
  103. AreaApi.getAreaTree().then((res) => {
  104. if (res.code === 0) {
  105. uni.setStorageSync('areaData', res.data);
  106. }
  107. });
  108. });
  109. </script>
  110. <style lang="scss" scoped>
  111. .footer-box {
  112. .add-btn {
  113. flex: 1;
  114. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  115. border-radius: 80rpx;
  116. font-size: 30rpx;
  117. font-weight: 500;
  118. line-height: 80rpx;
  119. color: $white;
  120. position: relative;
  121. z-index: 1;
  122. }
  123. .sync-wxaddress {
  124. flex: 1;
  125. line-height: 80rpx;
  126. background: $white;
  127. border-radius: 80rpx;
  128. font-size: 30rpx;
  129. font-weight: 500;
  130. color: $dark-6;
  131. margin-right: 18rpx;
  132. }
  133. }
  134. </style>