resetImagePath.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\command;
  4. use app\common\model\article\Article;
  5. use app\common\model\article\ArticleCategory;
  6. use app\common\model\community\Community;
  7. use app\common\model\community\CommunityTopic;
  8. use app\common\model\store\broadcast\BroadcastGoods;
  9. use app\common\model\store\broadcast\BroadcastRoom;
  10. use app\common\model\store\Guarantee;
  11. use app\common\model\store\product\Product;
  12. use app\common\model\store\product\ProductAssistUser;
  13. use app\common\model\store\product\ProductAttrValue;
  14. use app\common\model\store\product\ProductGroupUser;
  15. use app\common\model\store\product\ProductReply;
  16. use app\common\model\store\product\Spu;
  17. use app\common\model\store\service\StoreService;
  18. use app\common\model\store\StoreCategory;
  19. use app\common\model\store\StoreSeckillTime;
  20. use app\common\model\system\attachment\Attachment;
  21. use app\common\model\system\financial\Financial;
  22. use app\common\model\system\merchant\Merchant;
  23. use app\common\model\system\merchant\MerchantIntention;
  24. use app\common\model\user\MemberInterests;
  25. use app\common\model\user\User;
  26. use app\common\model\user\UserBrokerage;
  27. use think\console\Command;
  28. use think\console\Input;
  29. use think\console\input\Argument;
  30. use think\console\input\Option;
  31. use think\console\Output;
  32. use think\facade\Log;
  33. class resetImagePath extends Command
  34. {
  35. protected $path = '';
  36. protected $change = '/';
  37. protected $type = [3];
  38. protected $field = [
  39. 'image',
  40. 'image_input',
  41. 'cover_img',
  42. 'share_img',
  43. 'feeds_img',
  44. 'pic',
  45. 'mer_avatar',
  46. 'mer_banner',
  47. 'images',
  48. 'qrcode_url',
  49. 'avatar_img',
  50. 'avatar',
  51. 'attachment_src',
  52. 'brokerage_icon',
  53. 'pics',
  54. 'slider_image',
  55. ];
  56. protected function configure()
  57. {
  58. // 指令配置
  59. $this->setName('reset:imagePath')
  60. ->addArgument('path', Argument::OPTIONAL, 'path:http:/crmeb.com')
  61. ->addOption('url', null, Option::VALUE_REQUIRED, 'change:http:/crmeb1.com', '/')
  62. ->setDescription('php think reset:imagePath http://old.com --url http://new.com');
  63. }
  64. protected function execute(Input $input, Output $output)
  65. {
  66. $this->path = rtrim($input->getArgument('path'), '/');
  67. if ($input->hasOption('url')) {
  68. $this->change = rtrim($input->getOption('url'), '/');
  69. if (!$this->change) $this->change = '/';
  70. }
  71. $output->writeln('开始执行');
  72. foreach ($this->type as $type) {
  73. $models = $this->switchModel($type);
  74. foreach ($models as $model) {
  75. $this->getResult($model, $type);
  76. }
  77. }
  78. $output->info('执行完成');
  79. }
  80. protected function getResult($model, $type)
  81. {
  82. if (is_null($model)) return;
  83. try {
  84. $key = $model->getPk();
  85. if ($key){
  86. $model->chunk(100, function ($data) {
  87. foreach ($data as $item) {
  88. $save = 0;
  89. foreach ($this->field as $f) {
  90. if (isset($item->$f) && !empty($item->$f)) {
  91. $sr = $this->changeImage($item->$f);
  92. $item->$f = $sr;
  93. $save = 1;
  94. }
  95. }
  96. if ($save) $item->save();
  97. }
  98. });
  99. }
  100. return;
  101. } catch (\Exception $exception) {
  102. Log::info('图片处理异常:' . $exception->getMessage());
  103. }
  104. }
  105. protected function changeImage($data)
  106. {
  107. if (!$data) return $data;
  108. echo PHP_EOL;
  109. echo '替换前:';
  110. print_r($data);
  111. if (is_array($data)) {
  112. $load = implode(',', $data);
  113. $load1 = str_replace($this->path, $this->change, $load);
  114. $string = explode(',', $load1);
  115. } else {
  116. $string = str_replace($this->path, $this->change, $data);
  117. }
  118. echo PHP_EOL;
  119. echo '替换后:';
  120. print_r($string);
  121. echo PHP_EOL;
  122. return $string;
  123. }
  124. protected function switchModel($type)
  125. {
  126. $model = [];
  127. // 商品规格
  128. $model[] = (new ProductAttrValue());
  129. // 商品规格
  130. $model[] = (new ProductAssistUser());
  131. // 商品规格
  132. $model[] = (new ProductGroupUser());
  133. // 商品
  134. $model[] = (new Product());
  135. // 直播间
  136. $model[] = (new BroadcastRoom());
  137. // 直播间商品
  138. $model[] = (new BroadcastGoods());
  139. // 服务保障
  140. $model[] = (new Guarantee());
  141. // 分类
  142. $model[] = (new StoreCategory());
  143. // 商品评价
  144. $model[] = (new ProductReply());
  145. // spu
  146. $model[] = (new Spu());
  147. // 文章
  148. $model[] = (new Article());
  149. // 文章分类
  150. $model[] = (new ArticleCategory());
  151. // 社区
  152. $model[] = (new Community());
  153. // 社区话题
  154. $model[] = (new CommunityTopic());
  155. // 流水
  156. $model[] = (new Financial());
  157. // 会员
  158. $model[] = (new UserBrokerage());
  159. // 会员权益
  160. $model[] = (new MemberInterests());
  161. // 商户
  162. $model[] = (new Merchant());
  163. // 商户权益
  164. $model[] = (new MerchantIntention());
  165. // 客服
  166. $model[] = (new StoreService());
  167. // 秒杀配置
  168. $model[] = (new StoreSeckillTime());
  169. // 素材
  170. $model[] = (new Attachment());
  171. // 用户
  172. $model[] = (new User());
  173. return $model;
  174. }
  175. }