gaoluyang
2026-06-24 bfdc0e0e6d5e47aa501f9b6fadd143d9c97cf00a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<script lang="ts" setup>
import type { MallSpuApi } from '#/api/mall/product/spu';
import type { MallBargainActivityApi } from '#/api/mall/promotion/bargain/bargainActivity';
import type {
  RuleConfig,
  SpuProperty,
} from '#/views/mall/product/spu/components';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '../../../../../../packages/effects/common-ui/src';
import { cloneDeep, convertToInteger, formatToFraction } from '../../../../../../packages/utils/src';
 
import { Button, InputNumber, message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { VxeColumn } from '#/adapter/vxe-table';
import { getSpu } from '#/api/mall/product/spu';
import {
  createBargainActivity,
  getBargainActivity,
  updateBargainActivity,
} from '#/api/mall/promotion/bargain/bargainActivity';
import { $t } from '#/locales';
import {
  getPropertyList,
  SpuAndSkuList,
  SpuSkuSelect,
} from '#/views/mall/product/spu/components';
 
import { useFormSchema } from '../data';
 
defineOptions({ name: 'PromotionBargainActivityForm' });
 
const emit = defineEmits(['success']);
 
const formData = ref<MallBargainActivityApi.BargainActivity>();
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['砍价活动'])
    : $t('ui.actionTitle.create', ['砍价活动']);
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    labelWidth: 130,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
});
 
// ================= 商品选择相关 =================
 
const spuSkuSelectRef = ref(); // 商品和属性选择 Ref
const spuAndSkuListRef = ref(); // SPU 和 SKU 列表组件 Ref
 
const ruleConfig: RuleConfig[] = [
  {
    name: 'productConfig.bargainFirstPrice',
    rule: (arg) => arg > 0,
    message: '商品砍价起始价格必须大于 0 !!!',
  },
  {
    name: 'productConfig.bargainMinPrice',
    rule: (arg) => arg >= 0,
    message: '商品砍价底价不能小于 0 !!!',
  },
  {
    name: 'productConfig.stock',
    rule: (arg) => arg >= 1,
    message: '商品活动库存必须大于等于 1 !!!',
  },
];
 
const spuList = ref<MallSpuApi.Spu[]>([]); // 选择的 SPU 列表
const spuPropertyList = ref<SpuProperty<MallSpuApi.Spu>[]>([]); // SPU 属性列表
 
/** 打开商品选择器 */
function openSpuSelect() {
  spuSkuSelectRef.value?.open();
}
 
/** 选择商品后的回调 */
async function handleSpuSelected(spuId: number, skuIds?: number[]) {
  await formApi.setFieldValue('spuId', spuId);
  await getSpuDetails(spuId, skuIds);
}
 
/** 获取 SPU 详情 */
async function getSpuDetails(
  spuId: number,
  skuIds?: number[],
  products?: MallBargainActivityApi.BargainProduct[],
) {
  const res = await getSpu(spuId);
  if (!res) {
    return;
  }
 
  spuList.value = [];
 
  // 筛选指定的 SKU(砍价活动只选择一个 SKU)
  const selectSkus =
    skuIds === undefined
      ? res.skus
      : res.skus?.filter((sku) => skuIds.includes(sku.id!));
 
  // 为每个 SKU 配置砍价活动相关的配置
  selectSkus?.forEach((sku) => {
    let config: MallBargainActivityApi.BargainProduct = {
      spuId: res.id!,
      skuId: sku.id!,
      bargainFirstPrice: 1,
      bargainMinPrice: 1,
      stock: 1,
    };
    // 如果是编辑模式,回填已有配置
    if (products !== undefined) {
      const product = products.find((item) => item.skuId === sku.id);
      if (product) {
        // 分转元
        product.bargainFirstPrice = formatToFraction(
          product.bargainFirstPrice,
        ) as unknown as number;
        product.bargainMinPrice = formatToFraction(
          product.bargainMinPrice,
        ) as unknown as number;
      }
      config = product || config;
    }
    // 动态添加 productConfig 属性到 SKU
    (
      sku as MallSpuApi.Sku & {
        productConfig: MallBargainActivityApi.BargainProduct;
      }
    ).productConfig = config;
  });
  res.skus = selectSkus;
 
  const spuProperties: SpuProperty<MallSpuApi.Spu>[] = [
    {
      spuId: res.id!,
      spuDetail: res,
      propertyList: getPropertyList(res),
    },
  ];
 
  // 直接赋值,因为砍价活动只选择一个 SPU
  spuList.value = [res];
  spuPropertyList.value = spuProperties;
}
 
