Browse Source

fix:商品后台

super-yimizi 2 months ago
parent
commit
d27d0a60dc

+ 2 - 2
application/admin/controller/shop/Goods.php

@@ -703,7 +703,7 @@ class Goods extends Backend
         
         
         // 查询SKU数据
         // 查询SKU数据
         $list = $this->sku_model->where('goods_id', $row->id)->select();
         $list = $this->sku_model->where('goods_id', $row->id)->select();
-        
+    
         // 构造规格数据结构
         // 构造规格数据结构
         $spec_data = [];
         $spec_data = [];
         if ($row->spec_type == 1) { // 多规格商品
         if ($row->spec_type == 1) { // 多规格商品
@@ -742,7 +742,7 @@ class Goods extends Backend
         $this->assignconfig('goods', $row);
         $this->assignconfig('goods', $row);
         $this->assignconfig('goods_skus', $list);
         $this->assignconfig('goods_skus', $list);
         $this->assignconfig('spec_data', $spec_data); // 传递规格数据结构
         $this->assignconfig('spec_data', $spec_data); // 传递规格数据结构
-        
+
         return $this->view->fetch();
         return $this->view->fetch();
     }
     }
 
 

+ 1 - 1
application/admin/view/shop/goods/add_sku.html

@@ -1,4 +1,4 @@
-<style>
+             <style>
     #vue-app .row .input-group {
     #vue-app .row .input-group {
         margin-bottom: 10px;
         margin-bottom: 10px;
     }
     }

+ 179 - 102
public/assets/js/backend/shop/goods.js

@@ -625,29 +625,29 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
             // 为表格绑定事件
             // 为表格绑定事件
             Table.api.bindevent(table);
             Table.api.bindevent(table);
         },
         },
