postInquiry.php
7.1 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace App\Console\Commands\Inquiry;
use App\Models\Inquiry\ReInquiryDetail;
use App\Models\Inquiry\ReInquiryDetailLog;
use App\Models\Inquiry\ReInquiryForm;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
/**
* Class postInquiry
* @package App\Console\Commands\Inquiry
*/
class postInquiry extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'post_inquiry';
/**
* The console command description.
*
* @var string
*/
protected $description = '执行询盘请求';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
while (true) {
$list = ReInquiryDetailLog::where('status', ReInquiryDetailLog::STATUS_INIT)->where('start_at', '<=', date('Y-m-d H:i:s'))->limit(100)->get();
if (!$list->count()){
//5分钟同步一次
sleep(1);
}
// 询盘数据入库
foreach ($list as $key => $val) {
$this->output('开始执行' . $val->id);
try {
$detail = ReInquiryDetail::find($val['detail_id']);
if($val['type'] == 1){
$this->visit($detail, $val);
}else{
$res = $this->inquiry($detail, $val);
//转发详情
$detail->status = $res ? ReInquiryDetail::STATUS_SUCCESS : ReInquiryDetail::STATUS_FAIL;
$detail->result = $val['url'];
$detail->save();
//转发表单
if($res){
$form = ReInquiryForm::find($detail['form_id']);
$form->success_num = $form->success_num + 1;
$form->save();
}
}
}catch (\Exception $e){
Log::channel('inquiry_relay')->error('inquiry_relay handle error', [$e->getMessage(), $e->getFile(), $e->getLine()]);
$val->status = ReInquiryDetailLog::STATUS_FAIL;
$val->remark = mb_substr($e->getMessage(), 0, 200);
$val->save();
}
}
}
}
public function visit(ReInquiryDetail $detail, ReInquiryDetailLog $log){
if($detail['is_v6']) {
$data = [
'ip' => $detail['ip'],
'url' => $log['url'],
'device_port' => $detail['device_port'],
'referrer_url' => $detail['referrer'],
'user_agent' => $detail['user_agent'],
];
$res = Http::withoutVerifying()->post($detail['re_website'] . 'api/traffic_visit/', $data)->json();
if (empty($res['status']) || $res['status'] != 200) {
$log->status = ReInquiryDetailLog::STATUS_FAIL;
$log->remark = $res['message'] ?? '';
$log->save();
Log::channel('inquiry_relay')->error('inquiry_relay visit error', [$res, $detail['re_website'] . 'api/traffic_visit/',$data]);
return false;
}
}else{
//v4 v5分离项目 往测试链接推
$website = $detail['re_website'];
$site_array = file_get_contents('https://www.quanqiusou.cn/extend_api/saas/split.php');
$site_array = json_decode($site_array, true);
$mail_urls = array_column($site_array, 'main_url');
$key = array_search($detail['re_website'], $mail_urls);
if ($key !== false) {
// 分离项目 推送到测试链接
$website = $site_array[$key]['test_url'];
}
$data = [
'action' => 'stats_init',
'assort' => 0,
'referrer' => $detail['referrer'],
'currweb' => $log['url'],
'user_agent' => $detail['user_agent'],
"ip" => $detail['ip'],
];
$res = Http::get($website . 'wp-admin/admin-ajax.php', $data)->json();
if($res != 200){
$log->status = ReInquiryDetailLog::STATUS_FAIL;
$log->remark = $res['message'] ?? '';
$log->save();
Log::channel('inquiry_relay')->error('inquiry_relay v4|v5 visit error', [$res, $website . 'wp-admin/admin-ajax.php', $data]);
return false;
}
}
$log->status = ReInquiryDetailLog::STATUS_SUCCESS;
$log->save();
return true;
}
public function inquiry(ReInquiryDetail $detail, ReInquiryDetailLog $log){
// v6
if($detail['is_v6']) {
$data = [
'name' => $detail['name'],
'phone' => $detail['phone'],
'message' => $detail['message'],
'submit_ip' => $detail['ip'],
'refer' => $log->url,
];
if($detail->email){
$data['email'] = $detail->email;
}else{
$data['__amp_source_origin'] = trim($detail['re_website'], '/');
}
$res = Http::withoutVerifying()->withHeaders(['User-Agent' => $detail['user_agent']])->post($detail['re_website'] . 'api/inquiryQd/', $data)->json();
if(empty($res['code']) || $res['code'] != 200){
$log->status = ReInquiryDetailLog::STATUS_FAIL;
$log->remark = $res['message'] ?? '';
$log->save();
Log::channel('inquiry_relay')->error('inquiry_relay v6 inquiry error', [$res, $detail['website'] . 'api/inquiryQd/', $data]);
return false;
}
}else{
$data = [
'name' => $detail['name'],
'phone' => $detail['phone'],
'message' => $detail['message'],
'email' => $detail['email'],
'ip' => $detail['ip'],
'token' => md5($log->url . $detail['name'] . $detail['ip'] . date("Y-m-d")),
'refer' => $log->url,
'submit_time' => date('Y-m-d H:i:s'),
'source' => 5,
];
$res = Http::withoutVerifying()->post('https://form.globalso.com/api/external-interface/add/fa043f9cbec6b38f', $data)->json();
if(empty($res['code']) || $res['code'] != 200){
$log->status = ReInquiryDetailLog::STATUS_FAIL;
$log->remark = $res['message'] ?? '';
$log->save();
Log::channel('inquiry_relay')->error('inquiry_relay v4|v5 inquiry error', [$res, 'https://form.globalso.com/api/external-interface/add/fa043f9cbec6b38f', $data]);
return false;
}
}
$log->status = ReInquiryDetailLog::STATUS_SUCCESS;
$log->save();
return true;
}
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}