130 lines
5.2 KiB
PHP
130 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace crmeb\jobs;
|
|
|
|
|
|
use think\facade\Db;
|
|
use crmeb\services\UploadService;
|
|
use Exception;
|
|
use crmeb\interfaces\JobInterface;
|
|
use think\facade\Log;
|
|
|
|
class ImportPicJob implements JobInterface
|
|
{
|
|
public function fire($job, $data)
|
|
{
|
|
Log::error('开始导入商品图片:'.$job->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();
|
|
|
|
/**循环目录 */
|
|
foreach ($files as $file) {
|
|
if ($file === '.' || $file === '..' ||$file ==='__MACOSX') {
|
|
continue;
|
|
}
|
|
$files_two = scandir($directory . '/' . $file);
|
|
$image_extensions = array('jpg', 'jpeg', 'png');
|
|
$image = '';
|
|
$slider_image = [];
|
|
$details = [];
|
|
$sku_arr = [];
|
|
|
|
/**清洗图片
|
|
* file_two 商品名对应目录
|
|
*/
|
|
foreach ($files_two as $file_two) {
|
|
$arr = $file_two;
|
|
|
|
if (in_array($arr, $image_extensions)) {
|
|
/**首图 */
|
|
$images[] = $file_two;
|
|
if ($image == '' && is_int((int)$arr[0])) {
|
|
$image = $directory . '/' . $file . '/' . $file_two;
|
|
continue;
|
|
}
|
|
/**轮播图 */
|
|
if (is_int((int)$arr[0]) && count($slider_image) < 4) {
|
|
$slider_image[] = $directory . '/' . $file . '/' . $file_two;
|
|
continue;
|
|
}
|
|
/**详情图 */
|
|
if (is_int((int)$arr[0])) {
|
|
$details[] = $directory . '/' . $file_two;
|
|
continue;
|
|
}
|
|
/**sku图 */
|
|
$sku = explode('==', $arr[0]);
|
|
if ($sku) {
|
|
$sku = implode(',', $sku);
|
|
$sku_arr[$sku] = $directory . '/' . $file . '/' . $file_two;
|
|
}
|
|
}else{
|
|
continue;
|
|
}
|
|
}
|
|
$where = ['mer_id' => $mer_id, 'is_del' => 0];
|
|
$update = [];
|
|
$update_content['title'] = '';
|
|
$update_content['image'] = [];
|
|
$update_content['type'] = 1;
|
|
$find = Db::name('store_product')->where($where)->where('store_name', 'like', '%' . $file . '%')->find();
|
|
if ($find) {
|
|
try {
|
|
/**更新商品图片 */
|
|
$image = $upload->to($dir)->stream(file_get_contents($image));
|
|
$update['image'] = $image->filePath;
|
|
foreach ($slider_image as $k => $v) {
|
|
$oss = $upload->to($dir)->stream(file_get_contents($v));
|
|
$update['slider_image'][] = $oss->filePath;
|
|
}
|
|
$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) {
|
|
$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) {
|
|
Db::name('store_product_content')
|
|
->where(['product_id' => $find['product_id']])
|
|
->update(json_encode($update_content));
|
|
} else {
|
|
$update_content['product_id'] = $find['product_id'];
|
|
Db::name('store_product_content')
|
|
->insert(json_encode($update_content));
|
|
}
|
|
} catch (Exception $e) {
|
|
Log::error('导入商品图片失败'.$e->getMessage().'line:'.$e->getLine());
|
|
}
|
|
}
|
|
}
|
|
|
|
$job->delete();
|
|
|
|
}
|
|
|
|
public function failed($data)
|
|
{
|
|
Log::error('导入商品图片失败:'.json_encode($data));
|
|
// TODO: Implement failed() method.
|
|
}
|
|
}
|