address_list.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view class="address-page">
  3. <!-- 地址列表 -->
  4. <view class="address-list">
  5. <!-- 地址项组件 -->
  6. <address-item v-if="addressList.length > 0" v-for="(item, index) in addressList" :key="item.id" :address="item"
  7. :is-default="item.isDefault" @set-default="handleSetDefault(index)" @edit="handleEdit(index)"
  8. @delete="handleDelete(index)" />
  9. <!-- 空状态 -->
  10. <view class="empty-state" v-if="addressList.length === 0 && !loadState">
  11. <u-icon class="empty-icon" name="list" size="60" color="#ccc"></u-icon>
  12. <text class="empty-text">暂无数据</text>
  13. </view>
  14. <!-- 加载状态提示 -->
  15. <view class="load-more-status" v-if="loadState">
  16. <text>加载中...</text>
  17. </view>
  18. <view class="load-more-status" v-if="loadFinished && addressList.length !== 0">
  19. <text>没有更多数据了</text>
  20. </view>
  21. </view>
  22. <!-- 添加新地址按钮 -->
  23. <view class="add-btn-container">
  24. <button class="add-btn" @click="handleAddAddress">
  25. <text class="add-btn-text">添加新地址</text>
  26. </button>
  27. </view>
  28. </view>
  29. </template>
  30. <script setup>
  31. import {
  32. ref
  33. } from 'vue'
  34. import { onShow,onPullDownRefresh,onReachBottom } from '@dcloudio/uni-app'
  35. import AddressItem from '@/components/AddressItem.vue'
  36. import { listBook,delBook } from '../../api/address'
  37. const pageNum = ref(1)
  38. const pageSize = ref(10)
  39. const recordTotal = ref(0)
  40. const loadState = ref(false)
  41. const loadFinished = ref(false)
  42. // 模拟地址数据
  43. const addressList = ref([])
  44. const pages = ref(0)//数据总条数
  45. // 当前要操作的地址索引
  46. const currentIndex = ref(-1)
  47. onShow(() => {
  48. pageNum.value = 1
  49. getAddressList()
  50. })
  51. // 下拉刷新
  52. onPullDownRefresh(()=>{
  53. pageNum.value = 1
  54. getAddressList()
  55. })
  56. // 触底加载更多
  57. onReachBottom(()=>{
  58. if (pageNum.value < pages.value) {
  59. pageNum.value++;
  60. getAddressList()
  61. }
  62. })
  63. // 设置默认地址
  64. const handleSetDefault = (index) => {
  65. addressList.value.forEach((item, i) => {
  66. item.isDefault = i === index
  67. })
  68. }
  69. // 编辑地址
  70. const handleEdit = (index) => {
  71. // 这里可以跳转到编辑页面
  72. uni.navigateTo({ url: '/pages/address/edit?id=' + addressList.value[index].addressId })
  73. }
  74. // 删除地址
  75. const handleDelete = (index) => {
  76. currentIndex.value = index
  77. uni.showModal({
  78. title: '确认删除',
  79. content: '是否确认删除这个地址',
  80. // showCancel:false,
  81. success: async (res) => {
  82. if (res.confirm) {
  83. let res = await delBook(addressList.value[index].addressId)
  84. if (res.code == 200) {
  85. addressList.value.splice(currentIndex.value, 1)
  86. currentIndex.value = -1
  87. uni.showToast({
  88. title:'删除成功',
  89. icon:'success'
  90. })
  91. }
  92. } else if (res.cancel) {
  93. console.log('用户点击取消');
  94. }
  95. }
  96. });
  97. }
  98. // 添加新地址
  99. const handleAddAddress = () => {
  100. uni.navigateTo({
  101. url: '/pages/address/edit'
  102. })
  103. }
  104. const loadMore = () => {
  105. pageNum.value++
  106. getAddressList(true)
  107. }
  108. // 获取地址列表
  109. const getAddressList = () => {
  110. const params = {
  111. pageNum: pageNum.value,
  112. pageSize: pageSize.value
  113. }
  114. uni.showLoading({mask:true})
  115. listBook(params).then(res => {
  116. uni.hideLoading()
  117. uni.stopPullDownRefresh()
  118. if (res.code === 200) {
  119. const list = res.rows || []
  120. addressList.value = pageNum.value == 1 ? list : [...addressList.value,...list]
  121. pages.value = Math.ceil(res.total / pageSize.value) || 0
  122. }
  123. }, err => {
  124. uni.hideLoading()
  125. })
  126. }
  127. </script>
  128. <style scoped>
  129. .address-page {
  130. display: flex;
  131. flex-direction: column;
  132. height: 100vh;
  133. background-color: #F5F7FA;
  134. }
  135. .address-list {
  136. padding: 20rpx 30rpx 152rpx ;
  137. box-sizing: border-box;
  138. }
  139. .load-more-status {
  140. text-align: center;
  141. padding: 20rpx;
  142. font-size: 24rpx;
  143. color: #999;
  144. }
  145. .empty-state {
  146. display: flex;
  147. flex-direction: column;
  148. align-items: center;
  149. justify-content: center;
  150. padding: 120rpx 40rpx;
  151. color: #999999;
  152. }
  153. .empty-text {
  154. margin-top: 20rpx;
  155. font-size: 28rpx;
  156. }
  157. .add-btn-container {
  158. width: 100%;
  159. position: fixed;
  160. bottom: 0rpx;
  161. padding: 32rpx;
  162. background-color: #fff;
  163. border-top: 1rpx solid #eee;
  164. }
  165. .add-btn {
  166. height: 88rpx;
  167. background: #1B64F0;
  168. border-radius: 44rpx;
  169. }
  170. .add-btn-text {
  171. color: #fff;
  172. font-size: 32rpx;
  173. font-weight: 500;
  174. height: 88rpx;
  175. line-height: 88rpx;
  176. }
  177. /* 按钮激活效果 */
  178. .add-btn:active {
  179. opacity: 0.8;
  180. }
  181. </style>