-        getAttribute: function (category_id, attribute_ids = []) {
-            $.get('shop/attribute/attrs?category_id=' + category_id, function (res) {
-                const {code, data, msg} = res;
-                if (code) {
-                    $('#attributes').html(data.length ? Template('attributetpl', {row: data, attribute_ids}) : ' <input name="row[attribute_ids][0][]" type="hidden" value="">');
-                    var instance = $("form[data-toggle='validator']").data("validator");
-                    $('#attributes input').each(function () {
-                        instance._parse(this);
-                    });
-                } else {
-                    Toastr.error(msg);
-                }
-            })
-        },
+        // getAttribute: function (category_id, attribute_ids = []) {
+        //     $.get('shop/attribute/attrs?category_id=' + category_id, function (res) {
+        //         const {code, data, msg} = res;
+        //         if (code) {
+        //             $('#attributes').html(data.length ? Template('attributetpl', {row: data, attribute_ids}) : ' <input name="row[attribute_ids][0][]" type="hidden" value="">');
+        //             var instance = $("form[data-toggle='validator']").data("validator");
+        //             $('#attributes input').each(function () {
+        //                 instance._parse(this);
+        //             });
+        //         } else {
+        //             Toastr.error(msg);
+        //         }
+        //     })
+        // },
         add: function () {
         add: function () {
             let that = this;
             let that = this;
             
             
             // 等待DOM完全加载后再执行
             // 等待DOM完全加载后再执行
             $(document).ready(function() {
             $(document).ready(function() {
-                $('#c-category_id, #c-category_ids').on('change', function () {
-                    let category_id = $(this).val();
-                    that.getAttribute(category_id);
-                });
+              // $('#c-category_id, #c-category_ids').on('change', function () {
+                //     let category_id = $(this).val();
+                //     that.getAttribute(category_id);
+                // });  
                 
                 
                 // 初始化商品类型卡片交互
                 // 初始化商品类型卡片交互
                 that.initGoodsTypeCards();
                 that.initGoodsTypeCards();
@@ -2002,9 +2002,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
             console.log('商品分类值:', categoryVal);
             console.log('商品分类值:', categoryVal);
             if ($categoryId.length > 0 && $categoryId.attr('data-rule') && $categoryId.attr('data-rule').indexOf('required') !== -1) {
             if ($categoryId.length > 0 && $categoryId.attr('data-rule') && $categoryId.attr('data-rule').indexOf('required') !== -1) {
                 if (Controller.isMultiSelectEmpty(categoryVal)) {
                 if (Controller.isMultiSelectEmpty(categoryVal)) {
-                    isValid = false;
-                    errors.push('商品分类不能为空');
-                    console.log('商品分类验证失败');
+                    // isValid = false;
+                    // errors.push('商品分类不能为空');
+                    // console.log('商品分类验证失败');
                 }
                 }
             }
             }
             
             
@@ -2269,28 +2269,6 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                     container: 'body'
                     container: 'body'
                 });
                 });
 
 
-                $(document).on("click", ".btn-legal", function (a) {
-                    Fast.api.ajax({
-                        url: "shop/ajax/check_content_islegal",
-                        data: {content: $("#c-content").val()}
-                    }, function (data, ret) {
-
-                    }, function (data, ret) {
-                        if ($.isArray(data)) {
-                            Layer.alert(__('Banned words') + ":" + data.join(","));
-                        }
-                    });
-                });
-                $(document).on("click", ".btn-keywords", function (a) {
-                    Fast.api.ajax({
-                        url: "shop/ajax/get_content_keywords",
-                        data: {title: $("#c-title").val(), content: $("#c-content").val()}
-                    }, function (data, ret) {
-                        $("#c-keywords").val(data.keywords);
-                        $("#c-description").val(data.description);
-                    });
-                });
-
                 // 绑定FastAdmin表单验证事件
                 // 绑定FastAdmin表单验证事件
                 var $form = $("form[role=form]");
                 var $form = $("form[role=form]");
                 
                 
@@ -2601,12 +2579,26 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                             init() {
                             init() {
                                 console.log('Vue组件初始化开始');
                                 console.log('Vue组件初始化开始');
                                 console.log('Config数据:', Config);
                                 console.log('Config数据:', Config);
+                                console.log('Config.goods:', Config.goods);
+                                console.log('Config.goods_skus:', Config.goods_skus);
+                                console.log('Config.spec_data:', Config.spec_data);
                                 
                                 
                                 let skus = [];
                                 let skus = [];
                                 if (Config.goods_skus && Config.goods_skus.length) {
                                 if (Config.goods_skus && Config.goods_skus.length) {
                                     console.log('发现商品SKU数据,数量:', Config.goods_skus.length);
                                     console.log('发现商品SKU数据,数量:', Config.goods_skus.length);
                                     console.log('完整SKU数据:', Config.goods_skus);
                                     console.log('完整SKU数据:', Config.goods_skus);
                                     
                                     
+                                    // 检查SKU数据的字段类型
+                                    Config.goods_skus.forEach((sku, index) => {
+                                        console.log('SKU[' + index + '] 字段详情:', {
+                                            id: sku.id,
+                                            price: sku.price,
+                                            price_type: typeof sku.price,
+                                            spec_value_ids: sku.spec_value_ids,
+                                            sku_attr: sku.sku_attr
+                                        });
+                                    });
+                                    
                                     // 如果是多规格商品,使用新的规格数据结构
                                     // 如果是多规格商品,使用新的规格数据结构
                                     if (Config.goods && Config.goods.spec_type == '1') {
                                     if (Config.goods && Config.goods.spec_type == '1') {
                                         console.log('初始化多规格数据');
                                         console.log('初始化多规格数据');
@@ -2619,17 +2611,29 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                                 name: spec.name,
                                                 name: spec.name,
                                                 expanded: true, // 编辑模式下规格默认展开
                                                 expanded: true, // 编辑模式下规格默认展开
                                                 value: spec.value.map(val => ({
                                                 value: spec.value.map(val => ({
+                                                    id: val.id,
                                                     name: val.name,
                                                     name: val.name,
                                                     image: val.image || ''
                                                     image: val.image || ''
                                                 }))
                                                 }))
                                             }));
                                             }));
                                         }
                                         }
                                         
                                         
