Browse Source

refactor(member-center): 优化赠品选择商品过滤逻辑

Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
Sheep 5 days ago
parent
commit
74ba023d0c
1 changed files with 49 additions and 16 deletions
  1. 49 16
      src/views/member-center/edit-member-type/index.vue

+ 49 - 16
src/views/member-center/edit-member-type/index.vue

@@ -578,25 +578,58 @@ function uniqueGoodsList(list: any[]) {
 function choose() {
   const keys = getTableCheckedRowKeys();
   const data = getTableData();
-  closeModal();
 
-  data.forEach(item => {
-    keys.forEach((key: any) => {
-      if (item.skuId === key) {
-        model.value.giftConfigJson.push({
-          skuId: item.skuId,
-          quantity: 1,
-          shopId: item.shopId,
-          channelId: 1,
-          skuName: item.skuName,
-          skuCode: item.skuCode,
-          specName: '-',
-          pic: item.pic,
-          stock: item.shopSkuStocks
-        });
-        model.value.giftConfigJson = uniqueGoodsList(model.value.giftConfigJson);
+  // 找出当前页选中的商品完整数据
+  const selectedItems = data.filter((item: any) => keys.includes(item.skuId));
+
+  // 当前对话框搜索表单中选定的渠道 ID(行数据无单独 channelId 字段,以此作为渠道依据)
+  const currentChannelId: number | undefined = currentChannel.value?.id;
+
+  // 校验1:当前选中的商品必须来自同一门店
+  if (selectedItems.length > 1) {
+    const firstShopId = selectedItems[0].shopId;
+    if (!selectedItems.every((item: any) => item.shopId === firstShopId)) {
+      window.$message?.error('只能选择同一渠道、同一门店的商品,请重新选择');
+      return;
+    }
+  }
+
+  // 校验2:新选商品与已配置赠品必须同一门店、同一渠道
+  if (selectedItems.length > 0 && model.value.giftConfigJson.length > 0) {
+    const firstSelectedShopId = selectedItems[0]?.shopId;
+    // 取已配置中不在本次选中范围内的项作为基准
+    const existingItem = model.value.giftConfigJson.find(
+      (g: any) => !selectedItems.some((s: any) => s.skuId === g.skuId)
+    );
+    if (existingItem) {
+      // 门店不一致
+      if (existingItem.shopId !== firstSelectedShopId) {
+        window.$message?.error('只能选择同一渠道、同一门店的商品,请重新选择');
+        return;
+      }
+      // 渠道不一致:已有配置项的 channelId 与当前选择的渠道不同
+      if (currentChannelId && existingItem.channelId && existingItem.channelId !== currentChannelId) {
+        window.$message?.error('只能选择同一渠道、同一门店的商品,请重新选择');
+        return;
       }
+    }
+  }
+
+  closeModal();
+
+  selectedItems.forEach(item => {
+    model.value.giftConfigJson.push({
+      skuId: item.skuId,
+      quantity: 1,
+      shopId: item.shopId,
+      channelId: currentChannelId ?? 1, // 使用搜索表单中实际选择的渠道,而非硬编码 1
+      skuName: item.skuName,
+      skuCode: item.skuCode,
+      specName: '-',
+      pic: item.pic,
+      stock: item.shopSkuStocks
     });
+    model.value.giftConfigJson = uniqueGoodsList(model.value.giftConfigJson);
   });
 
   console.log(keys, '选择的商品', data);