index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view>
  3. <view class="address-window" :class="address.address == true ? 'on' : ''">
  4. <view class='title'>
  5. <view class=""></view>
  6. <view class="">选择地址</view>
  7. <image @tap='close' src="/static/images/shop/close@2x.png" mode=""></image>
  8. </view>
  9. <view class='list'>
  10. <view class='item' :class='active == index ? "active" : ""'
  11. v-for="(item, index) in addressList" @tap='tapAddress(index, item.id)' :key='index'>
  12. <view class='address'>
  13. <view class='address-t'><text>{{ item.province }}{{ item.city }}{{ item.district }}{{ item.detail }}</text><view v-if="item.isDefault" class="default">默认</view></view>
  14. <view class='name'>{{ item.realName }}<text class='phone'>{{
  15. item.phone }}</text></view>
  16. </view>
  17. </view>
  18. </view>
  19. <!-- 无地址 -->
  20. <view class='pictrue' v-if="!is_loading && !addressList.length">
  21. <image src='/static/images/noAddress.png'></image>
  22. </view>
  23. <view class="addressBnt-btn">
  24. <view class='addressBnt' @tap='goAddressPages'>添加地址</view>
  25. </view>
  26. </view>
  27. <view class='mask' catchtouchmove="true" :hidden='address.address == false' @tap='close'></view>
  28. </view>
  29. </template>
  30. <script setup>
  31. import { ref } from 'vue';
  32. import { getAddressList } from '@/api/user.js';
  33. const props = defineProps({
  34. pagesUrl: {
  35. type: String,
  36. default: '',
  37. },
  38. address: {
  39. type: Object,
  40. default: () => ({
  41. address: true,
  42. addressId: 0,
  43. }),
  44. },
  45. isLog: {
  46. type: Boolean,
  47. default: false,
  48. }
  49. });
  50. const emit = defineEmits(['OnChangeAddress', 'changeClose', 'changeTextareaStatus', 'OnDefaultAddress']);
  51. const active = ref(0);
  52. const is_loading = ref(true);
  53. const addressList = ref([]);
  54. // 对外暴露获取地址的方法
  55. defineExpose({
  56. fetchAddressList
  57. });
  58. function tapAddress(e, addressid) {
  59. active.value = e;
  60. let a = {};
  61. for (let i = 0, leng = addressList.value.length; i < leng; i++) {
  62. if (addressList.value[i].id == addressid) {
  63. a = addressList.value[i];
  64. }
  65. }
  66. emit('OnChangeAddress', a);
  67. }
  68. function close() {
  69. emit('changeClose');
  70. emit('changeTextareaStatus');
  71. }
  72. function goAddressPages() {
  73. emit('changeClose');
  74. emit('changeTextareaStatus');
  75. uni.navigateTo({
  76. url: props.pagesUrl
  77. });
  78. }
  79. function fetchAddressList() {
  80. getAddressList({
  81. page: 1,
  82. limit: 5
  83. }).then(res => {
  84. let list = res.data.list;
  85. addressList.value = list;
  86. is_loading.value = false;
  87. let defaultAddress = {};
  88. // 处理默认选中项
  89. if (!props.address.addressId) return;
  90. for (let i = 0, leng = list.length; i < leng; i++) {
  91. if (list[i].id == props.address.addressId) {
  92. active.value = i;
  93. defaultAddress = list[i];
  94. }
  95. }
  96. emit('OnDefaultAddress', defaultAddress);
  97. }).catch((error) => {
  98. console.error('fetchAddressList', error)
  99. })
  100. }
  101. // 监听 address.address 打开时刷新列表
  102. // watch(() => props.address.address, (val) => {
  103. // if (val) {
  104. // fetchAddressListFn();
  105. // }
  106. // }, { immediate: true });
  107. </script>
  108. <style scoped lang="scss">
  109. .address-window {
  110. background: #FFFFFF;
  111. border-radius: 40rpx 40rpx 0 0;
  112. position: fixed;
  113. bottom: 0;
  114. left: 0;
  115. width: 100%;
  116. z-index: 101;
  117. transform: translate3d(0, 100%, 0);
  118. transition: all .3s cubic-bezier(.25, .5, .5, .9);
  119. }
  120. .address-window.on {
  121. transform: translate3d(0, 0, 0);
  122. }
  123. .address-window .title {
  124. color: #333333;
  125. font-size: 36rpx;
  126. font-weight: bold;
  127. height: 100rpx;
  128. padding: 0 20rpx;
  129. display: flex;
  130. align-items: center;
  131. justify-content: space-between;
  132. image,view:first-child{
  133. width: 32rpx;
  134. height: 32rpx;
  135. }
  136. }
  137. .address-window .list {
  138. padding: 0 32rpx;
  139. }
  140. .address-window .list .item {
  141. display: flex;
  142. align-items: center;
  143. justify-content: space-between;
  144. background: #F9F7F0;
  145. border-radius: 16rpx;
  146. border-bottom: 2px solid transparent;
  147. padding: 16rpx;
  148. margin-bottom: 24rpx;
  149. &:last-child {
  150. margin-bottom: 0;
  151. }
  152. }
  153. .address-window .list .active {
  154. background: rgba(248,192,8,0.1);
  155. border: 2rpx solid #F8C008;
  156. }
  157. .address-window .list .item .address {
  158. flex: 1;
  159. margin-right: 16rpx;
  160. }
  161. .default {
  162. width: 64rpx;
  163. height: 40rpx;
  164. color: #333333;
  165. font-size: 24rpx;
  166. background: #F8C008;
  167. border-radius: 8rpx;
  168. text-align: center;
  169. line-height: 40rpx;
  170. flex-shrink: 0;
  171. }
  172. .address-window .list .item .address .name {
  173. font-size: 28rpx;
  174. color: #666666;
  175. margin-top: 16rpx;
  176. }
  177. .address-window .list .item .address .name .phone {
  178. margin-left: 18rpx;
  179. }
  180. .address-t {
  181. font-weight: bold;
  182. display: flex;
  183. justify-content: space-between;
  184. align-items: start;
  185. flex-wrap: nowrap;
  186. }
  187. .addressBnt-btn {
  188. padding: 22rpx 32rpx;
  189. }
  190. .address-window .addressBnt {
  191. font-size: 32rpx;
  192. font-weight: bold;
  193. color: #333333;
  194. text-align: center;
  195. line-height: 88rpx;
  196. background: #F8C008;
  197. border-radius: 16rpx;
  198. }
  199. .address-window .pictrue {
  200. width: 414rpx;
  201. height: 336rpx;
  202. margin: 0 auto;
  203. }
  204. .address-window .pictrue image {
  205. width: 100%;
  206. height: 100%;
  207. }
  208. </style>