ProductPicker.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <div class="product-picker">
  3. <el-select
  4. :value="selectedValue"
  5. :placeholder="placeholder"
  6. :clearable="clearable"
  7. :disabled="disabled"
  8. :filterable="true"
  9. :remote="true"
  10. :remote-method="handleSearch"
  11. :loading="loading"
  12. :style="{ width: selectWidth }"
  13. value-key="id"
  14. class="product-picker-select"
  15. @change="handleChange"
  16. @clear="handleClear"
  17. >
  18. <el-option
  19. v-for="item in options"
  20. :key="item.id"
  21. :label="getOptionLabel(item)"
  22. :value="item.id"
  23. >
  24. <div class="product-picker-option">
  25. <img
  26. v-if="item.coverImage"
  27. :src="item.coverImage"
  28. class="product-picker-cover"
  29. @error="handleImgError($event)"
  30. />
  31. <div v-else class="product-picker-cover product-picker-cover--placeholder">
  32. <i class="el-icon-goods"></i>
  33. </div>
  34. <div class="product-picker-info">
  35. <div class="product-picker-name">{{ item.name }}</div>
  36. <div class="product-picker-meta">
  37. <span class="product-picker-price">{{ formatPrice(item.price) }}</span>
  38. <span v-if="item.productType" class="product-picker-type">{{ typeLabel(item.productType) }}</span>
  39. <span class="product-picker-id">ID: {{ item.id }}</span>
  40. </div>
  41. </div>
  42. </div>
  43. </el-option>
  44. <div v-if="loading" class="product-picker-empty">加载中...</div>
  45. <div v-else-if="searched && options.length === 0" class="product-picker-empty">未找到匹配的商品</div>
  46. </el-select>
  47. </div>
  48. </template>
  49. <script>
  50. import { getProductList, getProduct } from '@/api/admin'
  51. export default {
  52. name: 'ProductPicker',
  53. props: {
  54. // v-model 绑定的商品ID
  55. value: {
  56. type: [Number, String],
  57. default: null
  58. },
  59. placeholder: {
  60. type: String,
  61. default: '请输入商品名称搜索'
  62. },
  63. clearable: {
  64. type: Boolean,
  65. default: true
  66. },
  67. disabled: {
  68. type: Boolean,
  69. default: false
  70. },
  71. selectWidth: {
  72. type: String,
  73. default: '320px'
  74. }
  75. },
  76. data() {
  77. return {
  78. loading: false,
  79. options: [],
  80. selectedValue: null,
  81. selectedItem: null,
  82. searched: false,
  83. page: 1,
  84. pageSize: 20,
  85. hasMore: false,
  86. keyword: ''
  87. }
  88. },
  89. watch: {
  90. value: {
  91. immediate: true,
  92. handler(val) {
  93. // 处理数字/字符串类型
  94. if (val !== null && val !== undefined && val !== '') {
  95. const num = Number(val)
  96. if (!isNaN(num)) {
  97. this.selectedValue = num
  98. // 如果已有选中项且ID匹配,无需重新拉取
  99. if (this.selectedItem && this.selectedItem.id === num) return
  100. this.fetchProductById(num)
  101. } else {
  102. this.selectedValue = null
  103. this.selectedItem = null
  104. }
  105. } else {
  106. this.selectedValue = null
  107. this.selectedItem = null
  108. }
  109. }
  110. }
  111. },
  112. methods: {
  113. typeLabel(val) {
  114. const map = { physical: '实物商品', assessment: '测评服务' }
  115. return map[val] || val || ''
  116. },
  117. formatPrice(price) {
  118. if (price === null || price === undefined) return ''
  119. return (price / 100).toFixed(2) + '元'
  120. },
  121. getOptionLabel(item) {
  122. // 选中后显示在输入框中的文本
  123. return `${item.name} (¥${(item.price / 100).toFixed(2)})`
  124. },
  125. async fetchProductById(id) {
  126. try {
  127. const res = await getProduct(id)
  128. if (res && res.data && res.data.id) {
  129. this.selectedItem = res.data
  130. // 把当前选中项放入选项列表(确保显示)
  131. const exists = this.options.some(o => o.id === res.data.id)
  132. if (!exists) {
  133. this.options = [res.data, ...this.options]
  134. }
  135. }
  136. } catch (e) {
  137. console.error('[ProductPicker] 加载商品详情失败', e)
  138. // 编辑回显失败不影响使用,允许用户重新搜索
  139. }
  140. },
  141. async handleSearch(query) {
  142. this.keyword = (query || '').trim()
  143. this.page = 1
  144. this.searched = true
  145. await this.loadOptions(true)
  146. },
  147. async loadOptions(reset) {
  148. this.loading = true
  149. try {
  150. const res = await getProductList({
  151. keyword: this.keyword || undefined,
  152. page: this.page,
  153. size: this.pageSize
  154. })
  155. const data = (res && res.data) || {}
  156. const records = data.records || []
  157. if (reset) {
  158. this.options = records
  159. } else {
  160. this.options = this.options.concat(records)
  161. }
  162. // 如果有当前选中项但不在列表中,补回
  163. if (this.selectedItem && !this.options.some(o => o.id === this.selectedItem.id)) {
  164. this.options = [this.selectedItem, ...this.options]
  165. }
  166. const total = data.total || records.length
  167. this.hasMore = this.options.length < total
  168. } catch (e) {
  169. console.error('[ProductPicker] 搜索商品失败', e)
  170. if (reset) this.options = []
  171. } finally {
  172. this.loading = false
  173. }
  174. },
  175. handleChange(val) {
  176. this.selectedValue = val
  177. if (val !== null && val !== undefined && val !== '') {
  178. const found = this.options.find(o => o.id === Number(val))
  179. this.selectedItem = found || null
  180. } else {
  181. this.selectedItem = null
  182. }
  183. this.$emit('input', val === null || val === undefined ? null : Number(val))
  184. this.$emit('change', this.selectedItem)
  185. },
  186. handleClear() {
  187. this.selectedItem = null
  188. this.$emit('input', null)
  189. this.$emit('change', null)
  190. },
  191. handleImgError(e) {
  192. // 封面图加载失败时用占位符替换
  193. e.target.style.display = 'none'
  194. }
  195. }
  196. }
  197. </script>
  198. <style scoped>
  199. .product-picker-option {
  200. display: flex;
  201. align-items: center;
  202. gap: 10px;
  203. padding: 4px 0;
  204. }
  205. .product-picker-cover {
  206. width: 40px;
  207. height: 40px;
  208. border-radius: 4px;
  209. object-fit: cover;
  210. flex-shrink: 0;
  211. background: #f5f7fa;
  212. display: flex;
  213. align-items: center;
  214. justify-content: center;
  215. }
  216. .product-picker-cover--placeholder {
  217. color: #c0c4cc;
  218. font-size: 18px;
  219. }
  220. .product-picker-info {
  221. flex: 1;
  222. min-width: 0;
  223. }
  224. .product-picker-name {
  225. font-size: 14px;
  226. color: #303133;
  227. overflow: hidden;
  228. text-overflow: ellipsis;
  229. white-space: nowrap;
  230. }
  231. .product-picker-meta {
  232. font-size: 12px;
  233. color: #909399;
  234. display: flex;
  235. align-items: center;
  236. gap: 10px;
  237. margin-top: 2px;
  238. }
  239. .product-picker-price {
  240. color: #f56c6c;
  241. font-weight: 600;
  242. }
  243. .product-picker-type {
  244. background: #ecf5ff;
  245. color: #409eff;
  246. padding: 1px 6px;
  247. border-radius: 2px;
  248. }
  249. .product-picker-id {
  250. color: #c0c4cc;
  251. }
  252. .product-picker-empty {
  253. padding: 10px 0;
  254. text-align: center;
  255. color: #909399;
  256. font-size: 13px;
  257. }
  258. </style>