SeckillActivityForm.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle" width="65%">
  3. <Form
  4. ref="formRef"
  5. v-loading="formLoading"
  6. :isCol="true"
  7. :rules="rules"
  8. :schema="allSchemas.formSchema"
  9. >
  10. <!-- 先选择 -->
  11. <template #spuId>
  12. <el-button @click="spuSelectRef.open()">选择商品</el-button>
  13. <SpuAndSkuList
  14. ref="spuAndSkuListRef"
  15. :rule-config="ruleConfig"
  16. :spu-list="spuList"
  17. :spu-property-list-p="spuPropertyList"
  18. >
  19. <el-table-column align="center" label="秒杀库存" min-width="168">
  20. <template #default="{ row: sku }">
  21. <el-input-number v-model="sku.productConfig.stock" :min="0" class="w-100%" />
  22. </template>
  23. </el-table-column>
  24. <el-table-column align="center" label="秒杀价格(元)" min-width="168">
  25. <template #default="{ row: sku }">
  26. <el-input-number
  27. v-model="sku.productConfig.seckillPrice"
  28. :min="0"
  29. :precision="2"
  30. :step="0.1"
  31. class="w-100%"
  32. />
  33. </template>
  34. </el-table-column>
  35. </SpuAndSkuList>
  36. </template>
  37. </Form>
  38. <template #footer>
  39. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  40. <el-button @click="dialogVisible = false">取 消</el-button>
  41. </template>
  42. </Dialog>
  43. <SpuSelect ref="spuSelectRef" :isSelectSku="true" @confirm="selectSpu" />
  44. </template>
  45. <script lang="ts" setup>
  46. import { SpuAndSkuList, SpuProperty, SpuSelect } from '../../components'
  47. import { allSchemas, rules } from './seckillActivity.data'
  48. import { cloneDeep } from 'lodash-es'
  49. import * as SeckillActivityApi from '@/api/mall/promotion/seckill/seckillActivity'
  50. import { SeckillProductVO } from '@/api/mall/promotion/seckill/seckillActivity'
  51. import * as ProductSpuApi from '@/api/mall/product/spu'
  52. import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components'
  53. import { convertToInteger, formatToFraction } from '@/utils'
  54. defineOptions({ name: 'PromotionSeckillActivityForm' })
  55. const { t } = useI18n() // 国际化
  56. const message = useMessage() // 消息弹窗
  57. const dialogVisible = ref(false) // 弹窗的是否展示
  58. const dialogTitle = ref('') // 弹窗的标题
  59. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  60. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  61. const formRef = ref() // 表单 Ref
  62. // ================= 商品选择相关 =================
  63. const spuSelectRef = ref() // 商品和属性选择 Ref
  64. const spuAndSkuListRef = ref() // sku 秒杀配置组件Ref
  65. const ruleConfig: RuleConfig[] = [
  66. {
  67. name: 'productConfig.stock',
  68. rule: (arg) => arg >= 1,
  69. message: '商品秒杀库存必须大于等于 1 !!!'
  70. },
  71. {
  72. name: 'productConfig.seckillPrice',
  73. rule: (arg) => arg >= 0.01,
  74. message: '商品秒杀价格必须大于等于 0.01 !!!'
  75. }
  76. ]
  77. const spuList = ref<SeckillActivityApi.SpuExtension[]>([]) // 选择的 spu
  78. const spuPropertyList = ref<SpuProperty<SeckillActivityApi.SpuExtension>[]>([])
  79. const selectSpu = (spuId: number, skuIds: number[]) => {
  80. formRef.value.setValues({ spuId })
  81. getSpuDetails(spuId, skuIds)
  82. }
  83. /**
  84. * 获取 SPU 详情
  85. */
  86. const getSpuDetails = async (
  87. spuId: number,
  88. skuIds: number[] | undefined,
  89. products?: SeckillProductVO[]
  90. ) => {
  91. const spuProperties: SpuProperty<SeckillActivityApi.SpuExtension>[] = []
  92. const res = (await ProductSpuApi.getSpuDetailList([spuId])) as SeckillActivityApi.SpuExtension[]
  93. if (res.length == 0) {
  94. return
  95. }
  96. spuList.value = []
  97. // 因为只能选择一个
  98. const spu = res[0]
  99. const selectSkus =
  100. typeof skuIds === 'undefined' ? spu?.skus : spu?.skus?.filter((sku) => skuIds.includes(sku.id!))
  101. selectSkus?.forEach((sku) => {
  102. let config: SeckillActivityApi.SeckillProductVO = {
  103. skuId: sku.id!,
  104. stock: 0,
  105. seckillPrice: 0
  106. }
  107. if (typeof products !== 'undefined') {
  108. const product = products.find((item) => item.skuId === sku.id)
  109. if (product) {
  110. product.seckillPrice = formatToFraction(product.seckillPrice)
  111. }
  112. config = product || config
  113. }
  114. sku.productConfig = config
  115. })
  116. spu.skus = selectSkus as SeckillActivityApi.SkuExtension[]
  117. spuProperties.push({
  118. spuId: spu.id!,
  119. spuDetail: spu,
  120. propertyList: getPropertyList(spu)
  121. })
  122. spuList.value.push(spu)
  123. spuPropertyList.value = spuProperties
  124. }
  125. // ================= end =================
  126. /** 打开弹窗 */
  127. const open = async (type: string, id?: number) => {
  128. dialogVisible.value = true
  129. dialogTitle.value = t('action.' + type)
  130. formType.value = type
  131. await resetForm()
  132. // 修改时,设置数据
  133. if (id) {
  134. formLoading.value = true
  135. try {
  136. const data = (await SeckillActivityApi.getSeckillActivity(
  137. id
  138. )) as SeckillActivityApi.SeckillActivityVO
  139. await getSpuDetails(data.spuId!, data.products?.map((sku) => sku.skuId), data.products)
  140. formRef.value.setValues(data)
  141. } finally {
  142. formLoading.value = false
  143. }
  144. }
  145. }
  146. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  147. /** 提交表单 */
  148. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  149. const submitForm = async () => {
  150. // 校验表单
  151. if (!formRef) return
  152. const valid = await formRef.value.getElFormRef().validate()
  153. if (!valid) return
  154. // 提交请求
  155. formLoading.value = true
  156. try {
  157. // 获取秒杀商品配置
  158. const products = cloneDeep(spuAndSkuListRef.value.getSkuConfigs('productConfig'))
  159. products.forEach((item: SeckillProductVO) => {
  160. item.seckillPrice = convertToInteger(item.seckillPrice)
  161. })
  162. const data = formRef.value.formModel as SeckillActivityApi.SeckillActivityVO
  163. data.products = products
  164. // 真正提交
  165. if (formType.value === 'create') {
  166. await SeckillActivityApi.createSeckillActivity(data)
  167. message.success(t('common.createSuccess'))
  168. } else {
  169. await SeckillActivityApi.updateSeckillActivity(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 = async () => {
  181. spuList.value = []
  182. spuPropertyList.value = []
  183. await nextTick()
  184. formRef.value.getElFormRef().resetFields()
  185. }
  186. </script>