RewardForm.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="80px"
  8. v-loading="formLoading"
  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. type="datetimerange"
  17. range-separator="-"
  18. :start-placeholder="t('common.startTimeText')"
  19. :end-placeholder="t('common.endTimeText')"
  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="parseInt(dict.value)"
  28. >{{ dict.label }}</el-radio
  29. >
  30. </el-radio-group>
  31. </el-form-item>
  32. <el-form-item label="优惠设置">
  33. <!-- TODO 待实现!这个实现下哈 -->
  34. </el-form-item>
  35. <el-form-item label="活动商品" prop="productScope">
  36. <el-radio-group v-model="formData.productScope">
  37. <el-radio
  38. v-for="dict in getIntDictOptions(DICT_TYPE.PROMOTION_PRODUCT_SCOPE)"
  39. :key="dict.value"
  40. :label="parseInt(dict.value)"
  41. >{{ dict.label }}</el-radio
  42. >
  43. </el-radio-group>
  44. </el-form-item>
  45. <!-- TODO:活动商品的开发,可以参考优惠劵的,已经搞好啦; -->
  46. <el-form-item
  47. v-if="formData.productScope === PromotionProductScopeEnum.SPU.scope"
  48. prop="productSpuIds"
  49. >
  50. <el-select
  51. v-model="formData.productSpuIds"
  52. placeholder="请选择活动商品"
  53. clearable
  54. size="small"
  55. multiple
  56. filterable
  57. style="width: 400px"
  58. >
  59. <el-option v-for="item in productSpus" :key="item.id" :label="item.name" :value="item.id">
  60. <span style="float: left">{{ item.name }}</span>
  61. <span style="float: right; font-size: 13px; color: #8492a6"
  62. >¥{{ (item.price / 100.0).toFixed(2) }}</span
  63. >
  64. </el-option>
  65. </el-select>
  66. </el-form-item>
  67. <el-form-item label="备注" prop="remark">
  68. <el-input v-model="formData.remark" placeholder="请输入备注" />
  69. </el-form-item>
  70. </el-form>
  71. <template #footer>
  72. <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  73. <el-button @click="dialogVisible = false">取 消</el-button>
  74. </template>
  75. </Dialog>
  76. </template>
  77. <script lang="ts" setup>
  78. import { getSpuSimpleList } from '@/api/mall/product/spu'
  79. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  80. import { CommonStatusEnum } from '@/utils/constants'
  81. import * as ProductBrandApi from '@/api/mall/product/brand'
  82. import {
  83. PromotionConditionTypeEnum,
  84. PromotionProductScopeEnum,
  85. PromotionActivityStatusEnum
  86. } from '@/utils/constants'
  87. // 商品数据
  88. const productSpus = ref<any[]>([])
  89. /** 初始化 **/
  90. onMounted(() => {
  91. getSpuSimpleList().then((response) => {
  92. productSpus.value = response
  93. })
  94. })
  95. defineOptions({ name: 'ProductBrandForm' })
  96. const { t } = useI18n() // 国际化
  97. const message = useMessage() // 消息弹窗
  98. const dialogVisible = ref(false) // 弹窗的是否展示
  99. const dialogTitle = ref('') // 弹窗的标题
  100. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  101. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  102. const formData = ref({
  103. id: undefined,
  104. name: undefined,
  105. startAndEndTime: undefined,
  106. startTime: undefined,
  107. endTime: undefined,
  108. conditionType: PromotionConditionTypeEnum.PRICE.type,
  109. remark: undefined,
  110. productScope: PromotionProductScopeEnum.ALL.scope,
  111. productSpuIds: undefined,
  112. rules: undefined
  113. })
  114. const formRules = reactive({
  115. name: [{ required: true, message: '活动名称不能为空', trigger: 'blur' }],
  116. startAndEndTime: [{ required: true, message: '活动时间不能为空', trigger: 'blur' }],
  117. conditionType: [{ required: true, message: '条件类型不能为空', trigger: 'change' }],
  118. productScope: [{ required: true, message: '商品范围不能为空', trigger: 'blur' }],
  119. productSpuIds: [{ required: true, message: '商品范围不能为空', trigger: 'blur' }]
  120. })
  121. const formRef = ref() // 表单 Ref
  122. /** 打开弹窗 */
  123. const open = async (type: string, id?: number) => {
  124. dialogVisible.value = true
  125. dialogTitle.value = t('action.' + type)
  126. formType.value = type
  127. resetForm()
  128. // 修改时,设置数据
  129. if (id) {
  130. formLoading.value = true
  131. try {
  132. // formData.value = await ProductBrandApi.getBrand(id)
  133. formData.value = {
  134. conditionType: 10,
  135. description: '',
  136. id: undefined,
  137. name: '测试活动',
  138. picUrl: '',
  139. productScope: 2,
  140. productSpuIds: [634],
  141. remark: '测试备注',
  142. startAndEndTime: [new Date(), new Date('2023-12-31')],
  143. status: 0
  144. }
  145. } finally {
  146. formLoading.value = false
  147. }
  148. }
  149. }
  150. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  151. /** 提交表单 */
  152. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  153. const submitForm = async () => {
  154. // 校验表单
  155. if (!formRef) return
  156. const valid = await formRef.value.validate()
  157. if (!valid) return
  158. console.log(formData.value)
  159. message.success('已在控制台打印数据')
  160. return
  161. // 提交请求
  162. formLoading.value = true
  163. try {
  164. const data = formData.value as ProductBrandApi.BrandVO
  165. if (formType.value === 'create') {
  166. await ProductBrandApi.createBrand(data)
  167. message.success(t('common.createSuccess'))
  168. } else {
  169. await ProductBrandApi.updateBrand(data)
  170. message.success(t('common.updateSuccess'))
  171. }
  172. dialogVisible.value = false
  173. // 发送操作成功的事件
  174. emit('success')
  175. } finally {
  176. formLoading.value = false
  177. }
  178. }
  179. /** 重置表单 */
  180. const resetForm = () => {
  181. formData.value = {
  182. id: undefined,
  183. name: '',
  184. picUrl: '',
  185. status: CommonStatusEnum.ENABLE,
  186. description: ''
  187. }
  188. formRef.value?.resetFields()
  189. }
  190. </script>