list.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 { onBeforeMount, reactive } from 'vue';
  41. import { onShow, onLoad } 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. openType: '', // 页面打开类型
  50. });
  51. // 选择收货地址
  52. const onSelect = (addressInfo) => {
  53. if (state.openType !== 'select'){ // 不作为选择组件时阻断操作
  54. return
  55. }
  56. uni.$emit('SELECT_ADDRESS', {
  57. addressInfo,
  58. });
  59. sheep.$router.back();
  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. onLoad((option) => {
  111. if (option.type) {
  112. state.openType = option.type;
  113. }
  114. });
  115. onShow(async () => {
  116. state.list = (await AddressApi.getAddressList()).data;
  117. state.loading = false;
  118. });
  119. onBeforeMount(() => {
  120. if (!!uni.getStorageSync('areaData')) {
  121. return;
  122. }
  123. // 提前加载省市区数据
  124. AreaApi.getAreaTree().then((res) => {
  125. if (res.code === 0) {
  126. uni.setStorageSync('areaData', res.data);
  127. }
  128. });
  129. });
  130. </script>
  131. <style lang="scss" scoped>
  132. .footer-box {
  133. .add-btn {
  134. flex: 1;
  135. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  136. border-radius: 80rpx;
  137. font-size: 30rpx;
  138. font-weight: 500;
  139. line-height: 80rpx;
  140. color: $white;
  141. position: relative;
  142. z-index: 1;
  143. }
  144. .sync-wxaddress {
  145. flex: 1;
  146. line-height: 80rpx;
  147. background: $white;
  148. border-radius: 80rpx;
  149. font-size: 30rpx;
  150. font-weight: 500;
  151. color: $dark-6;
  152. margin-right: 18rpx;
  153. }
  154. }
  155. </style>