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; } }