SyncBrFix.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Console\Commands;
  3. use Carbon\Carbon;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\File;
  7. use Symfony\Component\DomCrawler\Crawler;
  8. /**
  9. * 导入相册内容到产品表
  10. * php artisan sync:brsiteFix
  11. */
  12. class SyncBrFix extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'sync:brsiteFix';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26. public function handle()
  27. {
  28. $sqlFile = fopen('update_products.sql', 'a') or die("无法创建SQL文件");
  29. $ossHost = 'https://mietubl-website.oss-accelerate.aliyuncs.com';
  30. //$ossHost = 'https://mietubl-dev.oss-accelerate.aliyuncs.com';
  31. $baseUrl = 'https://mietubloficial.com.br';
  32. $urlList = [
  33. ['slug'=>'tws', 'url'=>'/tws/'],
  34. ['slug'=>'protetor-de-tela-de-vidro-temperado', 'url'=>'/protetor-de-tela-de-vidro-temperado/'],
  35. ['slug'=>'protetor-da-tela-do-tablet', 'url'=>'/protetor-da-tela-do-tablet/'],
  36. ['slug'=>'maquina-de-corte-de-protetor-de-tela', 'url'=>'/maquina-de-corte-de-protetor-de-tela/'],
  37. ['slug'=>'folhas-de-protetor-de-tela-de-hidrogel', 'url'=>'/folhas-de-protetor-de-tela-de-hidrogel/'],
  38. ['slug'=>'lightning', 'url'=>'/lightning/'],
  39. ['slug'=>'type-c', 'url'=>'/type-c/'],
  40. ['slug'=>'micro-usb', 'url'=>'/micro-usb/'],
  41. ['slug'=>'fones-de-ouvido-auricular-com-fio', 'url'=>'/fones-de-ouvido-auricular-com-fio/'],
  42. ['slug'=>'fones-de-ouvido', 'url'=>'/fones-de-ouvido/'],
  43. ['slug'=>'alto-falantes-bluetooth', 'url'=>'/alto-falantes-bluetooth/'],
  44. ['slug'=>'carregador-de-parede', 'url'=>'/carregador-de-parede/'],
  45. ['slug'=>'produtos-perifericos', 'url'=>'/produtos-perifericos/'],
  46. ];
  47. foreach ($urlList as $entry) {
  48. try {
  49. $category = DB::table('dist_product_category')
  50. ->where('slug', $entry['slug'])
  51. ->where('dist_id', 3)
  52. ->first();
  53. if (!$category) {
  54. echo "分类未找到,slug: {$entry['slug']}\n";
  55. continue;
  56. }
  57. echo "分类 {$category->name} \n";
  58. if ($entry['slug'] == 'produtos-perifericos') {
  59. $detailUrls = ['https://mietubloficial.com.br/produto/mini-impressora-de-pele-para-celular-mtb-pp01/'];
  60. } else {
  61. $html = file_get_contents($baseUrl . $entry['url']);
  62. $listCrawler = new Crawler($html);
  63. $detailUrls = $listCrawler->filter('.elementor-shortcode a')->extract(['href']);
  64. }
  65. foreach ($detailUrls as $detailUrl) {
  66. $detailHtml = file_get_contents($detailUrl);
  67. $detailCrawler = new Crawler($detailHtml);
  68. // 解析基础数据
  69. $title = $detailCrawler->filter('.product_title')->text();
  70. // 插入产品获取ID
  71. $productId = DB::table('dist_product')->where('title', $title)->where('dist_id', 3)->first();
  72. if (!$productId) {
  73. echo "产品未找到,title: {$title}\n";
  74. continue;
  75. }
  76. // 1. 去除最后一个字符(仅当末尾是斜杠时)
  77. $trimmedUrl = substr($detailUrl, 0, -1);
  78. // 2. 分割路径并取末段
  79. $path = parse_url($trimmedUrl, PHP_URL_PATH);
  80. $segments = explode('/', $path);
  81. $slug = end($segments);
  82. // 生成更新SQL
  83. $escapedTitle = addslashes($title); // 转义特殊字符
  84. $escapedSlug = addslashes($slug);
  85. $sql = "UPDATE dist_product SET seo_title = title,slug = '{$escapedSlug}' WHERE title = '{$title}' AND dist_id = 3;\n";
  86. // 写入SQL文件
  87. fwrite($sqlFile, $sql);
  88. echo "生成SQL:{$sql}\n";
  89. echo "处理主图完成,下一个产品 \n";
  90. echo "------------------------------------------\n";
  91. }
  92. } catch (\Exception $e) {
  93. echo "数据采集失败: " . $e->getMessage() . "\n";
  94. continue;
  95. }
  96. }
  97. dd('所有处理完成');
  98. }
  99. }