WIN-2308041133\Administrator 1 settimana fa
parent
commit
3db77049a2

+ 16 - 8
app/admin/controller/store/StoreProduct.php

@@ -268,15 +268,23 @@ class StoreProduct extends AuthController
                 $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
                 $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
             }
             }
             $data['productInfo'] = $productInfo;
             $data['productInfo'] = $productInfo;
+
+            $level = SystemUserLevel::where('status', 1)->order('grade asc')->field(['id', 'grade', 'name as level_name'])->select()->toArray();
+            $level_radio = [];
+            foreach ($level as $k => $v){
+                $level_radio_lst = StoreProductLevel::where('product_id', $id)->where('level_id',$v['id'])->where('type', 1)->find()->toArray();
+                $level_radio[] = ['grade'=>1,'level_name' => $v['level_name'], 'ratio' => $level_radio_lst['rebate_ratio']];
+            }
+//            $level_radio = [
+//                ['grade'=>1,'level_name' => '青源客', 'ratio' => 1.5],
+//                ['grade'=>2,'level_name' => '青润使', 'ratio' => 2.0],
+//                ['grade'=>3,'level_name' => '青金团', 'ratio' => 3.0],
+//                ['grade'=>4,'level_name' => '青玉团', 'ratio' => 4.0],
+//                ['grade'=>5,'level_name' => '青谷团', 'ratio' => 5.0]
+//            ];
+            $data['level_radio'] = $level_radio;
         }
         }
-        $level_radio = [
-            ['grade'=>1,'level_name' => '青源客', 'ratio' => 1.5],
-            ['grade'=>2,'level_name' => '青润使', 'ratio' => 2.0],
-            ['grade'=>3,'level_name' => '青金团', 'ratio' => 3.0],
-            ['grade'=>4,'level_name' => '青玉团', 'ratio' => 4.0],
-            ['grade'=>5,'level_name' => '青谷团', 'ratio' => 5.0]
-        ];
-        $data['level_radio'] = $level_radio;
+
 //        $data['level_set'] = 1;
 //        $data['level_set'] = 1;
         return JsonService::successful($data);
         return JsonService::successful($data);
     }
     }

+ 91 - 65
app/admin/view/store/store_product/create.php

