113 lines
2.7 KiB
PHP
113 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\ProductSku;
|
|
use App\Models\ProductSpu;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Str;
|
|
|
|
class ProductSkuUpdate extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'produck-sku:update';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '从主商品获取信息更新子商品';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$list = ProductSku::get();
|
|
$attributes = ['cover', 'images', 'media', 'description'];
|
|
|
|
$bar = $this->output->createProgressBar($list->count());
|
|
$bar->start();
|
|
|
|
$newUrl = 'https://jiqu-libs.oss-cn-chengdu.aliyuncs.com/';
|
|
foreach($list as $item) {
|
|
$data = [];
|
|
foreach($attributes as $key) {
|
|
$value = $item->$key;
|
|
$replace = false;
|
|
if (!$value) {
|
|
continue;
|
|
}
|
|
// 数组
|
|
if (is_array($value)) {
|
|
foreach($value as $subItem) {
|
|
if (!Str::startsWith($subItem, $newUrl)) {
|
|
$replace = true;
|
|
}
|
|
}
|
|
}
|
|
// 富文本
|
|
else if (preg_match('/^\<.+\>$/i', $value)) {
|
|
$replace = $this->parseEditorImage($value, $newUrl);
|
|
} else {
|
|
$replace = !Str::startsWith($value, $newUrl);
|
|
}
|
|
|
|
if ($replace) {
|
|
$data[$key] = $this->update($item, $key);
|
|
}
|
|
}
|
|
|
|
if (count($data) > 0) {
|
|
$item->forceFill($data)->save();
|
|
}
|
|
|
|
$bar->advance();
|
|
}
|
|
$bar->finish();
|
|
$this->line('');
|
|
return 0;
|
|
}
|
|
|
|
protected function update($item, $key)
|
|
{
|
|
$value = $item->$key;
|
|
$spu = ProductSpu::find($item->spu_id);
|
|
if ($spu) {
|
|
$value = $spu->$key;
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
protected function parseEditorImage($content, $newUrl)
|
|
{
|
|
$list = [];
|
|
preg_match_all("/src=\"(.+)\"/iU", $content, $list);
|
|
$replace = false;
|
|
foreach($list[1] as $item) {
|
|
if (!Str::startsWith($item, $newUrl)) {
|
|
$replace = true;
|
|
break;
|
|
}
|
|
}
|
|
return $replace;
|
|
}
|
|
}
|