//轉化EXCEL表格行
public function getalphnum($char){
$array=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$len=strlen($char);
$sum=0;
for($i=0;$i<$len;$i++){
$index=array_search($char[$i],$array);
$sum+=($index+1)*pow(26,$len-$i-1);
}
return $sum;
}
/**
* 商品導入
* @author : liumingxing
*/
public function goodsImport(Request $request){
$file = $request->file('file');
$originalName = $file->getClientOriginalName(); // 文件原名
$ext = $file->getClientOriginalExtension(); // 擴展名
$realPath = $file->getRealPath(); //臨時文件的絕對路徑
$type = $file->getClientMimeType();
$filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
$bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
$filePath = 'public/uploads/'.iconv('UTF-8', 'GBK', $filename);
$data = [];
Excel::load($filePath, function ($reader) use(&$data){
$reader = $reader->getSheet(0);
$data = $reader->toArray();
});
//圖片上傳
$imageFile = $request->file('file');
$excel = \PHPExcel_IOFactory::load($imageFile);
$path = base_path().'/public/uploads/';
$drawing = new \PHPExcel_Writer_Excel2007_Drawing();
$drawingHashTable = new \PHPExcel_HashTable();
$drawingHashTable->addFromSource($drawing->allDrawings($excel));
for ($i = 0; $i < $drawingHashTable->count(); ++$i){
$memoryDrawing = $drawingHashTable->getByIndex($i);
if($memoryDrawing instanceof \PHPExcel_Worksheet_MemoryDrawing){
$filePath = 'images/'.date('Y').'/'.date('m').'/'.date('d').'/'.$memoryDrawing->getCoordinates().'-'.$memoryDrawing->getHashCode().'.jpg';
$filenameImg = $path .$filePath;
//將圖片存到指定的目錄
imagejpeg($memoryDrawing->getImageResource(),$filenameImg);
//獲取該圖片所在的單元格
$cell = $memoryDrawing->getWorksheet()->getCell($memoryDrawing->getCoordinates());
$string = $memoryDrawing->getCoordinates();
$coordinate = \PHPExcel_Cell::coordinateFromString($string);
}
$data[$coordinate[1] - 1][$this->getalphnum($coordinate[0]) - 1] = $filePath;
}
$admin_id = Admin::user()->{'id'}; //創建id
$category_id = 8; //分類id
$now_time = date('Y-m-d H:i:s'); //時間
try{
//事務
DB::beginTransaction();
for ($i = 1;$i <= count($data);$i++){
if(empty($data[$i]) || empty($data[$i][0])){
continue;
}
//判斷用戶是否都填寫
if(!isset($data[$i][0]) || empty($data[$i][0])){
throw new \Exception('第'.($i+1).'行不能'.$data[0][0].'字段不能爲空,請重新上傳');
}
if(!isset($data[$i][1]) || empty($data[$i][1])){
throw new \Exception('第'.($i+1).'行不能'.$data[0][1].'字段不能爲空,請重新上傳');
}
if(!isset($data[$i][3]) || empty($data[$i][3])){
throw new \Exception('第'.($i+1).'行不能'.$data[0][3].'字段不能爲空,請重新上傳');
}
if(!isset($data[$i][4]) || empty($data[$i][4])){
throw new \Exception('第'.($i+1).'行不能'.$data[0][4].'字段不能爲空,請重新上傳');
}
//查詢是否存在
$goods = Goods::where([
'title' => $data[$i][0]
])->first();
if(empty($goods)){
//新增
$temp = [
'title' => $data[$i][0],
'price' => $data[$i][1],
'img' => $data[$i][2],
'describe' => $data[$i][3],
'sort' => $data[$i][4],
'created_id'=> $admin_id,
'updated_id'=> $admin_id,
'created_at'=> $now_time,
'updated_at'=> $now_time,
'is_putaway'=> 1,
'category_id'=> $category_id
];
$res = DB::table('i_goods')->insertGetId($temp);
if(!$res){
throw new \Exception($data[$i][0].'新增失敗,請重新上傳新增');
}
}else{
//更新
$temp = [
'price' => $data[$i][1],
'img' => $data[$i][2],
'describe' => $data[$i][3],
'sort' => $data[$i][4],
'updated_id'=> $admin_id,
'updated_at'=> $now_time,
'is_putaway'=> 1,
];
$res = DB::table('i_goods')
->where([
'title' => $data[$i][0]
])
->update($temp);
if(!$res){
throw new \Exception($data[$i][0].'更新失敗,請重新上傳更新');
}
}
}
DB::commit();
Storage::disk('uploads')->delete($filename); //刪除上傳的暫存文件
return response()->json(['code' => 200, 'message' => '文件上傳成功', 'data' => []]);
}catch (\Exception $e){
$error = $e->getMessage();
DB::rollBack();
return response()->json(['code' => 300, 'message' => '文件上傳失敗,請重新上傳', 'data' => []]);
}
}