address_list.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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"
  7. :address="item" :is-default="item.isDefault" @set-default="handleSetDefault(index)"
  8. @edit="handleEdit(index)" @delete="handleDelete(index)" @click="onSelectAddress(item)" />
  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 {
  35. onLoad,
  36. onShow,
  37. onPullDownRefresh,
  38. onReachBottom
  39. } from '@dcloudio/uni-app'
  40. import AddressItem from '@/components/AddressItem.vue'
  41. import {
  42. listBook,
  43. delBook
  44. } from '../../api/address'
  45. const pageNum = ref(1)
  46. const pageSize = ref(10)
  47. const recordTotal = ref(0)
  48. const loadState = ref(false)
  49. const loadFinished = ref(false)
  50. // 模拟地址数据
  51. const addressList = ref([])
  52. const pages = ref(0) //数据总条数
  53. // 当前要操作的地址索引
  54. const currentIndex = ref(-1)
  55. const addType = ref('')
  56. onLoad((option) => {
  57. addType.value = option.addType
  58. })
  59. onShow(() => {
  60. pageNum.value = 1
  61. getAddressList()
  62. })
  63. // 下拉刷新
  64. onPullDownRefresh(() => {
  65. pageNum.value = 1
  66. getAddressList()
  67. })
  68. // 触底加载更多
  69. onReachBottom(() => {
  70. if (pageNum.value < pages.value) {
  71. pageNum.value++;
  72. getAddressList()
  73. }
  74. })
  75. const onSelectAddress = (address) => {
  76. console.log('-------address---',address)
  77. if (!addType.value) {
  78. return
  79. }
  80. uni.$emit('addressSelected', {
  81. type:addType.value, // 'sender' 或 'receiver'
  82. address // 选中的地址对象
  83. })
  84. uni.navigateBack() // 返回上一页
  85. }
  86. // 设置默认地址
  87. const handleSetDefault = (index) => {
  88. addressList.value.forEach((item, i) => {
  89. item.isDefault = i === index
  90. })
  91. }
  92. // 编辑地址
  93. const handleEdit = (index) => {
  94. // 这里可以跳转到编辑页面
  95. uni.navigateTo({
  96. url: '/pages/address/edit?id=' + addressList.value[index].addressId
  97. })
  98. }
  99. // 删除地址
  100. const handleDelete = (index) => {
  101. currentIndex.value = index
  102. uni.showModal({
  103. title: '确认删除',
  104. content: '是否确认删除这个地址',
  105. // showCancel:false,
  106. success: async (res) => {
  107. if (res.confirm) {
  108. let res = await delBook(addressList.value[index].addressId)
  109. if (res.code == 200) {
  110. addressList.value.splice(currentIndex.value, 1)
  111. currentIndex.value = -1
  112. uni.showToast({
  113. title: '删除成功',
  114. icon: 'success'
  115. })
  116. }
  117. } else if (res.cancel) {
  118. console.log('用户点击取消');
  119. }
  120. }
  121. });
  122. }
  123. // 添加新地址
  124. const handleAddAddress = () => {
  125. uni.navigateTo({
  126. url: '/pages/address/edit'
  127. })
  128. }
  129. const loadMore = () => {
  130. pageNum.value++
  131. getAddressList(true)
  132. }
  133. // 获取地址列表
  134. const getAddressList = () => {
  135. const params = {
  136. pageNum: pageNum.value,
  137. pageSize: pageSize.value
  138. }
  139. uni.showLoading({
  140. mask: true
  141. })
  142. listBook(params).then(res => {
  143. uni.hideLoading()
  144. uni.stopPullDownRefresh()
  145. if (res.code === 200) {
  146. const list = res.rows || []
  147. addressList.value = pageNum.value == 1 ? list : [...addressList.value, ...list]
  148. pages.value = Math.ceil(res.total / pageSize.value) || 0
  149. }
  150. }, err => {
  151. uni.hideLoading()
  152. })
  153. }
  154. </script>
  155. <style scoped>
  156. .address-page {
  157. display: flex;
  158. flex-direction: column;
  159. height: 100vh;
  160. background-color: #F5F7FA;
  161. }
  162. .address-list {
  163. padding: 20rpx 30rpx 152rpx;
  164. box-sizing: border-box;
  165. }
  166. .load-more-status {
  167. text-align: center;
  168. padding: 20rpx;
  169. font-size: 24rpx;
  170. color: #999;
  171. }
  172. .empty-state {
  173. display: flex;
  174. flex-direction: column;
  175. align-items: center;
  176. justify-content: center;
  177. padding: 120rpx 40rpx;
  178. color: #999999;
  179. }
  180. .empty-text {
  181. margin-top: 20rpx;
  182. font-size: 28rpx;
  183. }
  184. .add-btn-container {
  185. width: 100%;
  186. position: fixed;
  187. bottom: 0rpx;
  188. padding: 32rpx;
  189. background-color: #fff;
  190. border-top: 1rpx solid #eee;
  191. }
  192. .add-btn {
  193. height: 88rpx;
  194. background: #1B64F0;
  195. border-radius: 44rpx;
  196. }
  197. .add-btn-text {
  198. color: #fff;
  199. font-size: 32rpx;
  200. font-weight: 500;
  201. height: 88rpx;
  202. line-height: 88rpx;
  203. }
  204. /* 按钮激活效果 */
  205. .add-btn:active {
  206. opacity: 0.8;
  207. }
  208. </style>