SeckillActivityForm.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 * as SeckillActivityApi from '@/api/mall/promotion/seckill/seckillActivity'
  49. import { SeckillProductVO } from '@/api/mall/promotion/seckill/seckillActivity'
  50. import * as ProductSpuApi from '@/api/mall/product/spu'
  51. import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components'
  52. import { convertToInteger, formatToFraction } from '@/utils'
  53. defineOptions({ name: 'PromotionSeckillActivityForm' })
  54. const { t } = useI18n() // 国际化
  55. const message = useMessage() // 消息弹窗
  56. const dialogVisible = ref(false) // 弹窗的是否展示
  57. const dialogTitle = ref('') // 弹窗的标题
  58. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  59. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  60. const formRef = ref() // 表单 Ref
  61. // ================= 商品选择相关 =================
  62. const spuSelectRef = ref() // 商品和属性选择 Ref
  63. const spuAndSkuListRef = ref() // sku 秒杀配置组件Ref
  64. const ruleConfig: RuleConfig[] = [
  65. {
  66. name: 'productConfig.stock',
  67. rule: (arg) => arg > 1,
  68. message: '商品秒杀库存必须大于 1 !!!'
  69. },
  70. {
  71. name: 'productConfig.seckillPrice',
  72. rule: (arg) => arg > 0.01,
  73. message: '商品秒杀价格必须大于 0.01 !!!'
  74. }
  75. ]
  76. const spuList = ref<SeckillActivityApi.SpuExtension[]>([]) // 选择的 spu
  77. const spuPropertyList = ref<SpuProperty<SeckillActivityApi.SpuExtension>[]>([])
  78. const selectSpu = (spuId: number, skuIds: number[]) => {
  79. formRef.value.setValues({ spuId })
  80. getSpuDetails(spuId, skuIds)
  81. }
  82. /**
  83. * 获取 SPU 详情
  84. */
  85. const getSpuDetails = async (
  86. spuId: number,
  87. skuIds: number[] | undefined,
  88. products?: SeckillProductVO[]
  89. ) => {
  90. const spuProperties: SpuProperty<SeckillActivityApi.SpuExtension>[] = []
  91. const res = (await ProductSpuApi.getSpuDetailList([spuId])) as SeckillActivityApi.SpuExtension[]
  92. if (res.length == 0) {
  93. return
  94. }
  95. spuList.value = []
  96. // 因为只能选择一个
  97. const spu = res[0]
  98. const selectSkus =
  99. typeof skuIds === 'undefined' ? spu?.skus : spu?.skus?.filter((sku) => skuIds.includes(sku.id!))
  100. selectSkus?.forEach((sku) => {
  101. let config: SeckillActivityApi.SeckillProductVO = {
  102. skuId: sku.id!,
  103. stock: 0,
  104. seckillPrice: 0
  105. }
  106. if (typeof products !== 'undefined') {
  107. const product = products.find((item) => item.skuId === sku.id)
  108. if (product) {
  109. // 分转元
  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 resetForm = async () => {
  149. spuList.value = []
  150. spuPropertyList.value = []
  151. await nextTick()
  152. formRef.value.getElFormRef().resetFields()
  153. }
  154. /** 提交表单 */
  155. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  156. const submitForm = async () => {
  157. // 校验表单
  158. if (!formRef) return
  159. const valid = await formRef.value.getElFormRef().validate()
  160. if (!valid) return
  161. // 提交请求
  162. formLoading.value = true
  163. try {
  164. const data = formRef.value.formModel as SeckillActivityApi.SeckillActivityVO
  165. const products = spuAndSkuListRef.value.getSkuConfigs('productConfig')
  166. products.forEach((item: SeckillProductVO) => {
  167. // 秒杀价格元转分
  168. item.seckillPrice = convertToInteger(item.seckillPrice)
  169. })
  170. // 获取秒杀商品配置
  171. data.products = products
  172. if (formType.value === 'create') {
  173. await SeckillActivityApi.createSeckillActivity(data)
  174. message.success(t('common.createSuccess'))
  175. } else {
  176. await SeckillActivityApi.updateSeckillActivity(data)
  177. message.success(t('common.updateSuccess'))
  178. }
  179. dialogVisible.value = false
  180. // 发送操作成功的事件
  181. emit('success')
  182. } finally {
  183. formLoading.value = false
  184. }
  185. }
  186. </script>
  187. <style lang="scss" scoped>
  188. .demo-table-expand {
  189. padding-left: 42px;
  190. :deep(.el-form-item__label) {
  191. width: 82px;
  192. font-weight: bold;
  193. color: #99a9bf;
  194. }
  195. }
  196. </style>