list.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // 选择收货地址, 有h5端需返回两次的bug
  49. const onSelect = (addressInfo) => {
  50. // const pages = sheep.$router.getCur();
  51. // const lastPage = pages[pages.length - 2];
  52. // if (!_.isEmpty(lastPage) && lastPage.route === "pages/order/confirm") {
  53. // sheep.$router.back();
  54. // uni.navigateBack();
  55. uni.$emit('SELECT_ADDRESS', {
  56. addressInfo,
  57. });
  58. sheep.$router.back();
  59. // }
  60. };
  61. // 导入微信地址
  62. function importWechatAddress() {
  63. let wechatAddress = {};
  64. // #ifdef MP
  65. uni.chooseAddress({
  66. success: (res) => {
  67. wechatAddress = {
  68. consignee: res.userName,
  69. mobile: res.telNumber,
  70. province_name: res.provinceName,
  71. city_name: res.cityName,
  72. district_name: res.countyName,
  73. address: res.detailInfo,
  74. region: '',
  75. is_default: false,
  76. };
  77. if (!isEmpty(wechatAddress)) {
  78. sheep.$router.go('/pages/user/address/edit', {
  79. data: JSON.stringify(wechatAddress),
  80. });
  81. }
  82. },
  83. fail: (err) => {
  84. console.log('%cuni.chooseAddress,调用失败', 'color:green;background:yellow');
  85. },
  86. });
  87. // #endif
  88. // #ifdef H5
  89. sheep.$platform.useProvider('wechat').jssdk.openAddress({
  90. success: (res) => {
  91. wechatAddress = {
  92. consignee: res.userName,
  93. mobile: res.telNumber,
  94. province_name: res.provinceName,
  95. city_name: res.cityName,
  96. district_name: res.countryName,
  97. address: res.detailInfo,
  98. region: '',
  99. is_default: false,
  100. };
  101. if (!isEmpty(wechatAddress)) {
  102. sheep.$router.go('/pages/user/address/edit', {
  103. data: JSON.stringify(wechatAddress),
  104. });
  105. }
  106. },
  107. });
  108. // #endif
  109. }
  110. onShow(async () => {
  111. state.list = (await sheep.$api.user.address.list()).data;
  112. state.loading = false;
  113. });
  114. onBeforeMount(() => {
  115. if (!!uni.getStorageSync('areaData')) {
  116. return;
  117. }
  118. // 提前加载省市区数据
  119. sheep.$api.data.area().then((res) => {
  120. if (res.error === 0) {
  121. uni.setStorageSync('areaData', res.data);
  122. }
  123. });
  124. });
  125. </script>
  126. <style lang="scss" scoped>
  127. .footer-box {
  128. .add-btn {
  129. flex: 1;
  130. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  131. border-radius: 80rpx;
  132. font-size: 30rpx;
  133. font-weight: 500;
  134. line-height: 80rpx;
  135. color: $white;
  136. position: relative;
  137. z-index: 1;
  138. }
  139. .sync-wxaddress {
  140. flex: 1;
  141. line-height: 80rpx;
  142. background: $white;
  143. border-radius: 80rpx;
  144. font-size: 30rpx;
  145. font-weight: 500;
  146. color: $dark-6;
  147. margin-right: 18rpx;
  148. }
  149. }
  150. </style>