Przeglądaj źródła

Merge branch 'liuxiangxin' into jun

jun 4 miesięcy temu
rodzic
commit
690b2ce501

+ 32 - 0
app/Http/Controllers/Admin/Product.php

@@ -17,6 +17,7 @@ use App\Models\Product\City as ProductCity;
 use App\Models\ProductPhoto;
 use Illuminate\Support\Facades\DB;
 use App\Models\WeiBan\Tags as WeiBanTags;
+use App\Models\FilesManager;
 
 /**
  * 产品管理
@@ -108,6 +109,37 @@ class Product extends Auth{
 		return $this->fetch();
     }
 
+	/**
+	 * 表格导入
+	 * 
+	 * */
+	public function import_execl( Request $request,Model $Model,FilesManager $FilesManager){
+		// 验证参数
+		// $request->scene('import_execl')->validate();
+		// 获取表格信息
+		$file								= request()->file('order_file');
+		// 返回结果
+		$sheetList							= $FilesManager->excelToProduct($file);
+		// 如果不存在结果
+		if( isset($sheetList['error']) )	return json_send(['code'=>'error','msg'=>$sheetList['error']]);
+		// 循环处理
+		foreach ($sheetList as $value) 		{
+			// 获取详情描述
+			$description					= $value['description'];
+			unset($value['description']);
+			// 写入
+			$id								= $Model->add($value);
+			// 提示新增失败
+			if( !$id )						return json_send(['code'=>'error','msg'=>'新增失败']);
+			// 更新内容
+			$result							= $Model->updateDesc($id,$description);
+			// 提示新增失败
+			if( !$result )					return json_send(['code'=>'error','msg'=>'更新内容失败']);
+		}
+		// 提示成功
+		return								json_send(['code'=>'success','msg'=>'导入成功','path'=>'']);
+	}
+
 	/**
 	 * 添加
 	 * 

+ 2 - 0
app/Http/Middleware/VerifyCsrfToken.php

@@ -30,6 +30,8 @@ class VerifyCsrfToken extends Middleware
         // 编辑器
         'admin/ueditor/upload',
         // 导入微赞订单
+        'admin/product/import_execl',
+        // 导入微赞订单
         'admin/orders/import_execl',
         // 批量更新订单状态
         'admin/orders/import_execl_status',

+ 86 - 0
app/Models/FilesManager.php

@@ -219,6 +219,92 @@ class FilesManager extends Model
         return                                  $sheetList;
     }
 
+
+    /**
+     * 从Excel获取订单数据
+     * 
+     * @param     \Illuminate\Http\UploadedFile   $file     传入的文件
+     * 
+     */
+    public function excelToProduct($file)
+    {
+        // 获取文件后缀名
+        $ext                        = pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
+        // 验证文件格式
+        if (!in_array($ext, ['csv', 'xlsx', 'xls']))    return ['error' => '请上传Excel'];
+        // 读取文件
+        $reader                     = IOFactory::createReader(ucwords($ext))->load($file->getPathname());
+        // 提取数据
+        $sheetList                  = $reader->getActiveSheet()->toArray('');
+        // 表格列标题
+        $column                     = array_shift($sheetList);
+        // 列标题换字段
+        $column                     = $this->columnToProduct($column);
+        // 字段值
+        if( !$column )              return ['error' => '请检查内容表头格式'];
+        // 循环表格数据
+        foreach ($sheetList as $row=>$value)    {
+            // 获取对应的数据
+            $order                  = [];
+            // 循环每个字段,获取值
+            foreach ($value as $kk => $vv)      {
+                // 根据字段索引获取对应的值,转存到订单
+                if( isset($column[$kk]) )         $order[$column[$kk]] = $vv;
+            }
+            // 如果存在的话
+            if( $order['grade'] )    {
+                // 转数组
+                $order['grade']      = explode(',',$order['grade']);
+                // 适读
+                $order['grade']      = empty($order['grade']) ? '' : '【适读'. $order['grade'][0].'-'.$order['grade'][count($order['grade'])-1].'年级】';
+            }
+            // 分类ID
+            $value['category_id']	 = 1;
+            // 时间转换
+            $order['insert_time']    =  $order['insert_time'] ? strtotime($order['insert_time']) : 0;
+            $order['update_time']    =  $order['update_time'] ? strtotime($order['update_time']) : 0;
+            // 库存默认
+            $order['stock']          = 99999;
+            // 适读
+            $order['name']           = $order['grade'];
+            // 描述
+            $order['description']    = str_ireplace('http://img.duibaoduikan.com/','https://edu.dfwy.tech/',$order['description']);
+            // 删除数据
+            unset($order['grade']);
+            // 追加到订单列表
+            $sheetList[$row]         = $order;
+        }
+        // 返回结果
+        return                       $sheetList;
+    }
+
+    /**
+     * 获取列对应的数据库字段名
+     * 
+     */
+    private function columnToProduct($column)
+    {
+        // 字段值
+        $field                              = [];
+        // 循环列标题
+        foreach ($column as $key => $value) {
+
+            if( $value == 'goods_name')    $field[$key] = 'name';
+            if( $value == 'category_id')   $field[$key] = 'type_id';
+            if( $value == 'goods_main_img')$field[$key] = 'thumb';
+            if( $value == 'goods_specs')   $field[$key] = 'spec';
+            if( $value == 'goods_price')   $field[$key] = 'price';
+            if( $value == 'fit_grade' )    $field[$key] = 'grade';
+            if( $value == 'magazine_info') $field[$key] = 'description';
+            if( $value == 'goods_sort')    $field[$key] = 'sort';
+            if( $value == 'status')        $field[$key] = 'status';
+            if( $value == 'create_time')   $field[$key] = 'insert_time';
+            if( $value == 'update_time')   $field[$key] = 'update_time';
+        }
+        // 返回字段值
+        return                          $field;
+    }
+
     /**
      * 获取列对应的数据库字段名
      * 

+ 55 - 0
resources/views/admin/product/index.blade.php

@@ -7,6 +7,9 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 	@if(check_auth('admin/product/add'))
 	<a href="{{url('admin/product/add')}}" class="btn btn-primary">新增</a>
 	@endif
+	@if( check_auth('admin/product/import_execl') )
+	<a href="javascript:;" class="btn btn-primary upload"> <span class="fa fa-upload"></span> 导入产品</a>
+	@endif
 </div>
 <form action="" method="get" class="form-horizontal form-line">
 	<div class="form-group col col-lg-2 col-md-2 col-sm-2 col-xs-12" style="margin-right: 2px;">
@@ -38,6 +41,7 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 	</div>
 	<input type="submit" class="btn btn-sm btn-primary" value="查询"/>
 	<a href="{{url('admin/product/index')}}" class="btn btn-sm btn-default" >重置</a>
+
 </form>
 <div class="row">
 	<div class="col-xs-12">
@@ -145,4 +149,55 @@ style="margin: 0 auto;width: 96%;padding: 30px 0px;"
 		})
 	})
 </script>
+<script src="/static/fileupload/jquery.ui.widget.js"></script>
+<script src="/static/fileupload/jquery.fileupload.js"></script>
+<script type="text/javascript">
+ $(function(){
+	$('.upload').on('click', function() {
+		$('#form-upload').remove();
+		$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input osctype="btn_upload_file" type="file" name="order_file" multiple="multiple" /></form>');
+		$('#form-upload input[name=\'order_file\']').trigger('click');
+		$('[osctype="btn_upload_file"]').fileupload({
+			dataType: 'json',
+			url: "{{url('admin/product/import_execl')}}",
+			singleFileUploads: false,
+			beforeSend: function() {
+				art.dialog({
+					id: 'loading',
+					lock: true,
+					title: '文件上传中'
+				});
+			},
+			done: function(e, data) {
+				art.dialog.list['loading'].close();
+				var result = data.result;
+				if (result.code == 'error') {
+					art.dialog({
+						content: result.msg,
+						lock: true,
+						ok: function() {}
+					});
+				}
+				if (result.code == 'success') {
+					art.dialog({
+						content: result.msg,
+						lock: true,
+						ok: function() {
+							location.reload();
+						}
+					});
+				}
+			},
+			fail: function(e,c) {
+				art.dialog.list['loading'].close();
+				art.dialog({
+					content: '<p>'+c.jqXHR.status+'=>'+c.jqXHR.statusText+'</p>',
+					lock: true,
+					ok: function() {}
+				});
+			}
+		});
+	});
+ })
+</script>
 @endsection

+ 2 - 0
routes/web.php

@@ -133,6 +133,8 @@ Route::middleware('admin')->prefix('admin')->group(function(){
     Route::any('product/get_sku_html',[App\Http\Controllers\Admin\Product::class,'get_sku_html']);
     // 状态
     Route::any('product/set_sort',[App\Http\Controllers\Admin\Product::class,'set_sort']);
+    // 订单导入
+    Route::any('product/import_execl',[App\Http\Controllers\Admin\Product::class,'import_execl']);
 
 
     /* 客户管理 */