@@ -1152,7 +1152,7 @@
 
 
             },
             },
             /**
             /**
-             * 获取商品信息
+             * 获取商品信息 - 修复等级返利数据填充问题
              * */
              * */
             getProductInfo: function () {
             getProductInfo: function () {
                 var that = this;
                 var that = this;
@@ -1166,68 +1166,65 @@
                     that.$set(that, 'merList', res.data.merList);
                     that.$set(that, 'merList', res.data.merList);
                     var productInfo = res.data.productInfo || {};
                     var productInfo = res.data.productInfo || {};
                     if (productInfo.id && that.id) {
                     if (productInfo.id && that.id) {
+                        // 将产品信息赋值给formData
                         that.$set(that, 'formData', productInfo);
                         that.$set(that, 'formData', productInfo);
-                        // 处理等级返利数据 - 确保有grade字段
+
+                        // 修复等级返利数据填充问题
+                        console.log('获取到的level_radio数据:', productInfo.level_radio);
+
+                        // 初始化默认的等级数组
+                        var defaultLevels = [
+                            { level_name: '青源客', ratio: '', grade: 1 },
+                            { level_name: '青润使', ratio: '', grade: 2 },
+                            { level_name: '青金团', ratio: '', grade: 3 },
+                            { level_name: '青玉团', ratio: '', grade: 4 },
+                            { level_name: '青谷团', ratio: '', grade: 5 }
+                        ];
+
+                        // 检查是否有保存的等级返利数据
                         if (productInfo.level_radio && productInfo.level_radio.length > 0) {
                         if (productInfo.level_radio && productInfo.level_radio.length > 0) {
-                            // 如果后台返回的数据有grade字段,直接使用
-                            // 如果没有,补充grade字段
-                            var processedLevelRadio = productInfo.level_radio.map(function(item, index) {
-                                // 确保有grade字段,如果没有则根据位置添加
-                                if (!item.hasOwnProperty('grade')) {
-                                    // 根据等级名称匹配grade
-                                    var gradeMap = {
-                                        '青源客': 1,
-                                        '青润使': 2,
-                                        '青金团': 3,
-                                        '青玉团': 4,
-                                        '青谷团': 5
-                                    };
-                                    var grade = gradeMap[item.level_name] || (index + 1);
-                                    return {
-                                        level_name: item.level_name,
-                                        ratio: item.ratio,
-                                        grade: grade
-                                    };
-                                }
-                                return item;
-                            });
-                            // 确保有5个等级,按grade排序
-                            var finalLevelRadio = [];
-                            for (var i = 1; i <= 5; i++) {
-                                var found = processedLevelRadio.find(function(item) {
-                                    return item.grade == i;
+                            console.log('有保存的等级返利数据,开始处理...');
+
+                            // 遍历后台返回的数据,更新默认数组中的比例
+                            productInfo.level_radio.forEach(function(savedItem) {
+                                // 查找对应的等级
+                                var foundIndex = defaultLevels.findIndex(function(defaultItem) {
+                                    // 优先使用grade匹配,如果没有grade则使用level_name匹配
+                                    if (savedItem.grade) {
+                                        return defaultItem.grade == savedItem.grade;
+                                    } else {
+                                        return defaultItem.level_name === savedItem.level_name;
+                                    }
                                 });
                                 });
-                                if (found) {
-                                    finalLevelRadio.push(found);
-                                } else {
-                                    // 如果没有该等级,创建默认值
-                                    var levelNameMap = {
-                                        1: '青源客',
-                                        2: '青润使',
-                                        3: '青金团',
-                                        4: '青玉团',
-                                        5: '青谷团'
-                                    };
-                                    finalLevelRadio.push({
-                                        level_name: levelNameMap[i] || '未知等级',
-                                        ratio: '',
-                                        grade: i
-                                    });
+
+                                if (foundIndex !== -1) {
+                                    // 更新比例
+                                    defaultLevels[foundIndex].ratio = savedItem.ratio || '';
+                                    console.log('更新等级 ' + defaultLevels[foundIndex].level_name + ' 的比例为: ' + defaultLevels[foundIndex].ratio);
                                 }
                                 }
-                            }
-                            that.$set(that, 'level_radio', finalLevelRadio);
+                            });
                         } else {
                         } else {
-                            // 如果没有数据,使用默认值
-                            that.$set(that, 'level_radio', [
-                                { level_name: '青源客', ratio: '', grade: 1 },
-                                { level_name: '青润使', ratio: '', grade: 2 },
-                                { level_name: '青金团', ratio: '', grade: 3 },
-                                { level_name: '青玉团', ratio: '', grade: 4 },
-                                { level_name: '青谷团', ratio: '', grade: 5 }
-                            ]);
+                            console.log('没有保存的等级返利数据,使用默认值');
                         }
                         }
+
+                        // 设置到Vue实例
+                        that.$set(that, 'level_radio', defaultLevels);
+                        console.log('最终设置的level_radio:', defaultLevels);
+
+                        // 生成商品规格
                         that.generate();
                         that.generate();
+                    } else {
+                        // 新增商品时使用默认值
+                        that.$set(that, 'level_radio', [
+                            { level_name: '青源客', ratio: '', grade: 1 },
+                            { level_name: '青润使', ratio: '', grade: 2 },
+                            { level_name: '青金团', ratio: '', grade: 3 },
+                            { level_name: '青玉团', ratio: '', grade: 4 },
+                            { level_name: '青谷团', ratio: '', grade: 5 }
+                        ]);
                     }
                     }
+
+                    // 获取规则列表并初始化
                     that.getRuleList();
                     that.getRuleList();
                     that.init();
                     that.init();
                 }).catch(function (res) {
                 }).catch(function (res) {
@@ -1480,7 +1477,8 @@
                 });
                 });
                 //实例化编辑器
                 //实例化编辑器
                 this.um = UM.getEditor('myEditor', {initialFrameWidth: '99%', initialFrameHeight: 400});
                 this.um = UM.getEditor('myEditor', {initialFrameWidth: '99%', initialFrameHeight: 400});
