PostInquiryForward.php
9.5 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* Created by PhpStorm.
* User: akun
* Date: 2025/02/26
* Time: 10:14
*/
namespace App\Console\Commands\Inquiry;
use App\Models\Inquiry\InquiryInfo;
use App\Models\Inquiry\InquiryProject;
use App\Models\Inquiry\InquiryRelayDetail;
use App\Models\Inquiry\InquiryRelayDetailLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
/**
* Class postInquiry
* @package App\Console\Commands\Inquiry
*/
class PostInquiryForward extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'post_inquiry_forward';
/**
* 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 = InquiryRelayDetailLog::where('status', InquiryRelayDetailLog::STATUS_INIT)->where('start_at', '<=', date('Y-m-d H:i:s'))->orderBy('id', 'asc')->limit(100)->get();
if (!$list->count()) {
sleep(1);
}
// 询盘数据入库
foreach ($list as $key => $val) {
//加个锁 避免重复执行
$lockKey = 'inquiry_relay_detail_log_lock_' . $val->id;
if (!Redis::setnx($lockKey, 1)) {
continue;
}
Redis::expire($lockKey, 60);
$log = InquiryRelayDetailLog::find($val->id);
if ($log->status != InquiryRelayDetailLog::STATUS_INIT) {
continue;
}
$this->output('开始执行' . $val->id);
try {
$detail = InquiryRelayDetail::find($val['detail_id']);
//防止 程序中断 启动时 同一时间一下就发了
$last_log = InquiryRelayDetailLog::where('detail_id', $val['detail_id'])->where('id', '<', $val['id'])->orderBy('id', 'desc')->first();
//不是第一个 或者 上一个处理失败了 直接处理
if (!empty($last_log)) {
//上一个还没有处理 直接跳过
if ($last_log->status == InquiryRelayDetailLog::STATUS_INIT) {
continue;
}
//上一个处理完成
if ($last_log->status == InquiryRelayDetailLog::STATUS_SUCCESS) {
//上次处理时间间隔达到没
$interval = strtotime($val->start_at) - strtotime($last_log->start_at);
if (time() - strtotime($last_log->updated_at) < $interval) {
continue;
}
}
}
if ($val['type'] == 1) {
$this->visit($detail, $val);
} else {
$res = $this->inquiry($detail, $val);
//转发表单
if ($res) {
$form = InquiryInfo::find($detail['form_id']);
$form->success = $form->success + 1;
$form->save();
Log::channel('inquiry_forward')->info('询盘成功:', [$detail['form_id'], $val->id, getmypid()]);
}
}
} catch (\Exception $e) {
Log::channel('inquiry_forward')->error('post_inquiry_forward handle error', [$e->getMessage(), $e->getFile(), $e->getLine()]);
$val->status = InquiryRelayDetailLog::STATUS_FAIL;
$val->remark = mb_substr($e->getMessage(), 0, 200);
$val->save();
}
}
}
}
public function visit(InquiryRelayDetail $detail, InquiryRelayDetailLog $log)
{
$website = 'https://' . $detail['website'] . '/';
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()->timeout(30)->post($website . 'api/traffic_visit/', $data)->json();
if (empty($res['status']) || $res['status'] != 200) {
$log->status = InquiryRelayDetailLog::STATUS_FAIL;
$log->remark = mb_substr($res['message'] ?? '', 0, 200);
$log->save();
Log::channel('inquiry_forward')->error('post_inquiry_forward visit error', [$res, $website . 'api/traffic_visit/', $data]);
return false;
}
} else {
//v4 v5分离项目 往测试链接推
$project_info = InquiryProject::select(['is_split', 'test_domain'])->where('domain', 'like', '%' . $detail['website'] . '%')->first();
if (!$project_info) {
$log->status = InquiryRelayDetailLog::STATUS_FAIL;
$log->remark = '获取域名对应项目失败';
$log->save();
return false;
}
if ($project_info['is_split'] == 1) {
$website = $project_info['test_domain'];
}
$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);
$status = $res->status();
if ($status != 200) {
$log->status = InquiryRelayDetailLog::STATUS_FAIL;
$log->remark = mb_substr($res->body() ?? '', 0, 200);
$log->save();
Log::channel('inquiry_forward')->error('post_inquiry_forward v4|v5 visit error', [$res->body(), $website . 'wp-admin/admin-ajax.php', $data]);
return false;
}
}
$log->status = InquiryRelayDetailLog::STATUS_SUCCESS;
$log->save();
return true;
}
public function inquiry(InquiryRelayDetail $detail, InquiryRelayDetailLog $log)
{
// v6
if ($detail['is_v6']) {
$res = $this->v6Inquiry($detail, $log);
} else {
$res = $this->v5Inquiry($detail, $log);
}
if (!$res) {
return false;
}
$log->status = InquiryRelayDetailLog::STATUS_SUCCESS;
$log->save();
return true;
}
public function v6Inquiry($detail, $log)
{
$website = 'https://' . $detail['website'] . '/';
$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($website, '/');
}
$res = Http::withoutVerifying()->timeout(30)->withHeaders(['User-Agent' => $detail['user_agent']])->post($website . 'api/inquiryQd?source=5', $data)->json();
if (empty($res['code']) || $res['code'] != 200) {
$log->status = InquiryRelayDetailLog::STATUS_FAIL;
$log->remark = mb_substr($res['message'] ?? '', 0, 200);
$log->save();
Log::channel('inquiry_forward')->error('post_inquiry_forward v6 inquiry error', [$res, $website . 'api/inquiryQd/', $data]);
return false;
}
return true;
}
public function v5Inquiry($detail, $log)
{
$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,
];
$result = Http::withoutVerifying()->timeout(30)->post('https://www.globalso.site/api/external-interface/add/fa043f9cbec6b38f', $data);
$res['data'][0]['status'] = 'success';
//兼容接口返回格式
if (!empty($res['data'][0]['status'])) {
$res['data'][0]['code'] = $res['data'][0]['status'] == 'success' ? 200 : 400;
!empty($res['data'][0]['msg']) && $res['message'] = $res['data'][0]['msg'];
}
if (empty($res['data'][0]['code']) || !in_array($res['data'][0]['code'], [200, 300])) {
$log->status = InquiryRelayDetailLog::STATUS_FAIL;
$log->remark = mb_substr($res['message'] ?? '', 0, 200);
$log->save();
Log::channel('inquiry_forward')->error('post_inquiry_forward v4|v5 inquiry error', [$result->body(), 'https://www.globalso.site/api/external-interface/add/fa043f9cbec6b38f', $data]);
return false;
}
return true;
}
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}