db-product-picker.vue 2.5 KB

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