ProductAttributes.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!-- 商品发布 - 库存价格 - 属性列表 -->
  2. <template>
  3. <el-col v-for="(item, index) in attributeList" :key="index">
  4. <div>
  5. <el-text class="mx-1">属性名:</el-text>
  6. <el-tag :closable="!isDetail" class="mx-1" type="success" @close="handleCloseProperty(index)">
  7. {{ item.name }}
  8. </el-tag>
  9. </div>
  10. <div>
  11. <el-text class="mx-1">属性值:</el-text>
  12. <el-tag
  13. v-for="(value, valueIndex) in item.values"
  14. :key="value.id"
  15. :closable="!isDetail"
  16. class="mx-1"
  17. @close="handleCloseValue(index, valueIndex)"
  18. >
  19. {{ value.name }}
  20. </el-tag>
  21. <el-select
  22. v-show="inputVisible(index)"
  23. :id="`input${index}`"
  24. :ref="setInputRef"
  25. v-model="inputValue"
  26. :reserve-keyword="false"
  27. allow-create
  28. class="!w-30"
  29. default-first-option
  30. filterable
  31. size="small"
  32. @blur="handleInputConfirm(index, item.id)"
  33. @change="handleInputConfirm(index, item.id)"
  34. @keyup.enter="handleInputConfirm(index, item.id)"
  35. >
  36. <el-option
  37. v-for="item2 in attributeOptions"
  38. :key="item2.id"
  39. :label="item2.name"
  40. :value="item2.name"
  41. />
  42. </el-select>
  43. <el-button
  44. v-show="!inputVisible(index)"
  45. class="button-new-tag ml-1"
  46. size="small"
  47. @click="showInput(index)"
  48. >
  49. + 添加
  50. </el-button>
  51. </div>
  52. <el-divider class="my-10px" />
  53. </el-col>
  54. </template>
  55. <script lang="ts" setup>
  56. import * as PropertyApi from '@/api/mall/product/property'
  57. import { PropertyAndValues } from '@/views/mall/product/spu/components'
  58. import { propTypes } from '@/utils/propTypes'
  59. defineOptions({ name: 'ProductAttributes' })
  60. const { t } = useI18n() // 国际化
  61. const message = useMessage() // 消息弹窗
  62. const inputValue = ref('') // 输入框值
  63. const attributeIndex = ref<number | null>(null) // 获取焦点时记录当前属性项的index
  64. // 输入框显隐控制
  65. const inputVisible = computed(() => (index: number) => {
  66. if (attributeIndex.value === null) return false
  67. if (attributeIndex.value === index) return true
  68. })
  69. const inputRef = ref<any[]>([]) //标签输入框Ref
  70. /** 解决 ref 在 v-for 中的获取问题*/
  71. const setInputRef = (el: any) => {
  72. if (el === null || typeof el === 'undefined') return
  73. // 如果不存在 id 相同的元素才添加
  74. if (!inputRef.value.some((item) => item.inputRef?.attributes.id === el.inputRef?.attributes.id)) {
  75. inputRef.value.push(el)
  76. }
  77. }
  78. const attributeList = ref<PropertyAndValues[]>([]) // 商品属性列表
  79. const attributeOptions = ref([] as PropertyApi.PropertyValueVO[]) // 商品属性名称下拉框
  80. const props = defineProps({
  81. propertyList: {
  82. type: Array,
  83. default: () => {}
  84. },
  85. isDetail: propTypes.bool.def(false) // 是否作为详情组件
  86. })
  87. watch(
  88. () => props.propertyList,
  89. (data) => {
  90. if (!data) return
  91. attributeList.value = data as any
  92. },
  93. {
  94. deep: true,
  95. immediate: true
  96. }
  97. )
  98. /** 删除属性值*/
  99. const handleCloseValue = (index: number, valueIndex: number) => {
  100. attributeList.value[index].values?.splice(valueIndex, 1)
  101. }
  102. /** 删除属性*/
  103. const handleCloseProperty = (index: number) => {
  104. attributeList.value?.splice(index, 1)
  105. emit('success', attributeList.value)
  106. }
  107. /** 显示输入框并获取焦点 */
  108. const showInput = async (index: number) => {
  109. attributeIndex.value = index
  110. inputRef.value[index].focus()
  111. // 获取属性下拉选项
  112. await getAttributeOptions(attributeList.value[index].id)
  113. }
  114. /** 输入框失去焦点或点击回车时触发 */
  115. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  116. const handleInputConfirm = async (index: number, propertyId: number) => {
  117. if (inputValue.value) {
  118. // 1. 重复添加校验
  119. if (attributeList.value[index].values.find((item) => item.name === inputValue.value)) {
  120. message.warning('已存在相同属性值,请重试')
  121. attributeIndex.value = null
  122. inputValue.value = ''
  123. return
  124. }
  125. // 2.1 情况一:属性值已存在,则直接使用并结束
  126. const existValue = attributeOptions.value.find((item) => item.name === inputValue.value)
  127. if (existValue) {
  128. attributeIndex.value = null
  129. inputValue.value = ''
  130. attributeList.value[index].values.push({ id: existValue.id, name: existValue.name })
  131. emit('success', attributeList.value)
  132. return
  133. }
  134. // 2.2 情况二:新属性值,则进行保存
  135. try {
  136. const id = await PropertyApi.createPropertyValue({ propertyId, name: inputValue.value })
  137. attributeList.value[index].values.push({ id, name: inputValue.value })
  138. message.success(t('common.createSuccess'))
  139. emit('success', attributeList.value)
  140. } catch {
  141. message.error('添加失败,请重试')
  142. }
  143. }
  144. attributeIndex.value = null
  145. inputValue.value = ''
  146. }
  147. /** 获取商品属性下拉选项 */
  148. const getAttributeOptions = async (propertyId: number) => {
  149. attributeOptions.value = await PropertyApi.getPropertyValueSimpleList(propertyId)
  150. }
  151. </script>