海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
8.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\CollectProduct;
  4. use App\Models\Product;
  5. use App\Models\Supplier;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Http;
  8. class Collector extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'collector {--import=} {--limit=}'; //php artisan collector --import=$supplier_id
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '马蜂窝产品采集';
  22. /**
  23. * 如果已经存在是否要更新,true更新,false不更新
  24. * @var bool
  25. */
  26. private bool $exists_update = true;
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. $supplier_id = $this->option('import');
  44. if (!empty($supplier_id)) {
  45. $this->import($supplier_id);
  46. $this->line("供应商 $supplier_id 导入完毕");
  47. } else {
  48. // $this->exists_update = false;
  49. $this->scenic();
  50. $this->hotel();
  51. $this->line('全部采集完毕');
  52. }
  53. return Command::SUCCESS;
  54. }
  55. //导入
  56. private function import($supplier_id)
  57. {
  58. $limit = $this->option('limit');
  59. $cpModel = CollectProduct::query()->orderBy('id', 'desc');
  60. if (!empty($limit)) {
  61. $arr = explode(',', $limit);
  62. $count = count($arr);
  63. if ($count == 1) {
  64. $import_data = $cpModel->limit($arr[0])->get()->toArray();
  65. } else if ($count == 2) {
  66. $import_data = $cpModel->offset($arr[0])->limit($arr[1])->get()->toArray();
  67. } else {
  68. $import_data = $cpModel->get()->toArray();
  69. }
  70. } else {
  71. $import_data = $cpModel->get()->toArray();
  72. }
  73. if (ctype_digit($supplier_id)) {
  74. $ids = [$supplier_id];
  75. } else if ($supplier_id == 'all') {
  76. $ids = Supplier::query()->where('id', '>', 1)->pluck('id');
  77. } else {
  78. return;
  79. }
  80. foreach ($ids as $supplier_id) {
  81. $this->line('正在导入 ' . $supplier_id);
  82. array_walk($import_data, function ($v) use ($supplier_id) {
  83. $v['supplier_id'] = $supplier_id;
  84. unset($v['unique_id'], $v['site']);
  85. Product::query()->updateOrCreate(['supplier_id' => $supplier_id, 'title' => $v['title']], $v);
  86. });
  87. $this->line("导入 $supplier_id 结束");
  88. }
  89. }
  90. //酒店采集
  91. private function hotel()
  92. {
  93. $http = Http::withOptions(['verify' => false]);
  94. $mddid = 10030; //10030==三亚
  95. for ($i=0; $i<10; $i++) {
  96. $this->line('开始采集:第 ' . ($i + 1) . ' 页');
  97. $data = $http->get('https://m.mafengwo.cn/rest/hotel/hotels/', [
  98. 'filter' => [
  99. 'mddid' => $mddid
  100. ],
  101. 'page' => [
  102. 'mode' => 'sequential',
  103. 'boundary' => $i * 20, //分页参数
  104. 'num' => 20
  105. ],
  106. ]);
  107. if (empty($data['data']['list'])) {
  108. continue;
  109. }
  110. foreach ($data['data']['list'] as $v) {
  111. if (empty($v['id'])) continue;
  112. if ($this->exists_update == false && CollectProduct::where(['unique_id' => $v['id'], 'site' => 1])->exists()) {
  113. continue;
  114. }
  115. $this->line('采集详情:' . $v['id']);
  116. //基本信息
  117. $params = [
  118. '_ts' => time() . '123',
  119. 'hotel_id' => (string)$v['id'],
  120. 'lat' => '',
  121. 'lng' => '',
  122. 'rmdd_id' => (string)$mddid,
  123. ];
  124. $params['_sn'] = $this->_sn($params);
  125. $data = $http->get('https://m.mafengwo.cn/hservice/detail/info/base_info', $params);
  126. if (empty($data['data']['info'])) {
  127. continue;
  128. }
  129. $base_info = $data['data']['info'];
  130. //酒店详情
  131. $data = $http->get('https://m.mafengwo.cn/hservice/detail/info/guide_info', ['hotel_id' => $v['id']]);
  132. $guide_info = $data['data']['info'] ?? [];
  133. //旅游须知
  134. $know = "<p>入住时间:" . (!empty($guide_info['check_in']['title']) ? $guide_info['check_in']['title'] : '') . "</p>";
  135. $know .= "<p>离店时间:" . (!empty($guide_info['check_out']['title']) ? $guide_info['check_out']['title'] : '') . "</p>";
  136. $know .= '<p>' . array_reduce($base_info['facility_sort'] ?? [], fn($v1, $v2) => $v1 . $v2['title'] ?? '') . '</p>';
  137. //扩展字段
  138. $extends['field_1_tags'] = array_map(fn($v) => $v['title'] ?? '', $guide_info['facility'] ?? []);
  139. $extends['field_1_name'] = $base_info['name'];
  140. $extends['field_1_address'] = $base_info['address'];
  141. $extends['field_1_latitude'] = $base_info['lat'];
  142. $extends['field_1_longitude'] = $base_info['lng'];
  143. CollectProduct::updateOrCreate(['unique_id' => $v['id'], 'site' => 1], [
  144. 'unique_id' => $v['id'],
  145. 'site' => 1,
  146. 'type' => 1, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  147. 'title' => mb_substr($base_info['name'] ?? '' . $base_info['level'] ?? '', 0, 255),
  148. 'price' => mt_rand(150, 350),
  149. 'original_price' => mt_rand(350, 550),
  150. 'pictures' => array_map(fn($v) => $v['url'] ?? '', $base_info['album'] ?? []) ?? [],
  151. 'stock' => mt_rand(1000, 9999),
  152. 'sale' => $base_info['num_collect'] ?? 0,
  153. 'status' => -2, //-2下架
  154. 'know' => $know,
  155. 'content' => $guide_info['intro'] ?? '',
  156. 'extends' => $extends,
  157. 'logitude' => $base_info['lng'] ?? 0,
  158. 'latitude' => $base_info['lat'] ?? 0,
  159. 'address' => $base_info['address'] ?? '',
  160. ]);
  161. $this->line("{$v['id']} 采集结束" . PHP_EOL);
  162. }
  163. $this->line('第 ' . ($i + 1) . ' 页采集结束' . PHP_EOL);
  164. }
  165. }
  166. //酒店详情计算_sn
  167. private function _sn($params): string
  168. {
  169. ksort($params);
  170. return substr(md5(json_encode($params) . 'c9d6618dbc657b41a66eb0af952906f1'), 2, 10);
  171. }
  172. //景区采集
  173. private function scenic()
  174. {
  175. $http = Http::withOptions(['verify' => false]);
  176. for($i=0; $i<10; $i++) {
  177. $data = $http->get('https://m.mafengwo.cn/sales/ajax.php', [
  178. 'sF' => 'search_new_list',
  179. 'offset' => $i * 10, //分页参数
  180. ]);
  181. if (empty($data['data'])) {
  182. continue;
  183. }
  184. $data = $data['data'];
  185. preg_match_all('/<a href="\/sales\/(\d+)\.html"/', $data, $matches);
  186. if (empty($matches[1])) continue;
  187. foreach ($matches[1] as $id) {
  188. if ($this->exists_update == false && CollectProduct::where(['unique_id' => $id, 'site' => 1])->exists()) {
  189. continue;
  190. }
  191. $this->line('开始采集:' . $id);
  192. $info = $http->get('https://m.mafengwo.cn/sales/detail/index/info?id=' . $id);
  193. //旅游须知
  194. $know = $info['data']['list']['content'][0]['content'] ?? [];
  195. if (isset($info['data']['list']['content'][0]['content']) && is_array($info['data']['list']['content'][0]['content'])) {
  196. $know = current(array_filter($info['data']['list']['content'][0]['content'], fn($v) => isset($v['name']) && $v['name'] == '购买须知'));
  197. if (isset($know['content']) && is_array($know['content'])) {
  198. $know = array_reduce(
  199. $know['content'], fn($v1, $v2) => $v1 .
  200. (isset($v2['name']) && is_string($v2['name']) ? "<h3>{$v2['name']}</h3>" : '') .
  201. (isset($v2['content']) && is_string($v2['content']) ? $v2['content'] : '')
  202. );
  203. }
  204. }
  205. //产品详情
  206. $content = '';
  207. if (isset($info['data']['list']['content'][0]['content']) && is_array($info['data']['list']['content'][0]['content'])) {
  208. $content = current(array_filter($info['data']['list']['content'][0]['content'], fn($v) => isset($v['key']) && $v['key'] == 'introduce'));
  209. if (isset($content['content']) && is_array($content['content'])) {
  210. $content = current(array_filter($content['content'], fn($v) => isset($v['key']) && $v['key'] == 'introduction'));
  211. $content = is_string($content['content']) ? $content['content'] : '';
  212. } else {
  213. $content = '';
  214. }
  215. }
  216. //扩展字段
  217. $extends = [];
  218. if (isset($info['data']['list']['base']['tags']) && is_array($info['data']['list']['base']['tags'])) {
  219. foreach ($info['data']['list']['base']['tags'] as $tag) {
  220. $extends['field_2_project'][] = ['name' => $tag, 'num' => '', 'price' => ''];
  221. }
  222. }
  223. CollectProduct::updateOrCreate(['unique_id' => $id, 'site' => 1], [
  224. 'unique_id' => $id,
  225. 'site' => 1,
  226. 'type' => 2, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项
  227. 'title' => mb_substr($info['data']['list']['base']['title'] ?? '', 0, 255),
  228. 'price' => $info['data']['list']['base']['price_zhanshi'] ?? 0,
  229. 'original_price' => ($info['data']['list']['base']['price_zhanshi'] ?? 0) * 1.58,
  230. 'pictures' => $info['data']['list']['base']['imgList'] ?? [],
  231. 'stock' => mt_rand(1000, 9999),
  232. 'sale' => $info['data']['list']['base']['sold']['num'] ?? 0,
  233. 'status' => -2, //-2下架
  234. 'know' => $know,
  235. 'content' => $content,
  236. 'extends' => $extends,
  237. 'logitude' => 0,
  238. 'latitude' => 0,
  239. 'address' => '',
  240. ]);
  241. $this->line($id . ' 采集完毕!' . PHP_EOL);
  242. }
  243. }
  244. }
  245. }