OtherSettingsForm.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <el-form ref="otherSettingsFormRef" :model="formData" :rules="rules" label-width="120px">
  3. <el-row>
  4. <el-col :span="24">
  5. <el-row :gutter="20">
  6. <el-col :span="8">
  7. <el-form-item label="商品排序" prop="sort">
  8. <el-input-number v-model="formData.sort" :min="0" />
  9. </el-form-item>
  10. </el-col>
  11. <el-col :span="8">
  12. <el-form-item label="赠送积分" prop="giveIntegral">
  13. <el-input-number v-model="formData.giveIntegral" :min="0" />
  14. </el-form-item>
  15. </el-col>
  16. <el-col :span="8">
  17. <el-form-item label="虚拟销量" prop="virtualSalesCount">
  18. <el-input-number
  19. v-model="formData.virtualSalesCount"
  20. :min="0"
  21. placeholder="请输入虚拟销量"
  22. />
  23. </el-form-item>
  24. </el-col>
  25. </el-row>
  26. </el-col>
  27. <el-col :span="24">
  28. <el-form-item label="商品推荐">
  29. <el-checkbox-group v-model="checkboxGroup" @change="onChangeGroup">
  30. <el-checkbox v-for="(item, index) in recommendOptions" :key="index" :label="item.value">
  31. {{ item.name }}
  32. </el-checkbox>
  33. </el-checkbox-group>
  34. </el-form-item>
  35. </el-col>
  36. <el-col :span="24">
  37. <!-- TODO tag展示暂时不考虑排序 -->
  38. <el-form-item label="活动优先级">
  39. <el-tag>默认</el-tag>
  40. <el-tag class="ml-2" type="success">秒杀</el-tag>
  41. <el-tag class="ml-2" type="info">砍价</el-tag>
  42. <el-tag class="ml-2" type="warning">拼团</el-tag>
  43. </el-form-item>
  44. </el-col>
  45. <!-- TODO @puhui999:等优惠劵 ok 在搞 -->
  46. <el-col :span="24">
  47. <el-form-item label="赠送优惠劵">
  48. <el-button>选择优惠券</el-button>
  49. </el-form-item>
  50. </el-col>
  51. </el-row>
  52. </el-form>
  53. </template>
  54. <script lang="ts" name="OtherSettingsForm" setup>
  55. import type { Spu } from '@/api/mall/product/spu'
  56. import { PropType } from 'vue'
  57. import { propTypes } from '@/utils/propTypes'
  58. import { copyValueToTarget } from '@/utils'
  59. const message = useMessage() // 消息弹窗
  60. const props = defineProps({
  61. propFormData: {
  62. type: Object as PropType<Spu>,
  63. default: () => {}
  64. },
  65. activeName: propTypes.string.def('')
  66. })
  67. const otherSettingsFormRef = ref() // 表单Ref
  68. // 表单数据
  69. const formData = ref<Spu>({
  70. sort: 1, // 商品排序
  71. giveIntegral: 1, // 赠送积分
  72. virtualSalesCount: 1, // 虚拟销量
  73. recommendHot: false, // 是否热卖
  74. recommendBenefit: false, // 是否优惠
  75. recommendBest: false, // 是否精品
  76. recommendNew: false, // 是否新品
  77. recommendGood: false // 是否优品
  78. })
  79. // 表单规则
  80. const rules = reactive({
  81. sort: [required],
  82. giveIntegral: [required],
  83. virtualSalesCount: [required]
  84. })
  85. const recommendOptions = [
  86. { name: '是否热卖', value: 'recommendHot' },
  87. { name: '是否优惠', value: 'recommendBenefit' },
  88. { name: '是否精品', value: 'recommendBest' },
  89. { name: '是否新品', value: 'recommendNew' },
  90. { name: '是否优品', value: 'recommendGood' }
  91. ] // 商品推荐选项
  92. const checkboxGroup = ref<string[]>([]) // 选中的推荐选项
  93. /** 选择商品后赋值 */
  94. const onChangeGroup = () => {
  95. recommendOptions.forEach(({ value }) => {
  96. formData.value[value] = checkboxGroup.value.includes(value)
  97. })
  98. }
  99. /**
  100. * 将传进来的值赋值给formData
  101. */
  102. watch(
  103. () => props.propFormData,
  104. (data) => {
  105. if (!data) {
  106. return
  107. }
  108. copyValueToTarget(formData.value, data)
  109. recommendOptions.forEach(({ value }) => {
  110. if (formData.value[value] && !checkboxGroup.value.includes(value)) {
  111. checkboxGroup.value.push(value)
  112. }
  113. })
  114. },
  115. {
  116. immediate: true
  117. }
  118. )
  119. /**
  120. * 表单校验
  121. */
  122. const emit = defineEmits(['update:activeName'])
  123. const validate = async () => {
  124. // 校验表单
  125. if (!otherSettingsFormRef) return
  126. return await unref(otherSettingsFormRef).validate((valid) => {
  127. if (!valid) {
  128. message.warning('商品其他设置未完善!!')
  129. emit('update:activeName', 'otherSettings')
  130. // 目的截断之后的校验
  131. throw new Error('商品其他设置未完善!!')
  132. } else {
  133. // 校验通过更新数据
  134. Object.assign(props.propFormData, formData.value)
  135. }
  136. })
  137. }
  138. defineExpose({ validate })
  139. </script>