list.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // TODO 芋艿:未测试
  63. function importWechatAddress() {
  64. let wechatAddress = {};
  65. // #ifdef MP
  66. uni.chooseAddress({
  67. success: (res) => {
  68. wechatAddress = {
  69. consignee: res.userName,
  70. mobile: res.telNumber,
  71. province_name: res.provinceName,
  72. city_name: res.cityName,
  73. district_name: res.countyName,
  74. address: res.detailInfo,
  75. region: '',
  76. is_default: false,
  77. };
  78. if (!isEmpty(wechatAddress)) {
  79. sheep.$router.go('/pages/user/address/edit', {
  80. data: JSON.stringify(wechatAddress),
  81. });
  82. }
  83. },
  84. fail: (err) => {
  85. console.log('%cuni.chooseAddress,调用失败', 'color:green;background:yellow');
  86. },
  87. });
  88. // #endif
  89. // #ifdef H5
  90. sheep.$platform.useProvider('wechat').jssdk.openAddress({
  91. success: (res) => {
  92. wechatAddress = {
  93. consignee: res.userName,
  94. mobile: res.telNumber,
  95. province_name: res.provinceName,
  96. city_name: res.cityName,
  97. district_name: res.countryName,
  98. address: res.detailInfo,
  99. region: '',
  100. is_default: false,
  101. };
  102. if (!isEmpty(wechatAddress)) {
  103. sheep.$router.go('/pages/user/address/edit', {
  104. data: JSON.stringify(wechatAddress),
  105. });
  106. }
  107. },
  108. });
  109. // #endif
  110. }
  111. onLoad((option) => {
  112. if (option.type) {
  113. state.openType = option.type;
  114. }
  115. });
  116. onShow(async () => {
  117. state.list = (await AddressApi.getAddressList()).data;
  118. state.loading = false;
  119. });
  120. onBeforeMount(() => {
  121. if (!!uni.getStorageSync('areaData')) {
  122. return;
  123. }
  124. // 提前加载省市区数据
  125. AreaApi.getAreaTree().then((res) => {
  126. if (res.code === 0) {
  127. uni.setStorageSync('areaData', res.data);
  128. }
  129. });
  130. });
  131. </script>
  132. <style lang="scss" scoped>
  133. .footer-box {
  134. .add-btn {
  135. flex: 1;
  136. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  137. border-radius: 80rpx;
  138. font-size: 30rpx;
  139. font-weight: 500;
  140. line-height: 80rpx;
  141. color: $white;
  142. position: relative;
  143. z-index: 1;
  144. }
  145. .sync-wxaddress {
  146. flex: 1;
  147. line-height: 80rpx;
  148. background: $white;
  149. border-radius: 80rpx;
  150. font-size: 30rpx;
  151. font-weight: 500;
  152. color: $dark-6;
  153. margin-right: 18rpx;
  154. }
  155. }
  156. </style>