// ================= end =================
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    try {
      // 获取砍价商品配置(深拷贝避免直接修改原对象)
      const products: MallBargainActivityApi.BargainProduct[] = cloneDeep(
        spuAndSkuListRef.value?.getSkuConfigs('productConfig') || [],
      );
      if (products.length === 0) {
        message.error('请选择砍价商品');
        return;
      }
      // 价格需要转为分
      products.forEach((item) => {
        item.bargainFirstPrice = convertToInteger(item.bargainFirstPrice);
        item.bargainMinPrice = convertToInteger(item.bargainMinPrice);
      });
 
      // 提交表单
      const values = await formApi.getValues();
      const data = {
        ...values,
        // 用户每次砍价金额元转分
        randomMinPrice: values.randomMinPrice
          ? convertToInteger(values.randomMinPrice)
          : undefined,
        randomMaxPrice: values.randomMaxPrice
          ? convertToInteger(values.randomMaxPrice)
          : undefined,
        // 合并砍价商品配置(砍价活动只有一个商品)
        ...products[0],
      } as MallBargainActivityApi.BargainActivity;
 
      await (formData.value?.id
        ? updateBargainActivity(data)
        : createBargainActivity(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      spuList.value = [];
      spuPropertyList.value = [];
      return;
    }
 
    // 加载数据
    const data = modalApi.getData<MallBargainActivityApi.BargainActivity>();
    if (!data || !data.id) {
      return;
    }
    // 加载数据
    modalApi.lock();
    try {
      formData.value = await getBargainActivity(data.id);
      // 对齐活动商品处理结构
      await getSpuDetails(
        formData.value.spuId,
        [formData.value.skuId],
        [
          {
            spuId: formData.value.spuId,
            skuId: formData.value.skuId,
            bargainFirstPrice: formData.value.bargainFirstPrice, // 砍价起始价格,单位分
            bargainMinPrice: formData.value.bargainMinPrice, // 砍价底价
            stock: formData.value.stock, // 活动库存
          },
        ],
      );
      // 设置表单值时,价格字段从分转换为元
      await formApi.setValues({
        ...formData.value,
        randomMinPrice: formData.value.randomMinPrice
          ? formatToFraction(formData.value.randomMinPrice)
          : undefined,
        randomMaxPrice: formData.value.randomMaxPrice
          ? formatToFraction(formData.value.randomMaxPrice)
          : undefined,
      });
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <div>
    <Modal :title="getTitle" class="w-[70%]">
      <Form class="mx-4">
        <!-- 商品选择 -->
        <template #spuId>
          <div class="w-full">
            <Button v-if="!formData?.id" type="primary" @click="openSpuSelect">
              选择商品
            </Button>
 
            <!-- SPU 和 SKU 列表展示 -->
            <SpuAndSkuList
              ref="spuAndSkuListRef"
              :rule-config="ruleConfig"
              :spu-list="spuList"
              :spu-property-list-p="spuPropertyList"
              class="mt-4"
            >
              <!-- 扩展列:砍价活动特有配置 -->
              <template #default>
                <VxeColumn
                  align="center"
                  min-width="168"
                  title="砍价起始价格(元)"
                >
                  <template #default="{ row: sku }">
                    <InputNumber
                      v-model:value="sku.productConfig.bargainFirstPrice"
                      :min="0"
                      :precision="2"
                      :step="0.1"
                      class="w-full"
                    />
                  </template>
                </VxeColumn>
                <VxeColumn align="center" min-width="168" title="砍价底价(元)">
                  <template #default="{ row: sku }">
                    <InputNumber
                      v-model:value="sku.productConfig.bargainMinPrice"
                      :min="0"
                      :precision="2"
                      :step="0.1"
                      class="w-full"
                    />
                  </template>
                </VxeColumn>
                <VxeColumn align="center" min-width="168" title="活动库存">
                  <template #default="{ row: sku }">
                    <InputNumber
                      v-model:value="sku.productConfig.stock"
                      :max="sku.stock"
                      :min="0"
                      class="w-full"
                    />
                  </template>
                </VxeColumn>
              </template>
            </SpuAndSkuList>
          </div>
        </template>
      </Form>
    </Modal>
 
    <!-- 商品选择器弹窗(单选模式) -->
    <SpuSkuSelect
      ref="spuSkuSelectRef"
      :is-select-sku="true"
      :radio="true"
      @select="handleSpuSelected"
    />
  </div>
</template>