Browse Source

图片下载完成

master
李可松 4 years ago
parent
commit
3d424e87c0
  1. 103
      app/Console/Commands/Collector.php

103
app/Console/Commands/Collector.php

@ -7,6 +7,7 @@ use App\Models\Product;
use App\Models\Supplier;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
class Collector extends Command
{
@ -53,9 +54,10 @@ class Collector extends Command
$this->line("供应商 $supplier_id 导入完毕");
} else {
// $this->exists_update = false;
// $this->tourist_line();
// $this->hotel();
$this->tourist_line();
$this->hotel();
$this->scenic();
// $this->db_img_replace();
$this->line('全部采集完毕');
}
return Command::SUCCESS;
@ -95,7 +97,7 @@ class Collector extends Command
$v['supplier_id'] = $supplier_id;
unset($v['unique_id'], $v['site']);
Product::query()->updateOrCreate(['supplier_id' => $supplier_id, 'title' => $v['title']], $v);
Product::updateOrCreate(['supplier_id' => $supplier_id, 'title' => $v['title']], $v);
});
$this->line("导入 $supplier_id 结束");
}
@ -158,7 +160,7 @@ class Collector extends Command
$extends['field_2_open_time'][1]['winter'] = $match_open_time[1][2] ?? '';
}
CollectProduct::updateOrCreate(['unique_id' => $id, 'site' => 1], [
$this->save_to_db(['unique_id' => $id, 'site' => 1], [
'unique_id' => $id,
'site' => 1,
'type' => 2, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
@ -247,7 +249,7 @@ class Collector extends Command
$extends['field_1_latitude'] = $base_info['lat'];
$extends['field_1_longitude'] = $base_info['lng'];
CollectProduct::updateOrCreate(['unique_id' => $v['id'], 'site' => 1], [
$this->save_to_db(['unique_id' => $v['id'], 'site' => 1], [
'unique_id' => $v['id'],
'site' => 1,
'type' => 1, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
@ -339,7 +341,7 @@ class Collector extends Command
}
}
CollectProduct::updateOrCreate(['unique_id' => $id, 'site' => 1], [
$this->save_to_db(['unique_id' => $id, 'site' => 1], [
'unique_id' => $id,
'site' => 1,
'type' => 0, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
@ -362,4 +364,93 @@ class Collector extends Command
}
}
}
//保存到数据库
private function save_to_db($unique_flag, $data)
{
$this->search_img($data);
CollectProduct::updateOrCreate($unique_flag, $data);
}
//搜索数据中的数据,有图片则下载并规格数据
private function db_img_replace()
{
$all = CollectProduct::all(['id', 'pictures', 'know', 'content']);
foreach ($all as $model) {
$this->line('当前处理数据:' . $model->id);
$data = $model->toArray();
$this->search_img($data);
$model->pictures = $data['pictures'];
$model->know = $data['know'];
$model->content = $data['content'];
$model->save();
}
}
//从数据中搜索图片并下载
private function search_img(&$data)
{
$img_host = 'https://yytx.eugyl.com/storage/';
//保存产品图片集
if (!empty($data['pictures']) && is_array($data['pictures'])) {
foreach ($data['pictures'] as &$url) {
if (substr($url, 0, 4) != 'http' || str_contains($url, $img_host)) continue;
$this->line('正在下载图片:' . $url);
$src = $this->download_img($url);
if (!$src) continue;
$url = $src;
}
}
//保存富文本内的图片
foreach ($data as $key => &$rich_text) {
//只替换know和content字段
if ($key == 'know' || $key == 'content') {
//删除href链接
$rich_text = preg_replace('#<a.*?>|</a>#', '', $rich_text);
if (!empty($rich_text) && preg_match_all('/<img\s+.*?src=([\'"])(.*?)\1.*?>/i', $rich_text, $image_url) && !empty($image_url[2]) && is_array($image_url[2])) {
foreach (array_unique($image_url[2]) as $url) {
if (substr($url, 0, 4) != 'http' || str_contains($url, $img_host)) continue;
$this->line('正在下载图片:' . $url);
$src = $this->download_img($url);
if (!$src) continue;
$rich_text = str_replace([$url, ' data-src'], [$img_host . $src, ' src'], $rich_text);
}
}
}
}
}
//下载图片
private function download_img($url): string
{
$file_info = pathinfo($url);
$ext = 'jpg';
if (!empty($file_info['extension'])) {
$pos = strpos($file_info['extension'], '?');
$ext = $pos ? substr($file_info['extension'], 0, $pos) : $file_info['extension'];
}
try {
$raw = file_get_contents($url);
} catch (\Exception $e) {
return '';
}
if (empty($raw)) {
return '';
}
$md5 = md5($raw);
$filename = 'collect/images/' . date('Y-m-d') . '/' . $md5 . '.' . $ext;
if (file_exists(storage_path($filename)) || Storage::put($filename, $raw)) {
return $filename;
} else {
return '';
}
}
}
Loading…
Cancel
Save