list.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <view class="address-container">
  3. <u-empty v-if="addressList.length === 0" mode="address" icon="/static/images/empty/address.png">
  4. <view class="empty-tips">暂无收货地址</view>
  5. </u-empty>
  6. <view v-else class="address-list">
  7. <view v-for="(item, index) in addressList" :key="index" class="address-item">
  8. <view class="address-info" @click="selectAddress(item)">
  9. <view class="user-info">
  10. <text class="name">{{item.name}}</text>
  11. <text class="phone">{{item.phone}}</text>
  12. <text v-if="item.isDefault" class="default-tag">默认</text>
  13. </view>
  14. <view class="address-detail">{{item.province}}{{item.city}}{{item.district}}{{item.address}}</view>
  15. </view>
  16. <view class="operation">
  17. <view class="edit" @click="editAddress(item)">
  18. <u-icon name="edit-pen" size="40rpx"></u-icon>
  19. </view>
  20. <view class="delete" @click="deleteAddress(item)">
  21. <u-icon name="trash" size="40rpx"></u-icon>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. <view class="add-btn">
  27. <u-button type="primary" text="新增收货地址" @click="addAddress" color="#F95B5B"></u-button>
  28. </view>
  29. <u-modal
  30. :show="showDeleteModal"
  31. @confirm="confirmDelete"
  32. @cancel="showDeleteModal = false"
  33. title="删除确认"
  34. content="确定要删除该收货地址吗?"
  35. showCancelButton>
  36. </u-modal>
  37. </view>
  38. </template>
  39. <script>
  40. import { addressList, addressDel } from '../../../config/api.js';
  41. export default {
  42. data() {
  43. return {
  44. addressList: [],
  45. showDeleteModal: false,
  46. currentAddress: null,
  47. isSelect: false
  48. }
  49. },
  50. onLoad(options) {
  51. if (options.select) {
  52. this.isSelect = true
  53. }
  54. },
  55. onShow() {
  56. this.getAddressList()
  57. },
  58. methods: {
  59. async getAddressList() {
  60. try {
  61. const res = await addressList()
  62. if (res.code === 200) {
  63. this.addressList = res.data || []
  64. }
  65. } catch (e) {
  66. uni.$u.toast('获取地址列表失败')
  67. }
  68. },
  69. addAddress() {
  70. uni.navigateTo({
  71. url: '/packageUser/pages/address/edit'
  72. })
  73. },
  74. editAddress(item) {
  75. uni.navigateTo({
  76. url: '/packageUser/pages/address/edit?id=' + item.id
  77. })
  78. },
  79. deleteAddress(item) {
  80. this.currentAddress = item
  81. this.showDeleteModal = true
  82. },
  83. async confirmDelete() {
  84. if (!this.currentAddress) return
  85. try {
  86. const res = await addressDel({
  87. ids: this.currentAddress.id
  88. })
  89. if (res.code === 200) {
  90. uni.$u.toast('删除成功')
  91. this.getAddressList()
  92. }
  93. } catch (e) {
  94. uni.$u.toast('删除失败')
  95. }
  96. this.showDeleteModal = false
  97. this.currentAddress = null
  98. },
  99. selectAddress(item) {
  100. if (!this.isSelect) return
  101. const pages = getCurrentPages()
  102. const prevPage = pages[pages.length - 2]
  103. if (prevPage && prevPage.$vm.setSelectedAddress) {
  104. prevPage.$vm.setSelectedAddress(item)
  105. uni.navigateBack()
  106. }
  107. }
  108. }
  109. }
  110. </script>
  111. <style lang="scss">
  112. .address-container {
  113. min-height: 100vh;
  114. background: #f5f5f5;
  115. padding-bottom: 120rpx;
  116. .address-list {
  117. padding: 20rpx;
  118. .address-item {
  119. background: #fff;
  120. border-radius: 12rpx;
  121. padding: 30rpx;
  122. margin-bottom: 20rpx;
  123. .address-info {
  124. .user-info {
  125. margin-bottom: 16rpx;
  126. .name {
  127. font-size: 32rpx;
  128. font-weight: bold;
  129. margin-right: 20rpx;
  130. }
  131. .phone {
  132. font-size: 28rpx;
  133. color: #666;
  134. }
  135. .default-tag {
  136. background: #F95B5B;
  137. color: #fff;
  138. font-size: 20rpx;
  139. padding: 4rpx 12rpx;
  140. border-radius: 4rpx;
  141. margin-left: 20rpx;
  142. }
  143. }
  144. .address-detail {
  145. font-size: 28rpx;
  146. color: #333;
  147. line-height: 1.4;
  148. }
  149. }
  150. .operation {
  151. display: flex;
  152. justify-content: flex-end;
  153. margin-top: 20rpx;
  154. border-top: 1rpx solid #eee;
  155. padding-top: 20rpx;
  156. .edit, .delete {
  157. padding: 10rpx 20rpx;
  158. }
  159. }
  160. }
  161. }
  162. .add-btn {
  163. position: fixed;
  164. bottom: 0;
  165. left: 0;
  166. right: 0;
  167. padding: 20rpx 40rpx;
  168. background: #fff;
  169. }
  170. .empty-tips {
  171. font-size: 28rpx;
  172. color: #999;
  173. margin-top: 20rpx;
  174. }
  175. }
  176. </style>