'用户', 'list' => UserInfo::get(), 'attributes' => ['avatar']], ['name' => '文章', 'list' => Article::get(), 'attributes' => ['cover', 'content']], ['name' => '广告', 'list' => Ad::get(), 'attributes' => ['image']], ['name' => '门店', 'list' => Store::get(), 'attributes' => ['image']], ['name' => '商品SPU', 'list' => ProductSpu::get(), 'attributes' => ['cover', 'images', 'media', 'description']], ['name' => '商品SKU', 'list' => ProductSku::get(), 'attributes' => ['cover', 'images', 'media', 'description']], ['name' => '商品分类', 'list' => ProductCategory::get(), 'attributes' => ['icon']], ['name' => '商品特点', 'list' => ProductFeature::get(), 'attributes' => ['icon']], ['name' => '购买须知', 'list' => ProductBuynote::get(), 'attributes' => ['content']], ['name' => '分享背景', 'list' => ShareBg::get(), 'attributes' => ['image']], ['name' => '抽奖活动', 'list' => DrawActivity::get(), 'attributes' => ['desc', 'bg_image']], ['name' => '抽奖奖品', 'list' => DrawPrize::get(), 'attributes' => ['icon']], ]; foreach($list as $item) { $collection = $item['list']; $this->info($item['name'] . ': ' . $collection->count()); $bar = $this->output->createProgressBar($collection->count()); $bar->start(); foreach($collection as $model) { $this->updateModel($model, $item['attributes']); $bar->advance(); } $bar->finish(); } 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 = 'zcs-test'; 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; } }