setName('reset:imagePath') ->addArgument('path', Argument::OPTIONAL, 'path:http:/crmeb.com') ->addOption('url', null, Option::VALUE_REQUIRED, 'change:http:/crmeb1.com', '/') ->setDescription('php think reset:imagePath http://old.com --url http://new.com'); } protected function execute(Input $input, Output $output) { $this->path = rtrim($input->getArgument('path'), '/'); if ($input->hasOption('url')) { $this->change = rtrim($input->getOption('url'), '/'); if (!$this->change) $this->change = '/'; } $output->writeln('开始执行'); foreach ($this->type as $type) { $models = $this->switchModel($type); foreach ($models as $model) { $this->getResult($model, $type); } } $output->info('执行完成'); } protected function getResult($model, $type) { if (is_null($model)) return; try { $key = $model->getPk(); if ($key){ $model->chunk(100, function ($data) { foreach ($data as $item) { $save = 0; foreach ($this->field as $f) { if (isset($item->$f) && !empty($item->$f)) { $sr = $this->changeImage($item->$f); $item->$f = $sr; $save = 1; } } if ($save) $item->save(); } }); } return; } catch (\Exception $exception) { Log::info('图片处理异常:' . $exception->getMessage()); } } protected function changeImage($data) { if (!$data) return $data; echo PHP_EOL; echo '替换前:'; print_r($data); if (is_array($data)) { $load = implode(',', $data); $load1 = str_replace($this->path, $this->change, $load); $string = explode(',', $load1); } else { $string = str_replace($this->path, $this->change, $data); } echo PHP_EOL; echo '替换后:'; print_r($string); echo PHP_EOL; return $string; } protected function switchModel($type) { $model = []; // 商品规格 $model[] = (new ProductAttrValue()); // 商品规格 $model[] = (new ProductAssistUser()); // 商品规格 $model[] = (new ProductGroupUser()); // 商品 $model[] = (new Product()); // 直播间 $model[] = (new BroadcastRoom()); // 直播间商品 $model[] = (new BroadcastGoods()); // 服务保障 $model[] = (new Guarantee()); // 分类 $model[] = (new StoreCategory()); // 商品评价 $model[] = (new ProductReply()); // spu $model[] = (new Spu()); // 文章 $model[] = (new Article()); // 文章分类 $model[] = (new ArticleCategory()); // 社区 $model[] = (new Community()); // 社区话题 $model[] = (new CommunityTopic()); // 流水 $model[] = (new Financial()); // 会员 $model[] = (new UserBrokerage()); // 会员权益 $model[] = (new MemberInterests()); // 商户 $model[] = (new Merchant()); // 商户权益 $model[] = (new MerchantIntention()); // 客服 $model[] = (new StoreService()); // 秒杀配置 $model[] = (new StoreSeckillTime()); // 素材 $model[] = (new Attachment()); // 用户 $model[] = (new User()); return $model; } }