作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Sync;
  4 +
  5 +
  6 +use App\Exceptions\InquiryFilterException;
  7 +use App\Models\Project\Project;
  8 +use App\Models\SyncSubmitTask\SyncSubmitTaskZbj as SyncSubmitTaskModel;
  9 +use App\Services\SyncSubmitTaskService;
  10 +use Illuminate\Console\Command;
  11 +use Illuminate\Support\Facades\DB;
  12 +use Illuminate\Support\Facades\Redis;
  13 +use Illuminate\Support\Facades\Schema;
  14 +use Illuminate\Support\Str;
  15 +
  16 +/**
  17 + *
  18 + * Class SyncSubmitTask
  19 + * @package App\Console\Commands
  20 + * @author zbj
  21 + * @date 2023/11/28
  22 + */
  23 +class SyncSubmitTaskZbj extends Command
  24 +{
  25 +
  26 + protected $signature = 'sync_submit_task';
  27 + protected $description = '询盘、访问异步任务';
  28 +
  29 + public function handle()
  30 + {
  31 + $backup = false;
  32 + while (true) {
  33 + $task_id = $this->getTaskId();
  34 + if ($task_id > 2000000) {
  35 + $backup = true;
  36 + }
  37 + if (empty($task_id)) {
  38 + if ($backup) {
  39 + $this->backup();
  40 + $backup = false;
  41 + }
  42 + sleep(5);
  43 + continue;
  44 + }
  45 + $this->output('任务' . $task_id . '开始');
  46 + $task_info = SyncSubmitTaskModel::find($task_id);
  47 + if (empty($task_info) || $task_info->status) {
  48 + $this->output('任务不存在或者已执行');
  49 + continue;
  50 + }
  51 + try {
  52 + $project = Project::getProjectByDomain($task_info['data']['domain'] ?? '');
  53 + $task_info->project_id = $project->id;
  54 +
  55 + SyncSubmitTaskService::handler($task_info);
  56 + $task_info->status = 1;
  57 + $task_info->save();
  58 +
  59 + $this->output('任务完成');
  60 + } catch (InquiryFilterException $e) {
  61 + $task_info->status = 1;
  62 + $task_info->is_filtered = 1;
  63 + $task_info->remark = $e->getMessage();
  64 + $task_info->save();
  65 +
  66 + $this->output('任务完成');
  67 + } catch (\Exception $e) {
  68 + $task_info->retry = $task_info->retry + 1;
  69 + if ($task_info->retry >= 3) {
  70 + $task_info->status = 2;
  71 + $task_info->remark = Str::substr($e->getMessage(), 0, 200);
  72 + } else {
  73 + Redis::lpush('sync_submit_task', $task_id);
  74 + }
  75 + $task_info->save();
  76 +
  77 + $this->output('任务失败:' . $e->getMessage());
  78 + }
  79 + }
  80 + }
  81 +
  82 + public function getTaskId()
  83 + {
  84 + $task_id = Redis::rpop('sync_submit_task');
  85 + if (empty($task_id)) {
  86 + $ids = SyncSubmitTaskModel::where('status', 3)->limit(100)->pluck('id');
  87 + foreach ($ids as $id) {
  88 + Redis::lpush('sync_submit_task', $id);
  89 + }
  90 + $task_id = Redis::rpop('sync_submit_task');
  91 + }
  92 + return $task_id;
  93 + }
  94 +
  95 + /**
  96 + * 输出处理日志
  97 + */
  98 + public function output($message): bool
  99 + {
  100 + echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
  101 + return true;
  102 + }
  103 +
  104 + /**
  105 + * 备份数据
  106 + * @author zbj
  107 + * @date 2024/1/23
  108 + */
  109 + public function backup()
  110 + {
  111 + DB::beginTransaction();
  112 + try {
  113 + $table = (new SyncSubmitTaskModel())->getTable();
  114 + $new_table = $table . '_backup_' . date('Ymd');
  115 +
  116 + //重命名当前表
  117 + Schema::rename($table, $new_table);
  118 + //克隆表数据
  119 + DB::statement('CREATE TABLE ' . $table . ' LIKE ' . $new_table);
  120 +
  121 + DB::commit();
  122 +
  123 + $this->output('数据备份成功');
  124 + } catch (\Exception $e) {
  125 + $this->output('数据备份失败' . $e->getMessage());
  126 + DB::rollBack();
  127 + }
  128 + return true;
  129 + }
  130 +}