-                                        // 构建SKU映射表
-                                        for (let item of Config.goods_skus) {
-                                            console.log('处理SKU项:', item);
+                                        // 构建SKU映射表 - 使用多种方式映射
+                                        Config.goods_skus.forEach((item, skuIndex) => {
+                                            console.log('处理SKU项[' + skuIndex + ']:', item);
+                                            
+                                            // 方法1:使用SKU索引
+                                            skus[skuIndex] = item;
+                                            
+                                            // 方法2:使用spec_value_ids
+                                            if (item.spec_value_ids) {
+                                                let specValueIds = item.spec_value_ids.split(',').sort();
+                                                let idsKey = specValueIds.join(',');
+                                                skus[idsKey] = item;
+                                            }
+                                            
+                                            // 方法3:使用规格值名称组合
                                             if (item.spec_value_ids) {
                                             if (item.spec_value_ids) {
-                                                // 使用spec_value_ids构建映射键
                                                 let specValueIds = item.spec_value_ids.split(',');
                                                 let specValueIds = item.spec_value_ids.split(',');
                                                 let specValueNames = [];
                                                 let specValueNames = [];
                                                 
                                                 
@@ -2645,16 +2649,21 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                                     }
                                                     }
                                                 }
                                                 }
                                                 
                                                 
-                                                let attrKey = specValueNames.join(',');
-                                                skus[attrKey] = item;
-                                                console.log('SKU映射:', attrKey, '->', item);
+                                                if (specValueNames.length > 0) {
+                                                    let namesKey = specValueNames.join(',');
+                                                    skus[namesKey] = item;
+                                                    console.log('SKU映射[名称]:', namesKey, '->', item.price);
+                                                }
                                             }
                                             }
-                                        }
+                                            
+                                            console.log('SKU映射[索引]:', skuIndex, '->', item.price);
+                                        });
                                         
                                         
                                         this.skus = skus;
                                         this.skus = skus;
                                         this.specList = specList;
                                         this.specList = specList;
                                         console.log('多规格初始化完成,规格数量:', specList.length);
                                         console.log('多规格初始化完成,规格数量:', specList.length);
                                         console.log('规格列表:', specList);
                                         console.log('规格列表:', specList);
+                                        console.log('SKU映射表:', skus);
                                         
                                         
                                         // 延迟触发数据渲染,确保DOM已准备好
                                         // 延迟触发数据渲染,确保DOM已准备好
                                         this.$nextTick(() => {
                                         this.$nextTick(() => {
@@ -2668,6 +2677,24 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                                 }, 500);
                                                 }, 500);
                                             }
                                             }
                                         });
                                         });
+                                    } else {
+                                        // 单规格商品,直接构建SKU映射
+                                        console.log('初始化单规格数据');
+                                        if (Config.goods_skus && Config.goods_skus.length > 0) {
+                                            let item = Config.goods_skus[0];
+                                            skus[''] = item; // 单规格使用空字符串作为键
+                                            skus[0] = item; // 同时使用索引0作为键,以便更容易找到
+                                            console.log('单规格SKU映射:', skus);
+                                        }
+                                        
+                                        this.skus = skus;
+                                        this.specList = [];
+                                        
+                                        // 单规格商品也需要触发数据渲染
+                                        this.$nextTick(() => {
+                                            console.log('Vue nextTick: 准备渲染单规格表格数据');
+                                            this.renderTableData([]);
+                                        });
                                     }
                                     }
                                 }
                                 }
                                 
                                 
@@ -2740,43 +2767,71 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                 };
                                 };
                                 
                                 
                                 let columns = [];
                                 let columns = [];
-                                this.result = [];
-                                this.resetSpec(list, 0);
                                 
                                 
                                 console.log('渲染表格数据,模式:', isEditMode ? '编辑' : '新增');
                                 console.log('渲染表格数据,模式:', isEditMode ? '编辑' : '新增');
