diff --git a/app/Console/Commands/ProductSkuUpdate.php b/app/Console/Commands/ProductSkuUpdate.php new file mode 100644 index 00000000..79b6a8d4 --- /dev/null +++ b/app/Console/Commands/ProductSkuUpdate.php @@ -0,0 +1,105 @@ +$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; + } +}