list.vue 3.9 KB

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