AddressList.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <div class="address-list">
  3. <template v-if="mainData.list.length > 0">
  4. <a-card
  5. v-for="item in mainData.list"
  6. :key="item.userAddressId"
  7. size="small"
  8. style="margin-bottom: 10px;"
  9. >
  10. <template #title>
  11. <a-tag v-if="item.defaultAddr" color="processing">默认</a-tag>
  12. <span>{{item.province}} {{item.city}} {{item.area}} {{item.street}}</span>
  13. </template>
  14. <template #extra>
  15. <a-button v-if="showSelect" size="small" type="link" @click="handleSelect(item)">选择</a-button>
  16. <a-button v-if="showEdit" size="small" type="link" @click="handleEdit(item)">编辑</a-button>
  17. <a-popconfirm
  18. title="是否确定删除?"
  19. ok-text="确定"
  20. cancel-text="取消"
  21. @confirm="handleDelete(item)"
  22. >
  23. <a-button size="small" type="link" danger>删除</a-button>
  24. </a-popconfirm>
  25. </template>
  26. <div>{{item.address}}{{item.houseNo}}</div>
  27. <div>{{item.name}} {{item.phone}}</div>
  28. </a-card>
  29. <div v-if="mainData.total > mainData.list.length" style="text-align: center;">
  30. <a-button type="link" :disabled="mainData.disabled" @click="handleLoadMore">点击查看更多</a-button>
  31. </div>
  32. </template>
  33. <a-empty v-else />
  34. </div>
  35. </template>
  36. <script setup lang="js">
  37. import { message } from 'ant-design-vue';
  38. import { reactive, onMounted } from 'vue';
  39. import { getAddressList, delAddress } from '@/api/user';
  40. import { useUserStore } from '@/store/user';
  41. const props = defineProps({
  42. showEdit: {
  43. type: Boolean,
  44. default: true
  45. },
  46. showSelect: {
  47. type: Boolean,
  48. default: false
  49. }
  50. })
  51. const emits = defineEmits(['select', 'edit', 'delete'])
  52. const userStore = useUserStore();
  53. const mainData = reactive({
  54. list: [],
  55. pageNum: 1,
  56. total: 0,
  57. disabled: false
  58. })
  59. const fetchAddressList = async () => {
  60. getAddressList({
  61. pageNum: mainData.pageNum,
  62. pageSize: 20,
  63. userId: userStore.userInfo?.userId || ''
  64. }).then(res => {
  65. mainData.list = mainData.list.concat(res.data?.records || []);
  66. mainData.total = res.data?.total;
  67. }).finally(() => {
  68. mainData.disabled = false
  69. })
  70. }
  71. const handleLoadMore = () => {
  72. mainData.disabled = true
  73. mainData.pageNum += 1
  74. fetchAddressList()
  75. }
  76. const handleSelect = row => emits('select', row)
  77. const handleEdit = row => emits('edit', row)
  78. const handleDelete = row => {
  79. delAddress({
  80. userAddressId: row.userAddressId
  81. }).then(() => {
  82. mainData.list = []
  83. mainData.pageNum = 1
  84. mainData.total = 0
  85. fetchAddressList()
  86. message.success('删除成功')
  87. emits('delete')
  88. })
  89. }
  90. onMounted(() => {
  91. fetchAddressList()
  92. })
  93. </script>