list.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <!-- 收件地址列表 -->
  2. <template>
  3. <s-layout title="收货地址" :bgStyle="{ color: '#FFF' }">
  4. <view v-if="state.list.length">
  5. <s-address-item
  6. hasBorderBottom
  7. v-for="item in state.list"
  8. :key="item.id"
  9. :item="item"
  10. @tap="onSelect(item)"
  11. />
  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-es';
  44. import AreaApi from '@/sheep/api/system/area';
  45. import AddressApi from '@/sheep/api/member/address';
  46. const state = reactive({
  47. list: [], // 地址列表
  48. loading: true,
  49. });
  50. // 选择收货地址
  51. const onSelect = (addressInfo) => {
  52. uni.$emit('SELECT_ADDRESS', {
  53. addressInfo,
  54. });
  55. sheep.$router.back();
  56. };
  57. // 导入微信地址
  58. // TODO 芋艿:未测试
  59. function importWechatAddress() {
  60. let wechatAddress = {};
  61. // #ifdef MP
  62. uni.chooseAddress({
  63. success: (res) => {
  64. wechatAddress = {
  65. consignee: res.userName,
  66. mobile: res.telNumber,
  67. province_name: res.provinceName,
  68. city_name: res.cityName,
  69. district_name: res.countyName,
  70. address: res.detailInfo,
  71. region: '',
  72. is_default: false,
  73. };
  74. if (!isEmpty(wechatAddress)) {
  75. sheep.$router.go('/pages/user/address/edit', {
  76. data: JSON.stringify(wechatAddress),
  77. });
  78. }
  79. },
  80. fail: (err) => {
  81. console.log('%cuni.chooseAddress,调用失败', 'color:green;background:yellow');
  82. },
  83. });
  84. // #endif
  85. // #ifdef H5
  86. sheep.$platform.useProvider('wechat').jssdk.openAddress({
  87. success: (res) => {
  88. wechatAddress = {
  89. consignee: res.userName,
  90. mobile: res.telNumber,
  91. province_name: res.provinceName,
  92. city_name: res.cityName,
  93. district_name: res.countryName,
  94. address: res.detailInfo,
  95. region: '',
  96. is_default: false,
  97. };
  98. if (!isEmpty(wechatAddress)) {
  99. sheep.$router.go('/pages/user/address/edit', {
  100. data: JSON.stringify(wechatAddress),
  101. });
  102. }
  103. },
  104. });
  105. // #endif
  106. }
  107. onShow(async () => {
  108. state.list = (await AddressApi.getAddressList()).data;
  109. state.loading = false;
  110. });
  111. onBeforeMount(() => {
  112. if (!!uni.getStorageSync('areaData')) {
  113. return;
  114. }
  115. // 提前加载省市区数据
  116. AreaApi.getAreaTree().then((res) => {
  117. if (res.code === 0) {
  118. uni.setStorageSync('areaData', res.data);
  119. }
  120. });
  121. });
  122. </script>
  123. <style lang="scss" scoped>
  124. .footer-box {
  125. .add-btn {
  126. flex: 1;
  127. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  128. border-radius: 80rpx;
  129. font-size: 30rpx;
  130. font-weight: 500;
  131. line-height: 80rpx;
  132. color: $white;
  133. position: relative;
  134. z-index: 1;
  135. }
  136. .sync-wxaddress {
  137. flex: 1;
  138. line-height: 80rpx;
  139. background: $white;
  140. border-radius: 80rpx;
  141. font-size: 30rpx;
  142. font-weight: 500;
  143. color: $dark-6;
  144. margin-right: 18rpx;
  145. }
  146. }
  147. </style>