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