list.vue 3.5 KB

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