db-customer-picker.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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"
  17. class="picker__cell"
  18. @click="onSelect(item)"
  19. >
  20. <text class="picker__title">{{ item.customerName }}</text>
  21. <text v-if="item.customerNo" class="picker__label">{{ item.customerNo }}</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 { fetchCustomerOptions } 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. async function load() {
  60. loading.value = true
  61. try {
  62. options.value = await fetchCustomerOptions(keyword.value)
  63. } catch (e) {
  64. options.value = []
  65. } finally {
  66. loading.value = false
  67. }
  68. }
  69. function onSelect(item) {
  70. emit('select', item)
  71. close()
  72. }
  73. </script>
  74. <style scoped>
  75. .picker {
  76. display: flex;
  77. flex-direction: column;
  78. height: 70vh;
  79. }
  80. .picker__search {
  81. padding: 12px;
  82. }
  83. .picker__list {
  84. flex: 1;
  85. height: 0;
  86. }
  87. .picker__cell {
  88. padding: 14px 16px;
  89. border-bottom: 1px solid #f5f6f8;
  90. }
  91. .picker__title {
  92. display: block;
  93. font-size: 15px;
  94. color: #323233;
  95. }
  96. .picker__label {
  97. display: block;
  98. margin-top: 4px;
  99. font-size: 12px;
  100. color: #969799;
  101. }
  102. .db-empty {
  103. padding: 40px;
  104. text-align: center;
  105. color: #969799;
  106. font-size: 14px;
  107. }
  108. </style>