-                this.um.setContent(that.formData.description);
+                // 注意:这里使用Vue实例的formData.description,而不是直接设置
+                // 我们会在mounted后通过getProductInfo获取数据后设置
                 that.$nextTick(function () {
                 that.$nextTick(function () {
                     layui.use(['form', 'element'], function () {
                     layui.use(['form', 'element'], function () {
                         that.form = layui.form;
                         that.form = layui.form;
@@ -1646,22 +1644,32 @@
                     }
                     }
                 }
                 }
 
 
-                // 处理等级返利数据
+                // 处理等级返利数据 - 确保传递当前填写的level_radio
                 that.formData.level_radio = that.level_radio;
                 that.formData.level_radio = that.level_radio;
+                console.log('提交的level_radio数据:', that.level_radio);
 
 
-                // 验证等级返利数据
+                // 验证等级返利数据(改为可选验证,可以不填)
+                var hasLevelRatio = false;
                 for (var i = 0; i < that.level_radio.length; i++) {
                 for (var i = 0; i < that.level_radio.length; i++) {
                     var ratio = that.level_radio[i].ratio;
                     var ratio = that.level_radio[i].ratio;
-                    if (ratio === '' || ratio === null || ratio === undefined) {
-                        return that.showMsg('请填写所有等级的返利比例');
-                    }
-                    var ratioNum = parseFloat(ratio);
-                    if (isNaN(ratioNum) || ratioNum < 0 || ratioNum > 100) {
-                        return that.showMsg('等级返利比例必须在0-100之间');
+                    if (ratio && ratio !== '') {
+                        hasLevelRatio = true;
+                        var ratioNum = parseFloat(ratio);
+                        if (isNaN(ratioNum) || ratioNum < 0 || ratioNum > 100) {
+                            return that.showMsg('等级返利比例必须在0-100之间');
+                        }
                     }
                     }
                 }
                 }
 
 
+                // 如果有填写等级返利,则设置到formData,否则设置为空数组
+                if (!hasLevelRatio) {
+                    that.formData.level_radio = [];
+                }
+
+                // 获取编辑器内容
                 that.formData.description = that.getContent();
                 that.formData.description = that.getContent();
+
+                // 提交数据
                 that.requestPost(that.U({
                 that.requestPost(that.U({
                     c: 'store.StoreProduct',
                     c: 'store.StoreProduct',
                     a: 'save',
                     a: 'save',
@@ -1747,7 +1755,11 @@
         mounted: function () {
         mounted: function () {
             var that = this;
             var that = this;
             axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
             axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
+
+            // 获取商品信息
             that.getProductInfo();
             that.getProductInfo();
+
+            // 设置全局变量
             window.$vm = that;
             window.$vm = that;
             window.changeIMG = that.changeIMG;
             window.changeIMG = that.changeIMG;
             window.insertEditor = that.insertEditor;
             window.insertEditor = that.insertEditor;
@@ -1755,6 +1767,8 @@
             window.successFun = function () {
             window.successFun = function () {
                 that.getRuleList(1);
                 that.getRuleList(1);
             }
             }
+
+            // 视频上传
             $(that.$refs.filElem).change(function () {
             $(that.$refs.filElem).change(function () {
                 var inputFile = this.files[0];
                 var inputFile = this.files[0];
                 that.requestPost(that.U({c: "widget.video", a: 'get_signature'})).then(function (res) {
                 that.requestPost(that.U({c: "widget.video", a: 'get_signature'})).then(function (res) {
@@ -1784,7 +1798,19 @@
                 }).catch(function (res) {
                 }).catch(function (res) {
                     return that.showMsg(res.msg || '获取密钥失败,请检查您的配置');
                     return that.showMsg(res.msg || '获取密钥失败,请检查您的配置');
                 });
                 });
-            })
+            });
+
+            // 监听编辑器内容变化,更新到formData
+            that.$nextTick(function() {
+                // 使用setTimeout确保编辑器已初始化
+                setTimeout(function() {
+                    if (that.um) {
+                        that.um.addListener('contentChange', function() {
+                            that.formData.description = that.um.getContent();
+                        });
+                    }
+                }, 1000);
+            });
         }
         }
     });
     });
 </script>
 </script>