RewardForm.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle" width="65%">
  3. <el-form
  4. ref="formRef"
  5. v-loading="formLoading"
  6. :model="formData"
  7. :rules="formRules"
  8. label-width="80px"
  9. >
  10. <el-form-item label="活动名称" prop="name">
  11. <el-input v-model="formData.name" placeholder="请输入活动名称" />
  12. </el-form-item>
  13. <el-form-item label="活动时间" prop="startAndEndTime">
  14. <el-date-picker
  15. v-model="formData.startAndEndTime"
  16. :end-placeholder="t('common.endTimeText')"
  17. :start-placeholder="t('common.startTimeText')"
  18. range-separator="-"
  19. type="datetimerange"
  20. />
  21. </el-form-item>
  22. <el-form-item label="条件类型" prop="conditionType">
  23. <el-radio-group v-model="formData.conditionType">
  24. <el-radio
  25. v-for="dict in getIntDictOptions(DICT_TYPE.PROMOTION_CONDITION_TYPE)"
  26. :key="dict.value"
  27. :label="dict.value"
  28. >
  29. {{ dict.label }}
  30. </el-radio>
  31. </el-radio-group>
  32. </el-form-item>
  33. <el-form-item label="优惠设置">
  34. <RewardRule ref="rewardRuleRef" v-model="formData" />
  35. </el-form-item>
  36. <el-form-item label="活动范围" prop="productScope">
  37. <el-radio-group v-model="formData.productScope">
  38. <el-radio
  39. v-for="dict in getIntDictOptions(DICT_TYPE.PROMOTION_PRODUCT_SCOPE)"
  40. :key="dict.value"
  41. :label="dict.value"
  42. >
  43. {{ dict.label }}
  44. </el-radio>
  45. </el-radio-group>
  46. </el-form-item>
  47. <el-form-item
  48. v-if="formData.productScope === PromotionProductScopeEnum.SPU.scope"
  49. prop="productSpuIds"
  50. >
  51. <SpuShowcase v-model="formData.productSpuIds" />
  52. </el-form-item>
  53. <el-form-item
  54. v-if="formData.productScope === PromotionProductScopeEnum.CATEGORY.scope"
  55. label="分类"
  56. prop="productCategoryIds"
  57. >
  58. <ProductCategorySelect v-model="formData.productCategoryIds" />
  59. </el-form-item>
  60. <el-form-item label="备注" prop="remark">
  61. <el-input v-model="formData.remark" placeholder="请输入备注" />
  62. </el-form-item>
  63. </el-form>
  64. <template #footer>
  65. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  66. <el-button @click="dialogVisible = false">取 消</el-button>
  67. </template>
  68. </Dialog>
  69. </template>
  70. <script lang="ts" setup>
  71. import RewardRule from './components/RewardRule.vue'
  72. import SpuShowcase from '@/views/mall/product/spu/components/SpuShowcase.vue'
  73. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  74. import * as RewardActivityApi from '@/api/mall/promotion/reward/rewardActivity'
  75. import { PromotionConditionTypeEnum, PromotionProductScopeEnum } from '@/utils/constants'
  76. import ProductCategorySelect from '@/views/mall/product/category/components/ProductCategorySelect.vue'
  77. import { cloneDeep } from 'lodash-es'
  78. import { fenToYuan, yuanToFen } from '@/utils'
  79. defineOptions({ name: 'ProductBrandForm' })
  80. const { t } = useI18n() // 国际化
  81. const message = useMessage() // 消息弹窗
  82. const dialogVisible = ref(false) // 弹窗的是否展示
  83. const dialogTitle = ref('') // 弹窗的标题
  84. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  85. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  86. const formData = ref<RewardActivityApi.RewardActivityVO>({
  87. conditionType: PromotionConditionTypeEnum.PRICE.type,
  88. productScope: PromotionProductScopeEnum.ALL.scope,
  89. rules: []
  90. } as RewardActivityApi.RewardActivityVO)
  91. const formRules = reactive({
  92. name: [{ required: true, message: '活动名称不能为空', trigger: 'blur' }],
  93. startAndEndTime: [{ required: true, message: '活动时间不能为空', trigger: 'blur' }],
  94. conditionType: [{ required: true, message: '条件类型不能为空', trigger: 'change' }],
  95. productScope: [{ required: true, message: '商品范围不能为空', trigger: 'blur' }],
  96. productSpuIds: [{ required: true, message: '商品不能为空', trigger: 'blur' }],
  97. productCategoryIds: [{ required: true, message: '商品分类不能为空', trigger: 'blur' }]
  98. })
  99. const formRef = ref() // 表单 Ref
  100. const rewardRuleRef = ref<InstanceType<typeof RewardRule>>() // 活动规则 Ref
  101. /** 打开弹窗 */
  102. const open = async (type: string, id?: number) => {
  103. dialogVisible.value = true
  104. dialogTitle.value = t('action.' + type)
  105. formType.value = type
  106. resetForm()
  107. // 修改时,设置数据
  108. if (id) {
  109. formLoading.value = true
  110. try {
  111. const data = await RewardActivityApi.getReward(id)
  112. // 转区段时间
  113. data.startAndEndTime = [data.startTime, data.endTime]
  114. // 规则分转元
  115. data.rules.forEach((item: any) => {
  116. item.discountPrice = fenToYuan(item.discountPrice || 0)
  117. })
  118. formData.value = data
  119. // 获得商品范围
  120. await getProductScope()
  121. } finally {
  122. formLoading.value = false
  123. }
  124. }
  125. }
  126. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  127. /** 提交表单 */
  128. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  129. const submitForm = async () => {
  130. // 校验表单
  131. if (!formRef.value) return
  132. const valid = await formRef.value.validate()
  133. if (!valid) return
  134. // 提交请求
  135. formLoading.value = true
  136. try {
  137. // 设置活动规则优惠券
  138. rewardRuleRef.value?.setRuleCoupon()
  139. const data = cloneDeep(formData.value)
  140. // 时间段转换
  141. data.startTime = data.startAndEndTime![0]
  142. data.endTime = data.startAndEndTime![1]
  143. // 规则元转分
  144. data.rules.forEach((item) => {
  145. item.discountPrice = yuanToFen(item.discountPrice || 0)
  146. })
  147. // 设置商品范围
  148. setProductScopeValues(data)
  149. if (formType.value === 'create') {
  150. await RewardActivityApi.createRewardActivity(data)
  151. message.success(t('common.createSuccess'))
  152. } else {
  153. await RewardActivityApi.updateRewardActivity(data)
  154. message.success(t('common.updateSuccess'))
  155. }
  156. dialogVisible.value = false
  157. // 发送操作成功的事件
  158. emit('success')
  159. } finally {
  160. formLoading.value = false
  161. }
  162. }
  163. /** 重置表单 */
  164. const resetForm = () => {
  165. formData.value = {
  166. conditionType: PromotionConditionTypeEnum.PRICE.type,
  167. productScope: PromotionProductScopeEnum.ALL.scope,
  168. rules: []
  169. } as RewardActivityApi.RewardActivityVO
  170. }
  171. /** 获得商品范围 */
  172. const getProductScope = async () => {
  173. switch (formData.value.productScope) {
  174. case PromotionProductScopeEnum.SPU.scope:
  175. // 设置商品编号
  176. formData.value.productSpuIds = formData.value.productScopeValues
  177. break
  178. case PromotionProductScopeEnum.CATEGORY.scope:
  179. await nextTick()
  180. let productCategoryIds = formData.value.productScopeValues as any
  181. if (Array.isArray(productCategoryIds) && productCategoryIds.length > 0) {
  182. // 单选时使用数组不能反显
  183. productCategoryIds = productCategoryIds[0]
  184. }
  185. // 设置品类编号
  186. formData.value.productCategoryIds = productCategoryIds
  187. break
  188. default:
  189. break
  190. }
  191. }
  192. /** 设置商品范围 */
  193. function setProductScopeValues(data: any) {
  194. switch (formData.value.productScope) {
  195. case PromotionProductScopeEnum.SPU.scope:
  196. data.productScopeValues = formData.value.productSpuIds
  197. break
  198. case PromotionProductScopeEnum.CATEGORY.scope:
  199. data.productScopeValues = Array.isArray(formData.value.productCategoryIds)
  200. ? formData.value.productCategoryIds
  201. : [formData.value.productCategoryIds]
  202. break
  203. default:
  204. break
  205. }
  206. }
  207. </script>