attempts()); if ($job->attempts() > 3) { $job->delete(); $this->failed($data); } $directory = $data['path']; $mer_id = $data['mer_id']; $files = scandir($directory); $dir = 'def/' . date('Y-m-d'); $upload = UploadService::create(); try { /**循环目录 */ foreach ($files as $file) { if ($file === '.' || $file === '..' || $file === '__MACOSX') { continue; } if (!is_dir($directory . '/' . $file)) { continue; } $files_two = scandir($directory . '/' . $file); $image_extensions = array('jpg', 'jpeg', 'png'); $image = ''; $slider_image = []; $details = []; $sku_arr = []; /**清洗图片 */ foreach ($files_two as $file_two) { if ($file_two === '.' || $file_two === '..' || $file_two === '__MACOSX') { continue; } $arr = explode('.', $file_two); if (in_array($arr[1], $image_extensions)) { /**首图 */ $images[] = $file_two; if ($image == '' && is_numeric($arr[0])) { $image = $directory . '/' . $file . '/' . $file_two; continue; } /**轮播图 */ if (is_numeric($arr[0]) && count($slider_image) < 4) { $slider_image[] = $directory . '/' . $file . '/' . $file_two; continue; } /**详情图 */ if (is_numeric($arr[0])) { $details[] = $directory . '/' .$file.'/'. $file_two; continue; } /**sku图 */ $sku = explode('==', $arr[0]); if ($sku) { $sku = implode(',', $sku); $sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two; } } } $where = ['mer_id' => $mer_id, 'is_del' => 0]; $update = []; $update_content['title'] = ''; $update_content['image'] = []; $find = Db::name('store_product')->where($where)->where('store_name', $file)->find(); if ($find) { /**更新商品图片 */ $image = $upload->to($dir)->stream(file_get_contents($image)); $update['image'] = $image->filePath; Db::name('store_spu')->where('product_id', $find['product_id'])->update($update); foreach ($slider_image as $k => $v) { $oss = $upload->to($dir)->stream(file_get_contents($v)); $update['slider_image'][] = $oss->filePath; } if (isset($update['slider_image'])) { $update['slider_image'] = implode(',', $update['slider_image']); } Db::name('store_product')->where('product_id', $find['product_id'])->update($update); /**更新规格图片 */ foreach ($sku_arr as $k => $v) { // $sku = explode(',', $k); // if(count($sku)==2){ // $sku_name=$sku[0]; // }else{ // continue; // } $store_product_attr_value = Db::name('store_product_attr_value')->where(['mer_id' => $mer_id, 'product_id' => $find['product_id'], 'sku' => $k])->find(); if ($store_product_attr_value) { $oss = $upload->to($dir)->stream(file_get_contents($v)); Db::name('store_product_attr_value') ->where(['mer_id' => $mer_id, 'product_id' => $find['product_id'], 'sku' => $k]) ->update(['image' => $oss->filePath]); } } /**更新详情图片 */ $store_product_content = Db::name('store_product_content')->where(['product_id' => $find['product_id']])->find(); foreach ($details as $k => $v) { $oss = $upload->to($dir)->stream(file_get_contents($v)); $update_content['image'][] = $oss->filePath; } if ($store_product_content) { if (isset($update_content['image']) && !empty($update_content['image'])) { Db::name('store_product_content') ->where(['product_id' => $find['product_id']]) ->update(['content' => json_encode($update_content),'type'=>1]); } } else { $update_content['product_id'] = $find['product_id']; Db::name('store_product_content') ->insert(['product_id' => $find['product_id'], 'type' => 1, 'content' => json_encode($update_content)]); } } } } catch (Exception $e) { $data['product_id'] = $find['product_id']; $data['mer_id'] = $data['mer_id']; $data['store_name'] = $find['store_name']; $data['content'] = $e->getMessage(); $this->create_product_import_log($data, 0); } $job->delete(); } public function failed($data) { Log::error('导入商品图片失败:' . json_encode($data)); // TODO: Implement failed() method. } public function create_product_import_log($data, $status = 1) { $data = [ 'product_id' => $data['product_id']??0, 'mer_id' => $data['mer_id'], 'name' => $data['store_name'], 'content' => $data['content'] ?? '', 'status' => $status, 'create_time' => date('Y-m-d H:i:s'), ]; Db::name('store_product_import')->insert($data); } }