index.vue 5.3 KB

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