index.vue 4.8 KB

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