From a964513f3a1b8126499733776c8b64a96e6be375 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Fri, 29 Jul 2022 14:47:20 +0800 Subject: [PATCH] OssCopyCommand --- app/Console/Commands/OssCopy.php | 164 +++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 app/Console/Commands/OssCopy.php diff --git a/app/Console/Commands/OssCopy.php b/app/Console/Commands/OssCopy.php new file mode 100644 index 00000000..8f6716ab --- /dev/null +++ b/app/Console/Commands/OssCopy.php @@ -0,0 +1,164 @@ +updateModel($item, ['avatar']); + } + // 文章 + foreach(Article::get() as $item) { + $this->updateModel($item, ['cover', 'content']); + } + // 广告 + foreach(Ad::get() as $item) { + $this->updateModel($item, ['image']); + } + // 门店 + foreach(Store::get() as $item) { + $this->updateModel($item, ['image']); + } + // 商品 + foreach(ProductSpu::get() as $item) { + $this->updateModel($item, ['cover', 'images', 'media', 'description']); + } + foreach(ProductSku::get() as $item) { + $this->updateModel($item, ['cover', 'images', 'media', 'description']); + } + // 商品分类 + foreach(ProductCategory::get() as $item) { + $this->updateModel($item, ['icon']); + } + // 商品特点 + foreach(ProductFeature::get() as $item) { + $this->updateModel($item, ['icon']); + } + // 购买须知 + foreach(ProductBuynote::get() as $item) { + $this->updateModel($item, ['content']); + } + // 分享背景 + foreach(ShareBg::get() as $item) { + $this->updateModel($item, ['image']); + } + // 抽奖活动 + foreach(DrawActivity::get() as $item) { + $this->updateModel($item, ['desc', 'bg_image']); + } + // 抽奖奖品 + foreach(DrawPrize::get() as $item) { + $this->updateModel($item, ['icon']); + } + + return 0; + } + + protected function updateModel(Model $model, array $attributes = []) + { + $data = []; + foreach($attributes as $key) { + $item = $model->$key; + if (!$item) { + continue; + } + // 数组 + if (is_array($item)) { + $values = []; + foreach($item as $value) { + array_push($values, $this->copy($value)); + } + $data[$key] = $values; + } + // 富文本 + else if (preg_match('/^\<.+\>$/i', $item)) { + $data[$key] = $this->parseEditorImage($item); + } else { + $data[$key] = $this->copy($item); + } + } + if (count($data) > 0) { + $model->forceFill($data)->save(); + } + } + + protected function copy($url) + { + if ($url) { + $config = config('filesystems.disks.aliyun'); + $client = new OssClient(data_get($config, 'access_id'), data_get($config, 'access_key'), data_get($config, 'endpoint')); + $baseUrl = 'https://zcs-test.oss-cn-chengdu.aliyuncs.com/'; + $newUrl = 'https://jiqu-library.oss-cn-chengdu.aliyuncs.com/'; + $oldBucket = data_get($config, 'bucket'); + if (Str::startsWith($url, $baseUrl)) { + $path = substr($url, strlen($baseUrl)); + $bucket = 'jiqu-library'; + $client->copyObject($oldBucket, $path, $bucket, $path); + + return $newUrl . $path; + } + } + return $url; + } + + protected function parseEditorImage($content) + { + $list = []; + preg_match_all("/src=\"(.+)\"/iU", $content, $list); + $replaces = []; + foreach($list[1] as $item) { + array_push($replaces, ['search' => $item, 'value' => $this->copy($item)]); + } + $str = $content; + foreach($replaces as $item) { + $str = str_replace($item['search'], $item['value'], $str); + } + return $str; + } +}