AddressBookDialog.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <template>
  2. <el-dialog
  3. v-model="dialogVisible"
  4. :title="title"
  5. width="900px"
  6. class="address-book-dialog"
  7. @close="handleClose"
  8. >
  9. <!-- 搜索框 -->
  10. <div class="search-wrapper">
  11. <div class="search-container">
  12. <el-input
  13. v-model="queryParams.searchKeyword"
  14. placeholder="请输入姓名、手机号或地址进行搜索"
  15. clearable
  16. class="search-input"
  17. @input="handleSearch"
  18. @clear="handleClearSearch"
  19. >
  20. <template #prefix>
  21. <i class="search-icon">
  22. <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
  23. <path d="M7.33333 12.6667C10.2789 12.6667 12.6667 10.2789 12.6667 7.33333C12.6667 4.38781 10.2789 2 7.33333 2C4.38781 2 2 4.38781 2 7.33333C2 10.2789 4.38781 12.6667 7.33333 12.6667Z" stroke="#999999" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
  24. <path d="M14 14L11.1 11.1" stroke="#999999" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
  25. </svg>
  26. </i>
  27. </template>
  28. </el-input>
  29. </div>
  30. </div>
  31. <div class="address-list">
  32. <!-- 加载状态 -->
  33. <div v-if="loading" class="loading-wrapper">
  34. <div class="loading-spinner"></div>
  35. <div class="loading-text">加载中...</div>
  36. </div>
  37. <!-- 两列布局,每行显示两个地址 -->
  38. <div
  39. v-for="(addressRow, rowIndex) in addressRows"
  40. :key="rowIndex"
  41. class="address-row"
  42. >
  43. <div
  44. v-for="address in addressRow"
  45. :key="address?.addressId || rowIndex"
  46. class="address-item"
  47. :class="{ 'active': selectedAddressId === address?.addressId, 'empty': !address }"
  48. @click="address && handleSelectAddress(address)"
  49. >
  50. <div v-if="address" class="address-content">
  51. <div class="address-header">
  52. <span class="address-name">{{ address.contactName }}</span>
  53. <span class="address-phone">{{ address.contactPhone }}</span>
  54. </div>
  55. <div class="address-body">
  56. <div class="address-detail">{{ address.provinceName }}{{ address.cityName }}{{ address.countyName }}{{ address.detailedAddress }}</div>
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. <!-- 无数据提示 -->
  62. <div v-if="!loading && addressList.length === 0" class="no-data">
  63. <el-empty description="暂无地址数据" />
  64. </div>
  65. </div>
  66. <!-- 分页器 -->
  67. <div class="pagination-wrapper" v-if="total > 0">
  68. <el-pagination
  69. v-model:current-page="queryParams.pageNum"
  70. v-model:page-size="queryParams.pageSize"
  71. :total="total"
  72. layout="prev, pager, next, jumper"
  73. background
  74. @current-change="handlePageChange"
  75. />
  76. </div>
  77. <template #footer>
  78. <span class="dialog-footer">
  79. <el-button @click="handleCancel">取消</el-button>
  80. <el-button type="primary" @click="handleConfirm">确认选择</el-button>
  81. </span>
  82. </template>
  83. </el-dialog>
  84. </template>
  85. <script setup>
  86. import { ref, computed, watch, defineProps, defineEmits, reactive, toRefs } from 'vue'
  87. import { ElMessage } from 'element-plus'
  88. import { listBook } from "@/api/logistics/book"
  89. // 定义组件属性
  90. const props = defineProps({
  91. // 是否显示弹窗
  92. visible: {
  93. type: Boolean,
  94. required: true,
  95. default: false
  96. },
  97. // 地址簿类型:sender 或 receiver
  98. addressBookType: {
  99. type: String,
  100. required: true,
  101. validator: (value) => ['sender', 'receiver'].includes(value)
  102. }
  103. })
  104. // 定义组件事件
  105. const emit = defineEmits([
  106. 'update:visible',
  107. 'select-address',
  108. 'close',
  109. 'cancel',
  110. 'confirm',
  111. 'search'
  112. ])
  113. // 响应式数据
  114. const data = reactive({
  115. // 查询参数
  116. queryParams: {
  117. pageNum: 1,
  118. pageSize: 8, // 每页显示8个地址
  119. searchKeyword: ''
  120. },
  121. // 地址列表
  122. addressList: [],
  123. // 总条数
  124. total: 0,
  125. // 加载状态
  126. loading: false
  127. })
  128. const { queryParams, addressList, total, loading } = toRefs(data)
  129. // 弹窗显示状态
  130. const dialogVisible = computed({
  131. get() {
  132. return props.visible
  133. },
  134. set(value) {
  135. emit('update:visible', value)
  136. }
  137. })
  138. // 标题
  139. const title = computed(() => {
  140. return `选择${props.addressBookType === 'sender' ? '寄件人' : '收件人'}地址`
  141. })
  142. // 选中的地址ID
  143. const selectedAddressId = ref(null)
  144. // 选中的地址
  145. const selectedAddress = ref(null)
  146. // 监听visible变化,显示时获取数据
  147. watch(() => props.visible, (newVal) => {
  148. if (newVal) {
  149. // 重置选择状态
  150. selectedAddressId.value = null
  151. selectedAddress.value = null
  152. // 重置查询参数
  153. queryParams.value.pageNum = 1
  154. queryParams.value.searchKeyword = ''
  155. // 获取地址列表
  156. getAddressList()
  157. }
  158. })
  159. // 计算属性:将地址数据分组为每行2个
  160. const addressRows = computed(() => {
  161. const rows = []
  162. // 直接使用当前页的地址列表(已经是分页后的数据)
  163. const currentPageAddresses = addressList.value
  164. // 将地址分组为每行2个
  165. for (let i = 0; i < currentPageAddresses.length; i += 2) {
  166. const row = currentPageAddresses.slice(i, i + 2)
  167. // 如果这行只有1个地址,补一个空位保持布局
  168. if (row.length < 2) {
  169. row.push(null)
  170. }
  171. rows.push(row)
  172. }
  173. // 如果当前页没有地址,添加空行
  174. if (rows.length === 0) {
  175. rows.push([null, null])
  176. }
  177. return rows
  178. })
  179. // 获取地址列表
  180. const getAddressList = () => {
  181. loading.value = true
  182. // 构建查询参数,添加地址类型
  183. const params = {
  184. ...queryParams.value,
  185. type: props.addressBookType === 'sender' ? 'sender' : 'receiver'
  186. }
  187. listBook(params).then(response => {
  188. addressList.value = response.rows || []
  189. total.value = response.total || 0
  190. loading.value = false
  191. }).catch(error => {
  192. console.error('获取地址列表失败:', error)
  193. addressList.value = []
  194. total.value = 0
  195. loading.value = false
  196. })
  197. }
  198. // 处理搜索
  199. const handleSearch = () => {
  200. // 重置到第一页
  201. queryParams.value.pageNum = 1
  202. // 获取地址列表
  203. getAddressList()
  204. // 触发搜索事件
  205. emit('search', queryParams.value.searchKeyword)
  206. }
  207. // 处理清除搜索
  208. const handleClearSearch = () => {
  209. queryParams.value.searchKeyword = ''
  210. queryParams.value.pageNum = 1
  211. // 获取地址列表
  212. getAddressList()
  213. emit('search', '')
  214. }
  215. // 处理选择地址
  216. const handleSelectAddress = (address) => {
  217. if (!address) return
  218. selectedAddressId.value = address.addressId
  219. selectedAddress.value = { ...address }
  220. // 触发选择事件
  221. emit('select-address', address)
  222. }
  223. // 处理分页变化
  224. const handlePageChange = (page) => {
  225. queryParams.value.pageNum = page
  226. selectedAddressId.value = null
  227. selectedAddress.value = null
  228. // 获取新页的数据
  229. getAddressList()
  230. }
  231. // 处理取消
  232. const handleCancel = () => {
  233. dialogVisible.value = false
  234. emit('cancel')
  235. }
  236. // 处理确认
  237. const handleConfirm = () => {
  238. if (selectedAddress.value) {
  239. emit('confirm', selectedAddress.value)
  240. dialogVisible.value = false
  241. } else {
  242. // 如果没有选中地址,可以选择触发一个错误提示
  243. ElMessage.warning('请先选择一个地址')
  244. }
  245. }
  246. // 处理关闭
  247. const handleClose = () => {
  248. selectedAddressId.value = null
  249. selectedAddress.value = null
  250. queryParams.value.searchKeyword = ''
  251. emit('close')
  252. }
  253. </script>
  254. <style scoped>
  255. .address-book-dialog :deep(.el-dialog__header) {
  256. padding: 20px 20px 10px;
  257. border-bottom: 1px solid #e8e8e8;
  258. }
  259. .address-book-dialog :deep(.el-dialog__body) {
  260. padding: 0;
  261. }
  262. /* 搜索框样式 */
  263. .search-wrapper {
  264. padding: 20px;
  265. background-color: #fff;
  266. }
  267. .search-container {
  268. position: relative;
  269. }
  270. .search-input {
  271. width: 100%;
  272. }
  273. .search-input :deep(.el-input__wrapper) {
  274. border-radius: 8px;
  275. background-color: #f5f7fa;
  276. border: 1px solid #e4e7ed;
  277. padding: 0 16px;
  278. box-shadow: none;
  279. height: 40px;
  280. }
  281. .search-input :deep(.el-input__wrapper:hover) {
  282. border-color: #c0c4cc;
  283. }
  284. .search-input :deep(.el-input__wrapper.is-focus) {
  285. border-color: #409eff;
  286. box-shadow: 0 0 0 1px rgba(64, 158, 255, 0.2);
  287. }
  288. .search-input :deep(.el-input__inner) {
  289. height: 38px;
  290. line-height: 38px;
  291. font-size: 14px;
  292. color: #333;
  293. background-color: transparent;
  294. }
  295. .search-input :deep(.el-input__inner::placeholder) {
  296. color: #999;
  297. }
  298. .search-icon {
  299. display: flex;
  300. align-items: center;
  301. justify-content: center;
  302. margin-right: 8px;
  303. color: #999;
  304. }
  305. .search-input :deep(.el-input__prefix) {
  306. display: flex;
  307. align-items: center;
  308. justify-content: center;
  309. margin-right: 8px;
  310. }
  311. .search-input :deep(.el-input__suffix) {
  312. display: flex;
  313. align-items: center;
  314. margin-right: 8px;
  315. }
  316. /* 地址列表样式 - 两列布局 */
  317. .address-list {
  318. padding: 0 20px 20px;
  319. max-height: 500px;
  320. overflow-y: auto;
  321. background-color: #fff;
  322. position: relative;
  323. min-height: 300px;
  324. }
  325. /* 加载状态 */
  326. .loading-wrapper {
  327. position: absolute;
  328. top: 50%;
  329. left: 50%;
  330. transform: translate(-50%, -50%);
  331. display: flex;
  332. flex-direction: column;
  333. align-items: center;
  334. justify-content: center;
  335. z-index: 10;
  336. }
  337. .loading-spinner {
  338. width: 40px;
  339. height: 40px;
  340. border: 3px solid #f3f3f3;
  341. border-top: 3px solid #409eff;
  342. border-radius: 50%;
  343. animation: spin 1s linear infinite;
  344. margin-bottom: 10px;
  345. }
  346. @keyframes spin {
  347. 0% { transform: rotate(0deg); }
  348. 100% { transform: rotate(360deg); }
  349. }
  350. .loading-text {
  351. font-size: 14px;
  352. color: #666;
  353. }
  354. .address-row {
  355. display: grid;
  356. grid-template-columns: 1fr 1fr;
  357. gap: 16px;
  358. margin-bottom: 16px;
  359. }
  360. .address-item {
  361. background: #F5F7FA;
  362. border-radius: 16px;
  363. padding: 16px;
  364. cursor: pointer;
  365. transition: all 0.3s ease;
  366. display: flex;
  367. flex-direction: column;
  368. min-height: 100px;
  369. border: 1px solid transparent;
  370. }
  371. .address-item:hover {
  372. background: #EAF1FF;
  373. box-shadow: 0 2px 8px rgba(24, 144, 255, 0.1);
  374. border-color: #d9e7ff;
  375. }
  376. .address-item.active {
  377. border: 1px solid #2D71FF;
  378. background-color: #EAF1FF;
  379. box-shadow: 0 2px 8px rgba(24, 144, 255, 0.2);
  380. }
  381. .address-item.empty {
  382. visibility: hidden;
  383. pointer-events: none;
  384. }
  385. .address-content {
  386. display: flex;
  387. flex-direction: column;
  388. gap: 12px;
  389. }
  390. .address-header {
  391. display: flex;
  392. justify-content: flex-start;
  393. align-items: flex-start;
  394. gap: 8px;
  395. }
  396. .address-name {
  397. font-size: 16px;
  398. font-weight: 600;
  399. color: #333333;
  400. }
  401. .address-phone {
  402. font-size: 16px;
  403. font-weight: 600;
  404. color: #333333;
  405. margin-left: 20px;
  406. }
  407. .address-tags {
  408. display: flex;
  409. gap: 4px;
  410. flex-wrap: wrap;
  411. }
  412. .tag {
  413. font-size: 10px;
  414. padding: 2px 6px;
  415. border-radius: 2px;
  416. color: #fff;
  417. white-space: nowrap;
  418. }
  419. .default-tag {
  420. background-color: #1890ff;
  421. }
  422. .home-tag {
  423. background-color: #52c41a;
  424. }
  425. .company-tag {
  426. background-color: #722ed1;
  427. }
  428. .address-body {
  429. display: flex;
  430. align-items: center;
  431. }
  432. .address-detail {
  433. font-size: 14px;
  434. color: #333;
  435. line-height: 22px;
  436. word-break: break-all;
  437. }
  438. /* 无数据提示 */
  439. .no-data {
  440. display: flex;
  441. justify-content: center;
  442. align-items: center;
  443. min-height: 200px;
  444. color: #999;
  445. }
  446. .pagination-wrapper {
  447. padding: 20px;
  448. display: flex;
  449. justify-content: center;
  450. background-color: #fafafa;
  451. border-top: 1px solid #e8e8e8;
  452. }
  453. .address-book-dialog :deep(.el-dialog__footer) {
  454. padding: 12px 20px;
  455. border-top: 1px solid #e8e8e8;
  456. }
  457. /* 响应式调整 */
  458. @media (max-width: 768px) {
  459. .address-book-dialog {
  460. width: 95% !important;
  461. }
  462. .address-row {
  463. grid-template-columns: 1fr;
  464. }
  465. .search-wrapper {
  466. padding: 16px;
  467. }
  468. }
  469. </style>