address_list.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <view class="address-page">
  3. <!-- 搜索框(原生 input 实现,兼容微信小程序) -->
  4. <view class="search-section">
  5. <view class="search-box">
  6. <!-- 搜索图标 -->
  7. <view class="search-icon">
  8. <u-icon name="search" size="22" color="#999"></u-icon>
  9. </view>
  10. <!-- 输入框 -->
  11. <input
  12. class="search-input"
  13. type="text"
  14. v-model="searchKeyword"
  15. placeholder="请输入姓名/手机号/地址"
  16. placeholder-class="placeholder-style"
  17. confirm-type="search"
  18. @confirm="handleSearch"
  19. />
  20. <!-- 自定义清除按钮 -->
  21. <view v-if="searchKeyword" class="search-clear" @click.stop="handleClear">
  22. <u-icon name="close-circle" size="22" color="#999"></u-icon>
  23. </view>
  24. </view>
  25. </view>
  26. <!-- 地址列表 -->
  27. <view class="address-list">
  28. <!-- 地址项组件 -->
  29. <address-item v-if="addressList.length > 0" v-for="(item, index) in addressList" :key="item.id"
  30. :address="item" :is-default="item.isDefault" @set-default="handleSetDefault(index)"
  31. @edit="handleEdit(index)" @delete="handleDelete(index)" @click="onSelectAddress(item)" />
  32. <!-- 空状态 -->
  33. <view class="empty-state" v-if="addressList.length === 0 && !loadState">
  34. <u-icon class="empty-icon" name="list" size="60" color="#ccc"></u-icon>
  35. <text class="empty-text">暂无数据</text>
  36. </view>
  37. <!-- 加载状态提示 -->
  38. <view class="load-more-status" v-if="loadState">
  39. <text>加载中...</text>
  40. </view>
  41. <view class="load-more-status" v-if="loadFinished && addressList.length !== 0">
  42. <text>没有更多数据了</text>
  43. </view>
  44. </view>
  45. <!-- 添加新地址按钮 -->
  46. <view class="add-btn-container">
  47. <button class="add-btn" @click="handleAddAddress">
  48. <text class="add-btn-text">添加新地址</text>
  49. </button>
  50. </view>
  51. </view>
  52. </template>
  53. <script setup>
  54. import {
  55. ref
  56. } from 'vue'
  57. import {
  58. onLoad,
  59. onShow,
  60. onPullDownRefresh,
  61. onReachBottom
  62. } from '@dcloudio/uni-app'
  63. import AddressItem from '@/components/AddressItem.vue'
  64. import {
  65. listBook,
  66. delBook,
  67. updateBook
  68. } from '../../api/address'
  69. const searchKeyword = ref('')
  70. const pageNum = ref(1)
  71. const pageSize = ref(10)
  72. const recordTotal = ref(0)
  73. const loadState = ref(false)
  74. const loadFinished = ref(false)
  75. // 模拟地址数据
  76. const addressList = ref([])
  77. const pages = ref(0) // 总页数
  78. // 当前要操作的地址索引
  79. const currentIndex = ref(-1)
  80. const addType = ref('')
  81. onLoad((option) => {
  82. addType.value = option.addType
  83. })
  84. onShow(() => {
  85. pageNum.value = 1
  86. getAddressList()
  87. })
  88. // 下拉刷新
  89. onPullDownRefresh(() => {
  90. pageNum.value = 1
  91. getAddressList()
  92. })
  93. // 触底加载更多
  94. onReachBottom(() => {
  95. if (pageNum.value < pages.value) {
  96. pageNum.value++;
  97. getAddressList()
  98. }
  99. })
  100. const onSelectAddress = (address) => {
  101. console.log('-------address---', address)
  102. if (!addType.value) {
  103. return
  104. }
  105. uni.$emit('addressSelected', {
  106. type: addType.value, // 'sender' 或 'receiver'
  107. address // 选中的地址对象
  108. })
  109. uni.navigateBack() // 返回上一页
  110. }
  111. // 设置默认地址
  112. const handleSetDefault = async (index) => {
  113. const currentAddress = addressList.value[index];
  114. // 如果已经是默认地址,则无需操作
  115. if (currentAddress.defaultFlag === 1) {
  116. uni.showToast({
  117. title: '已是默认地址',
  118. icon: 'none'
  119. });
  120. return;
  121. }
  122. uni.showLoading({
  123. title: '设置中...',
  124. mask: true
  125. });
  126. try {
  127. // 调用接口设置为默认地址(后端应自动处理其他地址的非默认状态)
  128. const res = await updateBook({
  129. ...currentAddress,
  130. defaultFlag: 1
  131. });
  132. if (res.code === 200) {
  133. // 更新本地数据:将所有地址的 defaultFlag 设为 0,再将当前项设为 1
  134. addressList.value.forEach(item => item.defaultFlag = 0);
  135. currentAddress.defaultFlag = 1;
  136. uni.showToast({
  137. title: '设置成功',
  138. icon: 'success'
  139. });
  140. } else {
  141. uni.showToast({
  142. title: res.msg || '设置失败',
  143. icon: 'none'
  144. });
  145. }
  146. } catch (error) {
  147. uni.showToast({
  148. title: '网络错误,请重试',
  149. icon: 'none'
  150. });
  151. console.error('设置默认地址失败', error);
  152. } finally {
  153. uni.hideLoading();
  154. }
  155. }
  156. // 编辑地址
  157. const handleEdit = (index) => {
  158. // 跳转到编辑页面
  159. uni.navigateTo({
  160. url: '/pages/address/edit?id=' + addressList.value[index].addressId
  161. })
  162. }
  163. // 删除地址
  164. const handleDelete = (index) => {
  165. currentIndex.value = index
  166. uni.showModal({
  167. title: '确认删除',
  168. content: '是否确认删除这个地址',
  169. success: async (res) => {
  170. if (res.confirm) {
  171. uni.showLoading({
  172. title: '删除中...',
  173. mask: true
  174. });
  175. try {
  176. const delRes = await delBook(addressList.value[index].addressId)
  177. if (delRes.code == 200) {
  178. // 移除本地数据
  179. addressList.value.splice(currentIndex.value, 1)
  180. currentIndex.value = -1
  181. // 如果删除的是默认地址,且列表不为空,可考虑自动将第一个地址设为默认(根据业务需求可选)
  182. // 此处不做自动处理,由用户手动设置
  183. uni.showToast({
  184. title: '删除成功',
  185. icon: 'success'
  186. })
  187. } else {
  188. uni.showToast({
  189. title: delRes.msg || '删除失败',
  190. icon: 'none'
  191. })
  192. }
  193. } catch (error) {
  194. uni.showToast({
  195. title: '网络错误',
  196. icon: 'none'
  197. })
  198. } finally {
  199. uni.hideLoading()
  200. }
  201. } else if (res.cancel) {
  202. console.log('用户点击取消');
  203. }
  204. }
  205. });
  206. }
  207. // 添加新地址
  208. const handleAddAddress = () => {
  209. uni.navigateTo({
  210. url: '/pages/address/edit'
  211. })
  212. }
  213. // 搜索(点击键盘搜索或清除后调用)
  214. const handleSearch = () => {
  215. pageNum.value = 1
  216. getAddressList()
  217. }
  218. // 清除按钮点击
  219. const handleClear = () => {
  220. searchKeyword.value = ''
  221. handleSearch()
  222. }
  223. const loadMore = () => {
  224. pageNum.value++
  225. getAddressList(true)
  226. }
  227. // 获取地址列表
  228. const getAddressList = () => {
  229. const params = {
  230. pageNum: pageNum.value,
  231. pageSize: pageSize.value,
  232. searchKeyword:searchKeyword.value
  233. }
  234. uni.showLoading({
  235. mask: true
  236. })
  237. listBook(params).then(res => {
  238. uni.hideLoading()
  239. uni.stopPullDownRefresh()
  240. if (res.code === 200) {
  241. const list = res.rows || []
  242. addressList.value = pageNum.value == 1 ? list : [...addressList.value, ...list]
  243. pages.value = Math.ceil(res.total / pageSize.value) || 0
  244. }
  245. }, err => {
  246. uni.hideLoading()
  247. uni.stopPullDownRefresh()
  248. uni.showToast({
  249. title: '加载失败',
  250. icon: 'none'
  251. })
  252. })
  253. }
  254. </script>
  255. <style scoped lang="less">
  256. .address-page {
  257. display: flex;
  258. flex-direction: column;
  259. height: 100vh;
  260. background-color: #F5F7FA;
  261. }
  262. .search-section {
  263. background-color: #fff;
  264. padding: 16rpx;
  265. margin-left: 14rpx;
  266. }
  267. .search-box {
  268. display: flex;
  269. align-items: center;
  270. background-color: #F5F7FA;
  271. border-radius: 36rpx;
  272. padding: 0 20rpx;
  273. height: 72rpx; /* 与 u-search 高度一致 */
  274. }
  275. .search-icon {
  276. margin-right: 16rpx;
  277. display: flex;
  278. align-items: center;
  279. }
  280. .search-input {
  281. flex: 1;
  282. height: 100%;
  283. font-size: 28rpx;
  284. color: #333;
  285. background-color: transparent;
  286. border: none;
  287. outline: none;
  288. }
  289. .placeholder-style {
  290. color: #999;
  291. font-size: 28rpx;
  292. }
  293. .search-clear {
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. margin-left: 16rpx;
  298. padding: 8rpx;
  299. &:active {
  300. opacity: 0.6;
  301. }
  302. }
  303. .address-list {
  304. padding: 20rpx 30rpx 152rpx;
  305. box-sizing: border-box;
  306. }
  307. .load-more-status {
  308. text-align: center;
  309. padding: 20rpx;
  310. font-size: 24rpx;
  311. color: #999;
  312. }
  313. .empty-state {
  314. display: flex;
  315. flex-direction: column;
  316. align-items: center;
  317. justify-content: center;
  318. padding: 120rpx 40rpx;
  319. color: #999999;
  320. }
  321. .empty-text {
  322. margin-top: 20rpx;
  323. font-size: 28rpx;
  324. }
  325. .add-btn-container {
  326. width: 100%;
  327. position: fixed;
  328. bottom: 0rpx;
  329. padding: 32rpx;
  330. background-color: #fff;
  331. border-top: 1rpx solid #eee;
  332. box-sizing: border-box;
  333. }
  334. .add-btn {
  335. height: 88rpx;
  336. background: #1B64F0;
  337. border-radius: 44rpx;
  338. border: none;
  339. }
  340. .add-btn-text {
  341. color: #fff;
  342. font-size: 32rpx;
  343. font-weight: 500;
  344. height: 88rpx;
  345. line-height: 88rpx;
  346. }
  347. /* 按钮激活效果 */
  348. .add-btn:active {
  349. opacity: 0.8;
  350. }
  351. </style>