-                                console.log('固定默认值:', defaultValues);
-                                console.log('SKU数据映射:', this.skus);
+                                console.log('规格列表:', list);
+                                console.log('商品SKU数据:', Config.goods_skus);
                                 
                                 
-                                //渲染表格数据
-                                this.result.forEach((item, index) => {
-                                    let su = this.skus[item];
-                                    console.log('处理规格组合[' + index + ']:', item, '对应SKU数据:', su);
-                                    
-                                    let row;
+                                if (isEditMode && Config.goods_skus && Config.goods_skus.length > 0) {
+                                    // 编辑模式:直接使用goods_skus数据渲染表格
+                                    console.log('编辑模式:直接使用goods_skus数据');
                                     
                                     
-                                    if (isEditMode && su) {
-                                        // 编辑模式:完全使用后台SKU数据
-                                        console.log('编辑模式:使用后台SKU数据:', su);
-                                        row = {
-                                            skus: item ? item.split(',') : [],
-                                            sku_sn: su.sku_sn || '',
-                                            image: su.image || '',
-                                            price: parseFloat(su.price) || 0,
-                                            lineation_price: parseFloat(su.lineation_price) || 0,
-                                            cost_price: parseFloat(su.cost_price) || 0,
-                                            weight: parseFloat(su.weight) || 0,
-                                            volume: parseFloat(su.volume) || 0,
-                                            stocks: parseInt(su.stocks) || 0,
-                                            sales: parseInt(su.sales) || 0,
-                                            status: su.status !== undefined ? parseInt(su.status) : 1,
-                                            is_default: su.is_default !== undefined ? parseInt(su.is_default) : 0
+                                    Config.goods_skus.forEach((sku, index) => {
+                                        console.log('处理SKU[' + index + ']:', sku);
+                                        
+                                        // 解析sku_attr获取规格值组合
+                                        let skuSpecs = [];
+                                        if (sku.sku_attr) {
+                                            try {
+                                                let attrs = JSON.parse(sku.sku_attr);
+                                                skuSpecs = attrs.map(attr => attr.value);
+                                            } catch (e) {
+                                                console.warn('解析sku_attr失败:', sku.sku_attr);
+                                            }
+                                        }
+                                        
+                                        let row = {
+                                            skus: skuSpecs,
+                                            sku_sn: sku.sku_sn || '',
+                                            image: sku.image || '',
+                                            price: parseFloat(sku.price) || 0,
+                                            lineation_price: parseFloat(sku.lineation_price) || 0,
+                                            cost_price: parseFloat(sku.cost_price) || 0,
+                                            weight: parseFloat(sku.weight) || 0,
+                                            volume: parseFloat(sku.volume) || 0,
+                                            stocks: parseInt(sku.stocks) || 0,
+                                            sales: parseInt(sku.sales) || 0,
+                                            status: parseInt(sku.status) || 1,
+                                            is_default: parseInt(sku.is_default) || 0
                                         };
                                         };
-                                        console.log('编辑模式最终行数据:', row);
-                                    } else {
-                                        // 新增模式或编辑模式下没有对应SKU数据:使用固定默认值
-                                        console.log('新增模式:使用固定默认值');
-                                        row = {
-                                            skus: item ? item.split(',') : [],
+                                        
+                                        console.log('编辑模式SKU行数据[' + index + ']:', {
+                                            价格: row.price,
+                                            库存: row.stocks,
+                                            状态: row.status,
+                                            规格: row.skus
+                                        });
+                                        
+                                        columns.push(row);
+                                    });
+                                    
+                                    // 设置默认规格索引
+                                    this.defaultSpecIndex = columns.findIndex(item => item.is_default === 1);
+                                    if (this.defaultSpecIndex === -1 && columns.length > 0) {
+                                        this.defaultSpecIndex = 0; // 如果没有默认规格,使用第一个
+                                    }
+                                    
+                                } else {
+                                    // 新增模式:根据规格组合生成SKU
+                                    console.log('新增模式:根据规格组合生成SKU');
+                                    
+                                    // 检查是否为单规格商品
+                                    const isSingleSpec = !list || list.length === 0;
+                                    
+                                    if (isSingleSpec) {
+                                        // 单规格商品
+                                        let row = {
+                                            skus: [],
                                             sku_sn: defaultValues.goods_sn,
                                             sku_sn: defaultValues.goods_sn,
                                             image: '',
                                             image: '',
                                             price: defaultValues.price,
                                             price: defaultValues.price,
@@ -2786,38 +2841,60 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                             volume: defaultValues.volume,
                                             volume: defaultValues.volume,
                                             stocks: defaultValues.stocks,
                                             stocks: defaultValues.stocks,
                                             sales: 0,
                                             sales: 0,
-                                            status: 1, // 默认显示状态
-                                            is_default: index === 0 ? 1 : 0 // 第一个SKU默认为默认规格
+                                            status: 1,
+                                            is_default: 1
                                         };
                                         };
-                                        console.log('新增模式最终行数据:', row);
-                                    }
-                                    
-                                    // 保留已有的用户修改数据(仅在新增模式下)
-                                    if (!isEditMode) {
-                                        let old = this.tableData[index];
+                                        
+                                        // 保留已有的用户修改数据
+                                        let old = this.tableData[0];
                                         if (old) {
                                         if (old) {
-                                            console.log('新增模式:保留用户修改的数据:', old);
                                             for (let i in row) {
                                             for (let i in row) {
-                                                // 只有在新值为空且旧值不为空时才使用旧值
                                                 if ((row[i] === '' || row[i] === 0 || row[i] === '0.00') && old[i]) {
                                                 if ((row[i] === '' || row[i] === 0 || row[i] === '0.00') && old[i]) {
                                                     row[i] = old[i];
                                                     row[i] = old[i];
                                                 }
                                                 }
                                             }
                                             }
                                         }
                                         }
+                                        
+                                        columns.push(row);
+                                    } else {
+                                        // 多规格商品
+                                        this.result = [];
+                                        this.resetSpec(list, 0);
+                                        
+                                        this.result.forEach((item, index) => {
+                                            let row = {
+                                                skus: item ? item.split(',') : [],
+                                                sku_sn: defaultValues.goods_sn,
+                                                image: '',
+                                                price: defaultValues.price,
+                                                lineation_price: defaultValues.lineation_price,
+                                                cost_price: defaultValues.cost_price,
+                                                weight: defaultValues.weight,
+                                                volume: defaultValues.volume,
+                                                stocks: defaultValues.stocks,
+                                                sales: 0,
+                                                status: 1,
+                                                is_default: index === 0 ? 1 : 0
+                                            };
+                                            
+                                            // 保留已有的用户修改数据
+                                            let old = this.tableData[index];
+                                            if (old) {
+                                                for (let i in row) {
+                                                    if ((row[i] === '' || row[i] === 0 || row[i] === '0.00') && old[i]) {
+                                                        row[i] = old[i];
+                                                    }
+                                                }
+                                            }
+                                            
+                                            columns.push(row);
+                                        });
                                     }
                                     }
-                                    
-                                    columns.push(row);
-                                });
+                                }
                                 
                                 
                                 this.tableData = columns;
                                 this.tableData = columns;
                                 console.log('最终表格数据:', this.tableData);
                                 console.log('最终表格数据:', this.tableData);
                                 
                                 
-                                // 在编辑模式下,设置默认规格的索引
-                                if (isEditMode) {
-                                    this.defaultSpecIndex = this.tableData.findIndex(item => item.is_default === 1);
-                                    console.log('设置默认规格索引:', this.defaultSpecIndex);
-                                }
-                                
                                 //更新dom,加载事件
                                 //更新dom,加载事件
                                 this.$nextTick(function () {
                                 this.$nextTick(function () {
                                     Controller.api.bindUpload();
                                     Controller.api.bindUpload();