6
0
Fork 0

add command

base
panliang 2022-08-17 13:29:15 +08:00
parent 7e5fc84f85
commit 3052b115c0
1 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,105 @@
<?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'];
$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();
}
}
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;
}
}