叶静 1 月之前
父節點
當前提交
05abc20940
共有 1 個文件被更改,包括 58 次插入18 次删除
  1. 58 18
      src/app/shop/admin/goods/goods/select.vue

+ 58 - 18
src/app/shop/admin/goods/goods/select.vue

@@ -103,7 +103,7 @@ const currentSearchParams = ref({});
 const loading = ref(true);
 const table = reactive({
   list: [],
-  ids: props.modal.params.ids || [],
+  ids: (props.modal.params.ids || []).map(id => Number(id)), // 确保ID为数字类型
   selectedGoods: new Map(), // 使用Map存储选中的商品完整数据,key为商品id
   preloadedGoods: new Map(), // 存储预加载的商品数据
 });
@@ -117,7 +117,7 @@ async function getData(page, searchParams = null) {
 
     const params = {
       page: pageData.page,
-      size: pageData.size,
+      size: 20,
       isShow: 1,
       ...finalSearchParams,
     };
@@ -166,7 +166,8 @@ function setDefaultSelected() {
   }
 
   table.list.forEach((item) => {
-    if (table.ids?.includes(item.id)) {
+    const itemId = Number(item.id);
+    if (table.ids?.includes(itemId)) {
       multipleTableRef.value?.toggleRowSelection(item, true);
       toggleRowSelection('row', [item], item);
     }
@@ -175,17 +176,18 @@ function setDefaultSelected() {
 
 const multipleTableRef = ref();
 function selectRow(selection, row) {
+  const rowId = Number(row.id);
   if (
     !props.modal.params.max ||
     (props.modal.params.max && props.modal.params.max > table.ids.length)
   ) {
-    if (table.ids.includes(row.id)) {
-      let index = table.ids.findIndex((id) => id == row.id);
+    if (table.ids.includes(rowId)) {
+      let index = table.ids.findIndex((id) => id === rowId);
       table.ids.splice(index, 1);
       // 从选中商品数据中移除
-      table.selectedGoods.delete(row.id);
+      table.selectedGoods.delete(rowId);
     } else {
-      table.ids.push(row.id);
+      table.ids.push(rowId);
       // 添加到选中商品数据中
       addToSelectedGoods(row);
     }
@@ -200,18 +202,20 @@ function selectAll(selection) {
     if (selection.length == 0) {
       // 取消全选:移除当前页面的所有选中项
       table.list.forEach((l) => {
-        if (table.ids.includes(l.id)) {
-          let index = table.ids.findIndex((id) => id == l.id);
+        const lId = Number(l.id);
+        if (table.ids.includes(lId)) {
+          let index = table.ids.findIndex((id) => id === lId);
           table.ids.splice(index, 1);
           // 从选中商品数据中移除
-          table.selectedGoods.delete(l.id);
+          table.selectedGoods.delete(lId);
         }
       });
     } else {
       // 全选:添加当前页面的所有项
       table.list.forEach((l) => {
-        if (!table.ids.includes(l.id)) {
-          table.ids.push(l.id);
+        const lId = Number(l.id);
+        if (!table.ids.includes(lId)) {
+          table.ids.push(lId);
           // 添加到选中商品数据中
           addToSelectedGoods(l);
         }
@@ -229,7 +233,8 @@ function toggleRowSelection(type, selection, row) {
     } else if (type == 'all') {
       multipleTableRef.value?.clearSelection();
       table.list.forEach((l) => {
-        if (table.ids?.includes(l.id)) {
+        const lId = Number(l.id);
+        if (table.ids?.includes(lId)) {
           multipleTableRef.value?.toggleRowSelection(l, true);
         }
       });
@@ -245,19 +250,21 @@ function toggleRowSelection(type, selection, row) {
 // 添加商品到选中数据集合
 function addToSelectedGoods(item) {
   const goodsData = {
-    id: item.id,
+    id: Number(item.id),
     title: item.title || item.storeName,
     image: item.image,
     price: item.price || item.current_price || item.original_price || item.otPrice,
     stock: item.stock,
   };
-  table.selectedGoods.set(item.id, goodsData);
+  table.selectedGoods.set(Number(item.id), goodsData);
 }
 
 // 初始化时添加已选中的商品数据
 function initSelectedGoods() {
   table.list.forEach((item) => {
-    if (table.ids.includes(item.id)) {
+    const itemId = Number(item.id);
+    // 只添加还未在selectedGoods中的商品,避免覆盖预加载的数据
+    if (table.ids.includes(itemId) && !table.selectedGoods.has(itemId)) {
       addToSelectedGoods(item);
     }
   });
@@ -273,15 +280,42 @@ async function preloadSelectedGoods() {
       try {
         const { code, data } = await api.goods.info(id);
         if (code === '200' && data) {
-          addToSelectedGoods(data);
-          table.preloadedGoods.set(id, data);
+          // 直接添加到selectedGoods中,确保所有已选中的商品都被保存
+          const goodsData = {
+            id: Number(data.id),
+            title: data.title || data.storeName,
+            image: data.image,
+            price: data.price || data.current_price || data.original_price || data.otPrice,
+            stock: data.stock,
+          };
+          table.selectedGoods.set(Number(id), goodsData);
+          table.preloadedGoods.set(Number(id), data);
+        } else {
+          // 即使接口失败,也保留ID的占位信息
+          console.warn(`商品${id}详情获取失败,保留占位信息`);
+          table.selectedGoods.set(Number(id), {
+            id: Number(id),
+            title: `商品 ${id}`,
+            image: '',
+            price: 0,
+            stock: 0,
+          });
         }
       } catch (error) {
         console.warn(`获取商品${id}详情失败:`, error);
+        // 即使接口失败,也保留ID的占位信息
+        table.selectedGoods.set(Number(id), {
+          id: Number(id),
+          title: `商品 ${id}`,
+          image: '',
+          price: 0,
+          stock: 0,
+        });
       }
     });
 
     await Promise.allSettled(promises);
+    console.log('预加载完成,已选商品数量:', table.selectedGoods.size, '已选ID:', Array.from(table.selectedGoods.keys()));
   } catch (error) {
     console.error('预加载选中商品数据失败:', error);
   }
@@ -314,6 +348,12 @@ function confirm() {
   // 将Map转换为数组,获取所有选中的商品数据
   const selectedGoodsData = Array.from(table.selectedGoods.values());
 
+  console.log('提交商品数据:', {
+    总数: selectedGoodsData.length,
+    IDs: selectedGoodsData.map(g => g.id),
+    原始IDs: table.ids
+  });
+
   emit('modalCallBack', {
     event: 'confirm',
     data: selectedGoodsData,