SyncBrFix.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. exit;
  29. $sqlFile = fopen('update_products.sql', 'a') or die("无法创建SQL文件");
  30. $ossHost = 'https://mietubl-website.oss-accelerate.aliyuncs.com';
  31. //$ossHost = 'https://mietubl-dev.oss-accelerate.aliyuncs.com';
  32. $baseUrl = 'https://mietubloficial.com.br';
  33. $urlList = [
  34. ['slug'=>'tws', 'url'=>'/tws/'],
  35. ['slug'=>'protetor-de-tela-de-vidro-temperado', 'url'=>'/protetor-de-tela-de-vidro-temperado/'],
  36. ['slug'=>'protetor-da-tela-do-tablet', 'url'=>'/protetor-da-tela-do-tablet/'],
  37. ['slug'=>'maquina-de-corte-de-protetor-de-tela', 'url'=>'/maquina-de-corte-de-protetor-de-tela/'],
  38. ['slug'=>'folhas-de-protetor-de-tela-de-hidrogel', 'url'=>'/folhas-de-protetor-de-tela-de-hidrogel/'],
  39. ['slug'=>'lightning', 'url'=>'/lightning/'],
  40. ['slug'=>'type-c', 'url'=>'/type-c/'],
  41. ['slug'=>'micro-usb', 'url'=>'/micro-usb/'],
  42. ['slug'=>'fones-de-ouvido-auricular-com-fio', 'url'=>'/fones-de-ouvido-auricular-com-fio/'],
  43. ['slug'=>'fones-de-ouvido', 'url'=>'/fones-de-ouvido/'],
  44. ['slug'=>'alto-falantes-bluetooth', 'url'=>'/alto-falantes-bluetooth/'],
  45. ['slug'=>'carregador-de-parede', 'url'=>'/carregador-de-parede/'],
  46. ['slug'=>'produtos-perifericos', 'url'=>'/produtos-perifericos/'],
  47. ];
  48. foreach ($urlList as $entry) {
  49. try {
  50. $category = DB::table('dist_product_category')
  51. ->where('slug', $entry['slug'])
  52. ->where('dist_id', 3)
  53. ->first();
  54. if (!$category) {
  55. echo "分类未找到,slug: {$entry['slug']}\n";
  56. continue;
  57. }
  58. echo "分类 {$category->name} \n";
  59. if ($entry['slug'] == 'produtos-perifericos') {
  60. $detailUrls = ['https://mietubloficial.com.br/produto/mini-impressora-de-pele-para-celular-mtb-pp01/'];
  61. } else {
  62. $html = file_get_contents($baseUrl . $entry['url']);
  63. $listCrawler = new Crawler($html);
  64. $detailUrls = $listCrawler->filter('.elementor-shortcode a')->extract(['href']);
  65. }
  66. foreach ($detailUrls as $detailUrl) {
  67. $detailHtml = file_get_contents($detailUrl);
  68. $detailCrawler = new Crawler($detailHtml);
  69. // 解析基础数据
  70. $title = $detailCrawler->filter('.product_title')->text();
  71. // 插入产品获取ID
  72. $productId = DB::table('dist_product')->where('title', $title)->where('dist_id', 3)->first();
  73. if (!$productId) {
  74. echo "产品未找到,title: {$title}\n";
  75. continue;
  76. }
  77. // 1. 去除最后一个字符(仅当末尾是斜杠时)
  78. $trimmedUrl = substr($detailUrl, 0, -1);
  79. // 2. 分割路径并取末段
  80. $path = parse_url($trimmedUrl, PHP_URL_PATH);
  81. $segments = explode('/', $path);
  82. $slug = end($segments);
  83. // 生成更新SQL
  84. $escapedTitle = addslashes($title); // 转义特殊字符
  85. $escapedSlug = addslashes($slug);
  86. $sql = "UPDATE dist_product SET seo_title = title,slug = '{$escapedSlug}' WHERE title = '{$title}' AND dist_id = 3;\n";
  87. // 写入SQL文件
  88. fwrite($sqlFile, $sql);
  89. echo "生成SQL:{$sql}\n";
  90. echo "处理主图完成,下一个产品 \n";
  91. echo "------------------------------------------\n";
  92. }
  93. } catch (\Exception $e) {
  94. echo "数据采集失败: " . $e->getMessage() . "\n";
  95. continue;
  96. }
  97. }
  98. dd('所有处理完成');
  99. }
  100. }