@@ -21,6 +21,7 @@ use Illuminate\Console\Command; @@ -21,6 +21,7 @@ use Illuminate\Console\Command;
21 use Illuminate\Database\Eloquent\Model; 21 use Illuminate\Database\Eloquent\Model;
22 use Illuminate\Support\Facades\DB; 22 use Illuminate\Support\Facades\DB;
23 use Illuminate\Support\Facades\Log; 23 use Illuminate\Support\Facades\Log;
  24 +use Illuminate\Support\Facades\Redis;
24 use Illuminate\Support\Str; 25 use Illuminate\Support\Str;
25 26
26 /** 27 /**
@@ -172,22 +173,32 @@ class WebTrafficFix extends Command @@ -172,22 +173,32 @@ class WebTrafficFix extends Command
172 $date = $this->argument('date'); 173 $date = $this->argument('date');
173 $need_num = $this->argument('need_num'); 174 $need_num = $this->argument('need_num');
174 175
175 - $project_list = $this->getProjectList($type); 176 + // $project_list = $this->getProjectList($type);
  177 + // exit;
  178 + while (true) {
176 179
  180 +
  181 + $project = Redis::rpop('web_traffic_fix');
  182 + if(empty($project)){
  183 +
  184 + break;
  185 + }
  186 + $project_list = [json_decode($project, true)];
177 foreach ($project_list as $project) { 187 foreach ($project_list as $project) {
178 ProjectServer::useProject($project['project_id']); 188 ProjectServer::useProject($project['project_id']);
179 - if($project['project_id'] <= 135){  
180 - continue;  
181 - } 189 + // if($project['project_id'] <= 135){
  190 + // continue;
  191 + // }
182 echo 'project_id:' . $project['project_id'] . PHP_EOL; 192 echo 'project_id:' . $project['project_id'] . PHP_EOL;
183 Log::info('web_traffic_fix project_id:' . $project['project_id']); 193 Log::info('web_traffic_fix project_id:' . $project['project_id']);
184 - $ip_num = DB::connection('custom_mysql')->table('gl_customer_visit')->whereDate('updated_date', $date)->count();  
185 - if($ip_num >= 30){ 194 + $ip_num = DB::connection('custom_mysql')->table('gl_customer_visit')->where('updated_date', $date)->count();
  195 + if ($ip_num >= 30) {
  196 + echo $ip_num . PHP_EOL;
186 continue; 197 continue;
187 } 198 }
188 199
189 $randomTime = []; 200 $randomTime = [];
190 - for ($i=0;$i<$need_num-$ip_num;$i++){ 201 + for ($i = 0; $i < $need_num - $ip_num; $i++) {
191 $randomTime[] = Carbon::make($date)->addSeconds(rand(0, 86400))->toDateTimeString(); 202 $randomTime[] = Carbon::make($date)->addSeconds(rand(0, 86400))->toDateTimeString();
192 } 203 }
193 sort($randomTime); 204 sort($randomTime);
@@ -195,7 +206,8 @@ class WebTrafficFix extends Command @@ -195,7 +206,8 @@ class WebTrafficFix extends Command
195 $project_urls = $this->getProductUrls($project['project_id']); 206 $project_urls = $this->getProductUrls($project['project_id']);
196 $project_urls['home'] = $project['domain']; 207 $project_urls['home'] = $project['domain'];
197 208
198 - foreach ($randomTime as $time){ 209 + $data = [];
  210 + foreach ($randomTime as $time) {
199 //随机引流间隔 211 //随机引流间隔
200 $res_sjjg = $this->get_rand($this->sjjg); 212 $res_sjjg = $this->get_rand($this->sjjg);
201 if ($res_sjjg == 1) { 213 if ($res_sjjg == 1) {
@@ -207,10 +219,9 @@ class WebTrafficFix extends Command @@ -207,10 +219,9 @@ class WebTrafficFix extends Command
207 //随机客户端 219 //随机客户端
208 $project['device_port'] = $this->get_rand($this->yddzb); 220 $project['device_port'] = $this->get_rand($this->yddzb);
209 $project['user_agent'] = $project['device_port'] == 1 ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua); 221 $project['user_agent'] = $project['device_port'] == 1 ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua);
210 - 222 + $a = time();
211 $project['ip'] = $this->getIpAreas([$project['project_id']], $time)[0] ?? ''; 223 $project['ip'] = $this->getIpAreas([$project['project_id']], $time)[0] ?? '';
212 -  
213 - foreach ($project['visit_urls'] as $url){ 224 + foreach ($project['visit_urls'] as $url) {
214 $time = Carbon::make($time)->addSeconds(rand(2, 15))->toDateTimeString(); 225 $time = Carbon::make($time)->addSeconds(rand(2, 15))->toDateTimeString();
215 $url_array = parse_url($project['domain']); 226 $url_array = parse_url($project['domain']);
216 $referrer_url = $this->getReferer($project['ip']['ip_area'], $project['lang']); 227 $referrer_url = $this->getReferer($project['ip']['ip_area'], $project['lang']);
@@ -226,14 +237,19 @@ class WebTrafficFix extends Command @@ -226,14 +237,19 @@ class WebTrafficFix extends Command
226 'referrer_url' => $referrer_url 237 'referrer_url' => $referrer_url
227 ], 238 ],
228 ]; 239 ];
229 - $task = new SyncSubmitTask();  
230 - $task->data = json_encode($array);  
231 - $task->type = SyncSubmitTask::TYPE_VISIT;  
232 - $task->created_at = $time;  
233 - $task->status = 3;  
234 - $task->traffic = 1;  
235 - $task->save(); 240 + $data[] = [
  241 + 'data' => json_encode($array),
  242 + 'type' => SyncSubmitTask::TYPE_VISIT,
  243 + 'created_at' => $time,
  244 + 'updated_at' => $time,
  245 + 'status' => 3,
  246 + 'traffic' => 1,
  247 + 'project_id' => $project['project_id'],
  248 + ];
  249 + }
  250 +
236 } 251 }
  252 + DB::table('gl_sync_submit_task_20240516')->insert($data);
237 } 253 }
238 } 254 }
239 Log::info('web_traffic_fix finish'); 255 Log::info('web_traffic_fix finish');
@@ -279,7 +295,7 @@ class WebTrafficFix extends Command @@ -279,7 +295,7 @@ class WebTrafficFix extends Command
279 ->get(); 295 ->get();
280 //其他地方在引流的域名 296 //其他地方在引流的域名
281 $other = DB::connection('projects_mysql')->table('projects')->where('switch', 1)->pluck('domain')->toArray(); 297 $other = DB::connection('projects_mysql')->table('projects')->where('switch', 1)->pluck('domain')->toArray();
282 - $data = []; 298 +
283 foreach ($list as $project) { 299 foreach ($list as $project) {
284 $lang = WebLanguage::getLangById($project['main_lang_id']??1)['short']; 300 $lang = WebLanguage::getLangById($project['main_lang_id']??1)['short'];
285 if(empty($project->domainInfo['domain'])){ 301 if(empty($project->domainInfo['domain'])){
@@ -289,13 +305,15 @@ class WebTrafficFix extends Command @@ -289,13 +305,15 @@ class WebTrafficFix extends Command
289 if(in_array($project->domainInfo['domain'], $other)){ 305 if(in_array($project->domainInfo['domain'], $other)){
290 continue; 306 continue;
291 } 307 }
292 - $data[] = [ 308 + $data = [
293 'project_id' => $project['project_id'], 309 'project_id' => $project['project_id'],
294 'domain' => 'https://' . $project->domainInfo['domain'] . '/', 310 'domain' => 'https://' . $project->domainInfo['domain'] . '/',
295 'lang' => $lang 311 'lang' => $lang
296 ]; 312 ];
  313 +
  314 + Redis::lpush('web_traffic_fix', json_encode($data));
297 } 315 }
298 - return $data; 316 + return true;
299 } 317 }
300 318
301 /** 319 /**
@@ -355,19 +373,26 @@ class WebTrafficFix extends Command @@ -355,19 +373,26 @@ class WebTrafficFix extends Command
355 $main_countries = !empty($config->main_countries) ? explode(',',$config->main_countries) : []; 373 $main_countries = !empty($config->main_countries) ? explode(',',$config->main_countries) : [];
356 $filter_countries = !empty($config->filter_countries) ? explode(',',$config->filter_countries) : []; 374 $filter_countries = !empty($config->filter_countries) ? explode(',',$config->filter_countries) : [];
357 375
358 - //根据地区随机取该地区的IP  
359 - $ipdata = DB::table('gl_xunpan_ipdata')->whereIn('ip_area', $areas)  
360 - ->where(function ($query) use ($main_countries, $filter_countries){  
361 if($main_countries){ 376 if($main_countries){
362 - $query->whereIn('ip_area', $main_countries); 377 + $areas = $main_countries;
363 } 378 }
  379 +
364 if($filter_countries){ 380 if($filter_countries){
365 - $query->whereNotIn('ip_area', $main_countries); 381 + $areas2 = [];
  382 + foreach ($areas as $v){
  383 + if(!in_array($v, $filter_countries)){
  384 + $areas2[] = $v;
  385 + }
  386 + }
  387 + $areas = $areas2;
366 } 388 }
367 - })->inRandomOrder()->first(); 389 +
  390 + //根据地区随机取该地区的IP
  391 + $ipdata = DB::table('gl_xunpan_ipdata')->whereIn('ip_area', $areas)->inRandomOrder()->first();
368 if(!$ipdata){ 392 if(!$ipdata){
369 continue; 393 continue;
370 } 394 }
  395 +
371 $ipdata = (array)$ipdata ?: []; 396 $ipdata = (array)$ipdata ?: [];
372 $ipdata['diff'] = $time_zones[$ipdata['ip_area']]; 397 $ipdata['diff'] = $time_zones[$ipdata['ip_area']];
373 $data[] = $ipdata; 398 $data[] = $ipdata;
  1 +<?php
  2 +
  3 +namespace App\Models\SyncSubmitTask;
  4 +
  5 +use Illuminate\Database\Eloquent\Model;
  6 +use Illuminate\Support\Facades\Log;
  7 +use Illuminate\Support\Facades\Redis;
  8 +
  9 +/**
  10 + * @method static where(string $string, mixed $ip)
  11 + * @method static create(array $data)
  12 + */
  13 +class SyncSubmitTaskZbj extends Model
  14 +{
  15 +
  16 + const TYPE_INQUIRY = 'inquiry';
  17 + const TYPE_VISIT = 'visit';
  18 +
  19 + const TRAFFIC_DEFAULT = 0;
  20 + const TRAFFIC_TRUE = 1;
  21 +
  22 + //设置关联表名
  23 + /**
  24 + * @var mixed
  25 + */
  26 + protected $table = 'gl_sync_submit_task_20240516';
  27 +
  28 + protected $casts = [
  29 + 'data' => 'array',
  30 + ];
  31 +
  32 + /**
  33 + * @param $type
  34 + * @param $data
  35 + * @return bool
  36 + * @author zbj
  37 + * @date 2023/11/28
  38 + */
  39 + public static function addTask($type, $data): bool
  40 + {
  41 + if (empty($data)) {
  42 + return false;
  43 + }
  44 + try {
  45 + $data = [
  46 + 'data' => $data['data'],
  47 + 'domain' => !empty($data['domain']) ? $data['domain'] : request()->getHost(),
  48 + 'ip' => !empty($data['ip']) ? $data['ip'] : request()->getClientIp(),
  49 + 'referer' => !empty($data['referer']) ? $data['referer'] : request()->header('Referer'),
  50 + 'user_agent' => !empty($data['user_agent']) ? $data['user_agent'] : request()->header('user_agent'),
  51 + ];
  52 +
  53 + if(empty($data['referer']) || empty($data['user_agent']) || empty($data['data'])){
  54 + return false;
  55 + }
  56 +
  57 + $model = new self();
  58 + $model->type = $type;
  59 + $model->data = $data;
  60 + !empty($data['submit_time']) && $model->created_at = $data['submit_time'];
  61 + $model->save();
  62 +
  63 + } catch (\Exception $e) {
  64 + Log::error('SyncSubmitTask addTask error', ['msg' => $e->getMessage(), 'data' => $data]);
  65 + return false;
  66 + }
  67 + return true;
  68 + }
  69 +}