chuweiqiang 1 year ago
parent
commit
b3aeca713d
2 changed files with 65 additions and 47 deletions
  1. 5 2
      app/data/controller/shop/Goods.php
  2. 60 45
      app/data/view/shop/goods/form.html

+ 5 - 2
app/data/controller/shop/Goods.php

@@ -227,7 +227,10 @@ class Goods extends Controller
             $data['data_items'] = json_encode(ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->column($fields, 'goods_spec'), JSON_UNESCAPED_UNICODE);
 
         } elseif ($this->request->isPost()) {
-//            var_dump($data);die;
+//
+//            dump(json_encode($data,JSON_UNESCAPED_UNICODE));
+//
+//            die;
             //审核拒绝的情况
             if (!empty($data['examine_cause'])){
                 $updata = [
@@ -280,7 +283,7 @@ class Goods extends Controller
                 'price_selling'   => $item['selling'],
                 'status'          => $item['status'] ? 1 : 0,
                 'admin_id'        => $data['admin_id'],
-                'show_image'      => $data['cover']
+                'show_image'      => $item['show_image']
             ], 'goods_spec', [
                 'goods_code' => $data['code'],
             ]);

+ 60 - 45
app/data/view/shop/goods/form.html

@@ -218,15 +218,7 @@
                 <a class="layui-btn layui-btn-sm layui-btn-primary" ng-click="addSpecRow(specs)" ng-if="specs.length<3">增加规则分组</a>
                 <p class="margin-top-10"><span class="color-red">请完成属性修改后再编辑下面的规格信息,否则规格数据会丢失!</span></p>
             </div>
-            <td class="text-center text-top padding-0">
-                <div class="help-images">
-                    <!--                            <div>{$vo.cover|default=''}</div>-->
-                    <input name="cover" data-idx="{$vo.id}" data-max-width="500" data-max-height="500" type="hidden" value="{$vo.cover|default=''}">
-                    <script>
-                        $('[name="cover"]').uploadOneImage();
-                    </script>
-                </div>
-            </td>
+
 <!--            <button type="button" class="layui-btn"  id="test1">-->
 <!--                <i class="layui-icon">&#xe67c;</i>上传图片-->
 <!--            </button>-->
@@ -255,44 +247,61 @@
                     <td class="layui-bg-gray nowrap" ng-bind="td.name" ng-if="td.show" ng-repeat="td in rows"></td>
 
                     <td class="padding-0">
-<!--                        &lt;!&ndash;<div class="help-images">-->
-<!--                            &lt;!&ndash;                            <div>{$vo.cover|default=''}</div>&ndash;&gt;-->
-<!--                            <input name="cover" data-idx="{$vo.id}" data-max-width="500" data-max-height="500" type="hidden" value="{$vo.cover|default=''}">-->
-<!--                            <script>-->
-<!--                                $('[name="cover"]').uploadOneImage();-->
-<!--                            </script>-->
-
-<!--                        </div>&ndash;&gt;-->
-
-<!--                        <script src="//unpkg.com/layui@2.6.8/dist/layui.js"></script>-->
-<!--                        <script>-->
-<!--                            layui.use('upload', function(){-->
-<!--                                var upload = layui.upload;-->
-<!--                                console.log('开始上传')-->
-<!--                                //执行实例-->
-<!--                                var uploadInst = upload.render({-->
-<!--                                    elem: '.test1' //绑定元素-->
-<!--                                    ,url: '/admin/api.upload/file' //上传接口-->
-<!--                                    ,done: function(res, index, upload){-->
-<!--                                        //上传完毕回调-->
-<!--                                        $('.show_image1').attr('src', res.data.url);-->
-<!--                                        console.log('上传图片回调',res)-->
-<!--                                    }-->
-<!--                                    ,error: function(err){-->
-<!--                                        //请求异常回调-->
-<!--                                        console.log('请求异常回调',err)-->
-<!--                                    }-->
-<!--                                });-->
-<!--                            });-->
-<!--                        </script>-->
-<!--                        <button type="button" class="layui-btn test1">-->
-<!--                            <i class="layui-icon">&#xe67c;</i>上传图片-->
-<!--                        </button>-->
-<!--&lt;!&ndash;                        <input  type="file" class="uploadFile">&ndash;&gt;-->
+
+
+                        <script>
+                            $(document).ready(function() {
+                                $('.uploadFile').change(function(){ //当文件被选择时触发事件
+                                    console.log('要上传的图片元素',$(this))
+                                    var parents = $(this).parent()
+                                    var file = $(this)[0].files[0]; //获取选择的文件对象
+
+                                    if (file) {
+                                        var formData = new FormData(); //创建FormData对象
+
+                                        formData.append('file', file); //将文件对象添加到formData中
+
+                                        $.ajax({
+                                            url: '/admin/api.upload/file', //设置服务器URL地址
+                                            method: 'POST', //指定HTTP请求类型为POST
+                                            dataType: 'json', //期望返回JSON格式数据
+
+                                            processData: false, //不进行数据转换
+                                            contentType: false, //不设置Content-Type头部信息
+
+                                            data: formData, //将表单数据作为参数传递给服务器
+
+                                            success: function(response){ //成功回调函数
+                                                // console.log("文件上传成功!",rows);
+                                                parents.find('.layui-input').val(response.data.url)
+                                                // parents.find('.layui-input').focus()
+                                                parents.find('.layui-input').blur()
+                                                parents.find('.show_image').attr('src',response.data.url)
+                                                parents.find('.show_image').attr('data-lazy-src',response.data.url)
+                                            },
+
+                                            error: function(xhr, status, error){ //错误回调函数
+                                                console.error("文件上传失败!", xhr, status, error);
+
+                                                //根据错误情况进行相应的处理
+                                                //...
+                                            }
+                                        });
+                                    } else {
+                                        console.warn("未选择任何文件!");
+                                    }
+                                });
+                            });
+                        </script>
+                        <input type="file"  class="uploadFile">
+                        <input type="hidden" class="layui-input border-0 padding-left-0 text-center" ng-blur="rows[0].show_image=setValue(rows[0].key,'show_image',$event.target.value)" ng-model="rows[0].show_image">
+
                         {if isset($vo.cover)}
                             <img src="{{rows[0].show_image}}" class="show_image" data-tips-hover data-tips-image data-lazy-src="{{rows[0].show_image}}">
-                                                    <!--                        <input  ng-model="rows[0].show_image">-->
+
+<!--                            <input  type="hidden" name="data_items[$index][show_image]">-->
                         {/if}
+
                     </td>
 
                     <td class="padding-0">
@@ -400,7 +409,7 @@
                 formData.append('image', file); //将文件对象添加到formData中
 
                 $.ajax({
-                    url: '/admin/api.upload/state', //设置服务器URL地址
+                    url: '/admin/api.upload/file', //设置服务器URL地址
                     method: 'POST', //指定HTTP请求类型为POST
                     dataType: 'json', //期望返回JSON格式数据
 
@@ -524,6 +533,7 @@
                         if (fixed !== null) value = (parseFloat(value) || 0).toFixed(fixed);
                         $rootScope.items.forEach(function (rows) {
                             rows.forEach(function (item) {
+                                console.log(value);
                                 item[name] = value;
                             });
                         });
@@ -541,6 +551,8 @@
                 if (typeof callback === 'string' && callback.indexOf('_') > -1) {
                     value = eval(callback.replace('_', "'" + value + "'"));
                 }
+                console.log(name)
+                console.log(value)
                 return $rootScope.cache[key][name] = $rootScope.items[key][name] = value;
             };
             $rootScope.getValue = function (key, name, value) {
@@ -564,9 +576,11 @@
                         if (item.check && item.name.length > 0) {
                             item.show = true, item.group = spec.name;
                             temp.push(item);
+
                         }
                     });
                     data.push(temp), navs.push(spec.name);
+
                 });
                 $rootScope.navas = navs;
                 /*! 表格交叉 */
@@ -600,6 +614,7 @@
                         item.express = $rootScope.getValue(item.key, 'express', '1');
                         item.virtual = $rootScope.getValue(item.key, 'virtual', '0');
                         item.show_image = $rootScope.getValue(item.key, 'show_image', '');
+
                         return false;
                     });
                 });