作者 刘锟

询盘数据采集

@@ -5,10 +5,14 @@ @@ -5,10 +5,14 @@
5 * Date: 2025/2/13 5 * Date: 2025/2/13
6 * Time: 16:45 6 * Time: 16:45
7 */ 7 */
  8 +
8 namespace App\Console\Commands\Inquiry; 9 namespace App\Console\Commands\Inquiry;
9 10
  11 +use App\Helper\Translate;
  12 +use App\Models\Inquiry\InquiryInfo;
10 use App\Services\InquiryRelayService; 13 use App\Services\InquiryRelayService;
11 use Illuminate\Console\Command; 14 use Illuminate\Console\Command;
  15 +use Illuminate\Support\Facades\DB;
12 16
13 class SyncInquiryRelay extends Command 17 class SyncInquiryRelay extends Command
14 { 18 {
@@ -36,39 +40,125 @@ class SyncInquiryRelay extends Command @@ -36,39 +40,125 @@ class SyncInquiryRelay extends Command
36 parent::__construct(); 40 parent::__construct();
37 } 41 }
38 42
39 - /**  
40 - * @return bool  
41 - */ 43 +
42 public function handle() 44 public function handle()
43 { 45 {
44 $this->getInquiryForm(); 46 $this->getInquiryForm();
45 $this->getInquirySzcm(); 47 $this->getInquirySzcm();
46 - return true;  
47 } 48 }
48 49
49 /** 50 /**
50 * 获取表单系统中询盘信息 51 * 获取表单系统中询盘信息
  52 + * message_sign唯一值:md5(name+email+phone+ip+time)
51 */ 53 */
52 public function getInquiryForm() 54 public function getInquiryForm()
53 - {} 55 + {
  56 + $service = new InquiryRelayService();
  57 + $result = $service->getInquiryRelay();
  58 + if (isset($result['status']) && $result['status'] == 200) {
  59 + $data = $result['data'] ?? [];
  60 + foreach ($data as $item) {
  61 + $this->saveDate($item, $item['source_type']);
  62 + }
  63 + }
  64 + }
54 65
55 /** 66 /**
56 * 获取秋哥asp采集询盘信息 67 * 获取秋哥asp采集询盘信息
  68 + * type固定4
  69 + * message_sign唯一值:md5(name+email+phone+ip+time)
  70 + * 先查数据库最大origin_key,再获取数据
57 */ 71 */
58 public function getInquirySzcm() 72 public function getInquirySzcm()
59 { 73 {
60 - $serveice = new InquiryRelayService();  
61 - $result = $serveice->getInquirySzcm(1);  
62 - dd($result); 74 + $max_origin_key = InquiryInfo::where('type', 4)->orderBy('origin_key', 'desc')->value('origin_key');
  75 + if ($max_origin_key) {
  76 + $start_id = $max_origin_key + 1;
  77 + } else {
  78 + $start_id = 65029;
  79 + }
  80 +
  81 + $service = new InquiryRelayService();
  82 + while ($start_id > 0) {
  83 + $result = $service->getInquirySzcm($start_id);
  84 + if ($result) {
  85 + $this->saveDate($result, 4);
  86 + $start_id += 1;
  87 + } else {
  88 + $start_id = 0;
  89 + }
  90 + }
63 } 91 }
64 92
65 - public function saveDate($data) 93 + public function saveDate($data, $type)
66 { 94 {
67 - // 数据里面 可能没有 IP 这时候需要通过 submit_country 字段获取对应国家,在通过国家随机获取IP , 如果IP submit_country 都没有就原样记录  
68 - if (empty($data['ip'])) { 95 + $model = new InquiryInfo();
  96 + $message_sign = md5($data['name'] . $data['email'] . $data['phone'] . $data['ip'] . $data['time']);
  97 + $inquiry_info = $model->where('message_sign', $message_sign)->first();
  98 + if (!$inquiry_info) {
  99 + //没有IP,根据submit_country获取
  100 + if (empty($data['ip']) && isset($data['submit_country']) && $data['submit_country']) {
  101 + $chinese_name = DB::table('gl_world_country_city')->where('pid', 0)->where('iso2', $data['submit_country'])->value('chinese_name');
  102 + $data['ip'] = $this->getIpData($chinese_name);
  103 + }
  104 +
  105 + //获取country
  106 + $country = '';
  107 + if ($data['ip']) {
  108 + $country = file_get_contents("http://ip.globalso.com?ip=" . $data['ip']);
  109 + }
69 110
  111 + //翻译message
  112 + $message_cn = '';
  113 + $re_message_trans = Translate::translate($data['message'], 'zh');
  114 + if (isset($re_message_trans[0]['code']) && $re_message_trans[0]['code'] == 200) {
  115 + $message_cn = $re_message_trans[0]['texts'];
  116 + }
  117 +
  118 + //获取页面上title和keywords
  119 + $html = curl_c($data['refer'], false);
  120 +
  121 + if (empty($data['title'])) {
  122 + preg_match_all('/<title>([\w\W]*?)<\/title>/', $html, $matches);
  123 + if (!empty($matches[1])) {
  124 + $data['title'] = substr($matches[1][0], 0, 255);
  125 + }
  126 + }
  127 +
  128 + $keywords = '';
  129 + preg_match_all('/<meta\s+[^>]*?name=[\'|\"]keywords[\'|\"]\s+[^>]*?content=[\'|\"]([\w\W]*?)[\'|\"]/', $html, $matches);
  130 + if (!empty($matches[1])) {
  131 + $keywords = substr($matches[1][0], 0, 255);
  132 + }
  133 +
  134 + if (empty($data['origin_key'])) {
  135 + $data['origin_key'] = 0;
  136 + }
  137 +
  138 + if (empty($data['image'])) {
  139 + $data['image'] = '';
  140 + }
  141 +
  142 + $model->createInquiry($data['name'], $data['phone'], $data['email'], $data['ip'], $country, $data['message'], $message_cn, $type, $data['time'], $data['refer'], $data['title'], $keywords, $message_sign, $data['origin_key'], $data['image']);
70 } 143 }
  144 + }
71 145
72 - $country = file_get_contents("http://ip.globalso.com?ip=" . $data['ip']); 146 + /**
  147 + * 获取国家随机ip
  148 + * @param $country_name
  149 + * @return mixed|string
  150 + * @author Akun
  151 + * @date 2025/02/14 14:19
  152 + */
  153 + public function getIpData($country_name)
  154 + {
  155 + // 有国家 通过国家查询, 如果没有获取到就随机获取
  156 + $where = [];
  157 + $country_name && $where['ip_area'] = $country_name;
  158 + $ip = DB::table('gl_xunpan_ipdata')->where($where)->inRandomOrder()->value('ip');
  159 + if (empty($ip_data)) {
  160 + $ip = DB::table('gl_xunpan_ipdata')->inRandomOrder()->value('ip');
  161 + }
  162 + return $ip;
73 } 163 }
74 -}  
  164 +}