db-vehicle-picker.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <db-popup :show="show" height="70vh" @close="close" @update:show="(v) => emit('update:show', v)">
  3. <view class="picker">
  4. <view class="picker__search">
  5. <db-search
  6. v-model="keyword"
  7. placeholder="搜索车牌号"
  8. @change="onKeyword"
  9. @search="load"
  10. @clear="onClear"
  11. />
  12. </view>
  13. <scroll-view scroll-y class="picker__list">
  14. <view
  15. v-for="item in options"
  16. :key="item.id || item.licensePlate"
  17. class="picker__cell"
  18. @click="onSelect(item.licensePlate)"
  19. >
  20. <text class="picker__title">{{ item.licensePlate }}</text>
  21. <text v-if="typeLabel(item.vehicleType)" class="picker__label">{{ typeLabel(item.vehicleType) }}</text>
  22. </view>
  23. <view v-if="!loading && !options.length" class="db-empty">暂无匹配</view>
  24. </scroll-view>
  25. </view>
  26. </db-popup>
  27. </template>
  28. <script setup>
  29. import { ref, watch } from 'vue'
  30. import { fetchVehicleOptions } from '@/api/dbOptions'
  31. import DbPopup from '@/components/db-ui/db-popup.vue'
  32. import DbSearch from '@/components/db-ui/db-search.vue'
  33. const props = defineProps({ show: Boolean })
  34. const emit = defineEmits(['update:show', 'select'])
  35. const keyword = ref('')
  36. const loading = ref(false)
  37. const options = ref([])
  38. let timer = null
  39. watch(
  40. () => props.show,
  41. (v) => {
  42. if (v) {
  43. keyword.value = ''
  44. load()
  45. }
  46. }
  47. )
  48. function close() {
  49. emit('update:show', false)
  50. }
  51. function onClear() {
  52. keyword.value = ''
  53. load()
  54. }
  55. function onKeyword() {
  56. if (timer) clearTimeout(timer)
  57. timer = setTimeout(load, 300)
  58. }
  59. function typeLabel(t) {
  60. if (t === 'self') return '自有'
  61. if (t === 'carrier') return '三方'
  62. return ''
  63. }
  64. async function load() {
  65. loading.value = true
  66. try {
  67. options.value = await fetchVehicleOptions(keyword.value)
  68. } catch (e) {
  69. options.value = []
  70. } finally {
  71. loading.value = false
  72. }
  73. }
  74. function onSelect(plate) {
  75. emit('select', plate)
  76. close()
  77. }
  78. </script>
  79. <style scoped>
  80. .picker {
  81. display: flex;
  82. flex-direction: column;
  83. height: 70vh;
  84. }
  85. .picker__search {
  86. padding: 12px;
  87. }
  88. .picker__list {
  89. flex: 1;
  90. height: 0;
  91. }
  92. .picker__cell {
  93. padding: 14px 16px;
  94. border-bottom: 1px solid #f5f6f8;
  95. }
  96. .picker__title {
  97. display: block;
  98. font-size: 15px;
  99. color: #323233;
  100. }
  101. .picker__label {
  102. display: block;
  103. margin-top: 4px;
  104. font-size: 12px;
  105. color: #969799;
  106. }
  107. .db-empty {
  108. padding: 40px;
  109. text-align: center;
  110. color: #969799;
  111. font-size: 14px;
  112. }
  113. </style>