作者 赵彬吉

引流修复脚本

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\WebTraffic;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Models\HomeCount\Count;
  7 +use App\Models\Product\Category;
  8 +use App\Models\Product\Product;
  9 +use App\Models\Project\OnlineCheck;
  10 +use App\Models\Project\Project;
  11 +use App\Models\Project\WebTrafficConfig;
  12 +use App\Models\Visit\SyncSubmitTask;
  13 +use App\Models\Visit\Visit;
  14 +use App\Models\WebSetting\WebLanguage;
  15 +use App\Services\ProjectServer;
  16 +use Carbon\Carbon;
  17 +use Facade\Ignition\DumpRecorder\Dump;
  18 +use GuzzleHttp\Client;
  19 +use GuzzleHttp\Promise\Utils;
  20 +use Illuminate\Console\Command;
  21 +use Illuminate\Database\Eloquent\Model;
  22 +use Illuminate\Support\Facades\DB;
  23 +use Illuminate\Support\Facades\Log;
  24 +use Illuminate\Support\Str;
  25 +
  26 +/**
  27 + * 网站引流 修复
  28 + * Class Traffic
  29 + * @package App\Console\Commands
  30 + * @author zbj
  31 + * @date 2023/5/18
  32 + */
  33 +class WebTrafficFix extends Command
  34 +{
  35 +
  36 + /**
  37 + * The name and signature of the console command.
  38 + *
  39 + * @var string
  40 + */
  41 + protected $signature = 'web_traffic_fix {type} {date} {need_num}'; // 1 2024-05-16 44
  42 +
  43 + /**
  44 + * The console command description.
  45 + *
  46 + * @var string
  47 + */
  48 + protected $description = '网站引流';
  49 +
  50 + /**
  51 + * Create a new command instance.
  52 + *
  53 + * @return void
  54 + */
  55 + public function __construct()
  56 + {
  57 + parent::__construct();
  58 + }
  59 +
  60 + /**
  61 + * google域名后缀
  62 + * @var string[]
  63 + */
  64 + protected $suffix = [
  65 + 'co.jp' => '日本',
  66 + 'com.tr' => '土耳其',
  67 + 'nl' => '荷兰',
  68 + 'ru' => '俄罗斯',
  69 + 'fr' => '法国',
  70 + 'co.kr' => '韩国',
  71 + 'fi' => '芬兰',
  72 + 'be' => '比利时',
  73 + 'lt' => '立陶宛',
  74 + 'es' => '西班牙',
  75 + 'it' => '意大利',
  76 + 'com.au' => '澳大利亚',
  77 + 'no' => '挪威',
  78 + 'al' => '阿尔巴尼亚',
  79 + 'pt' => '葡萄牙',
  80 + 'lv' => '拉脱维亚',
  81 + 'hu' => '匈牙利',
  82 + 'cz' => '捷克',
  83 + 'de' => '德国',
  84 + 'ca' => '加拿大',
  85 + 'co.in' => '印度',
  86 + 'co.uk' => '英国',
  87 + 'com.vn' => '越南',
  88 + 'com.br' => '巴西',
  89 + 'co.il' => '以色列',
  90 + 'pl' => '波兰',
  91 + 'com.eg' => '埃及',
  92 + 'co.th' => '泰国',
  93 + 'sk' => '斯洛伐克',
  94 + 'ro' => '罗马尼亚',
  95 + 'com.mx' => '墨西哥',
  96 + 'com.my' => '马来西亚',
  97 + 'com.pk' => '巴基斯坦',
  98 + 'co.nz' => '新西兰',
  99 + 'co.za' => '南非',
  100 + 'com.ar' => '阿根廷',
  101 + 'com.kw' => '科威特',
  102 + 'com.sg' => '新加坡',
  103 + 'com.co' => '哥伦比亚',
  104 + 'co.id' => '印度尼西亚',
  105 + 'gr' => '希腊',
  106 + 'bg' => '保加利亚',
  107 + 'mn' => '蒙古',
  108 + 'dk' => '丹麦',
  109 + 'com.sa' => '沙特阿拉伯',
  110 + 'com.pe' => '秘鲁',
  111 + 'com.ph' => '菲律宾',
  112 + 'com.ua' => '乌克兰',
  113 + 'ge' => '格鲁吉亚',
  114 + 'ae' => '阿拉伯联合酋长国',
  115 + 'tn' => '突尼斯',
  116 + ];
  117 +
  118 + /**
  119 + * 概率值
  120 + * @var int[]
  121 + */
  122 + protected $sjjg = [720, 280];//访问间隔占比 访问|不访问
  123 + //访问页面类型占比 产品详情页、单页|产品分类页
  124 + protected $ymzb = [
  125 + 'urls_cats' => 700,
  126 + 'urls_details' => 300
  127 + ];
  128 + protected $sdzb = [600, 200, 150, 50]; //访问页面深度占比 1页|2页|3-6页|7-11页
  129 + protected $yddzb = [1 => 700, 2 => 300]; //移动端占比 pc|mobile
  130 + //模拟访问来源占比 (美国)
  131 + protected $lyzb = [
  132 + 'https://www.google.com/' => 630,
  133 + 'http://www.google.com/' => 30,
  134 + 'http://www.bing.com/' => 20,
  135 + 'https://www.bing.com/' => 5,
  136 + 'https://www.youtube.com/' => 5,
  137 + 'https://search.yahoo.com/' => 5,
  138 + 'https://www.facebook.com/' => 5,
  139 + ];
  140 +
  141 + //俄语
  142 + protected $eylyzb = [
  143 + 'https://www.yandex.com/' => 630,
  144 + 'https://www.google.com/' => 30,
  145 + 'http://www.google.com/' => 30,
  146 + 'http://www.bing.com/' => 20,
  147 + 'https://www.bing.com/' => 5,
  148 + 'https://www.youtube.com/' => 5,
  149 + 'https://search.yahoo.com/' => 5,
  150 + 'https://www.facebook.com/' => 5,
  151 + ];
  152 +
  153 + protected $otherzb = [700, 300]; //模拟访问来源占比 (非美国) google.com|google.其他后缀
  154 +
  155 + protected $pc_ua = [
  156 + 0 => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
  157 + 1 => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36',
  158 + 2 => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
  159 + ];
  160 +
  161 + protected $mobile_ua = [
  162 + 0 => 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko; googleweblight) Chrome/38.0.1025.166 Mobile Safari/535.19',
  163 + ];
  164 +
  165 + /**
  166 + * @return bool
  167 + */
  168 + public function handle()
  169 + {
  170 + try {
  171 + $type = $this->argument('type');
  172 + $date = $this->argument('date');
  173 + $need_num = $this->argument('need_num');
  174 +
  175 + $project_list = $this->getProjectList($type);
  176 +
  177 + foreach ($project_list as $project) {
  178 + echo $project['project_id'] . PHP_EOL;
  179 + $project['project_id'] = 1;
  180 + $ip_num = Count::where('project_id', $project['project_id'])->where('date', $date)->value('ip_num') ?: 0;
  181 + if($ip_num >= 30){
  182 + continue;
  183 + }
  184 +
  185 + for ($i=0;$i<$need_num;$i++){
  186 + $randomTime[] = Carbon::make($date)->addSeconds(rand(0, 86400))->toDateTimeString();
  187 + }
  188 + sort($randomTime);
  189 +
  190 + $project_urls = $this->getProductUrls($project['project_id']);
  191 + $project_urls['home'] = $project['domain'];
  192 +
  193 + foreach ($randomTime as $time){
  194 + //随机引流间隔
  195 + $res_sjjg = $this->get_rand($this->sjjg);
  196 + if ($res_sjjg == 1) {
  197 + echo 'niyin' . PHP_EOL;
  198 + continue;
  199 + }
  200 +
  201 + //随机访问页面
  202 + $project['visit_urls'] = $this->getVisitUrls($project_urls);
  203 + //随机客户端
  204 + $project['device_port'] = $this->get_rand($this->yddzb);
  205 + $project['user_agent'] = $project['device_port'] == 1 ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua);
  206 +
  207 + $project['ip'] = $this->getIpAreas([$project['project_id']], $time)[0] ?? '';
  208 +
  209 + foreach ($project['visit_urls'] as $url){
  210 + $time = Carbon::make($time)->addSeconds(rand(2, 15))->toDateTimeString();
  211 + $url_array = parse_url($project['domain']);
  212 + $referrer_url = $this->getReferer($project['ip']['ip_area'], $project['lang']);
  213 + $array = [
  214 + 'ip' => $project['ip']['ip'],
  215 + 'domain' => $url_array['host'] ?? '',
  216 + 'referer' => $referrer_url,
  217 + 'user_agent' => $project['user_agent'],
  218 + 'data' => [
  219 + 'url' => $url,
  220 + 'domain' => empty($url_array['host']) ? '' : $url_array['scheme'] . '://' . $url_array['host'],
  221 + 'device_port' => in_array($project['device_port'], array_keys(Visit::deviceMap())) ? $project['device_port'] : Visit::DEVICE_PC,
  222 + 'referrer_url' => $referrer_url
  223 + ],
  224 + ];
  225 + $task = new SyncSubmitTask();
  226 + $task->data = json_encode($array);
  227 + $task->type = SyncSubmitTask::TRAFFIC_DEFAULT;
  228 + $task->created_at = $time;
  229 + $task->status = 3;
  230 + $task->traffic = 1;
  231 + $task->save();
  232 + }
  233 + }
  234 + }
  235 + }catch (\Exception $e){
  236 + dump($e->getMessage());
  237 + }
  238 + }
  239 +
  240 + /**
  241 + * 非俄语站的引流的项目
  242 + */
  243 + protected function getProjectList($type){
  244 + $ru_lang_id = WebLanguage::getIdByLang('ru');
  245 +
  246 + //推广项目
  247 + $list = Project::with('domainInfo')
  248 + ->leftJoin('gl_project_deploy_optimize as pdo', 'pdo.project_id', '=', 'gl_project.id')
  249 + ->leftJoin('gl_project_online_check as poc', 'poc.project_id', '=', 'gl_project.id')
  250 + ->where('pdo.domain', '>', 0)
  251 + ->where('poc.qa_status', OnlineCheck::STATUS_ONLINE_TRUE)
  252 + ->whereIn('gl_project.type', [Project::TYPE_TWO, Project::TYPE_FOUR])
  253 + ->where('gl_project.is_upgrade', 0) //非升级项目
  254 + ->where('gl_project.main_lang_id', '<>', $ru_lang_id) //非俄语站
  255 + ->where(function ($query) use ($type) {
  256 + if($type == 1){
  257 + //1-3个月项目
  258 + $startTime = Carbon::now()->addMonths(-4)->toDateString();
  259 + $endTime = Carbon::now()->addMonths(-1)->toDateString();
  260 + $query->whereBetween('pdo.start_date', [$startTime,$endTime]);
  261 + }elseif($type == 2){
  262 + //4-8个月项目
  263 + $startTime = Carbon::now()->addMonths(-9)->startOfDay()->toDateTimeString();
  264 + $endTime = Carbon::now()->addMonths(-4)->endOfDay()->toDateTimeString();
  265 + $query->whereBetween('pdo.start_date', [$startTime,$endTime]);
  266 + }else{
  267 + //大于9个月项目
  268 + $startTime = Carbon::now()->addMonths(-9)->startOfDay()->toDateTimeString();
  269 + $query->where('pdo.start_date', '<', $startTime);
  270 + }
  271 + })->select(['pdo.project_id','gl_project.main_lang_id','gl_project.id'])
  272 + ->orderBy('project_id')
  273 + ->get();
  274 + //其他地方在引流的域名
  275 + $other = DB::connection('projects_mysql')->table('projects')->where('switch', 1)->pluck('domain')->toArray();
  276 + $data = [];
  277 + foreach ($list as $project) {
  278 + $lang = WebLanguage::getLangById($project['main_lang_id']??1)['short'];
  279 + if(empty($project->domainInfo['domain'])){
  280 + continue;
  281 + }
  282 + //其他地方在引流就不再引流了
  283 + if(in_array($project->domainInfo['domain'], $other)){
  284 + continue;
  285 + }
  286 + $data[] = [
  287 + 'project_id' => $project['project_id'],
  288 + 'domain' => 'https://' . $project->domainInfo['domain'] . '/',
  289 + 'lang' => $lang
  290 + ];
  291 + }
  292 + return $data;
  293 + }
  294 +
  295 + /**
  296 + * 获取产品分类、单页和详情链接
  297 + */
  298 + protected function getProductUrls($project_id){
  299 + ProjectServer::useProject($project_id);
  300 + //已发布产品分类页面
  301 + $data['urls_cats'] = DB::connection('custom_mysql')->table('gl_product_category')
  302 + ->where('project_id', $project_id)->where('status', Category::STATUS_ACTIVE)
  303 + ->whereNull('deleted_at')
  304 + ->pluck('route','id')->toArray();
  305 +
  306 + //已发布单页面
  307 + $data['urls_page'] = [];
  308 +// $data['urls_page'] = DB::connection('custom_mysql')->table('gl_web_custom_template')
  309 +// ->where('project_id', $project_id)->where('url', '<>', '404')->where('status', BCustomTemplate::STATUS_ACTIVE)->pluck('url', 'id')->toArray();
  310 +
  311 + //已发布产品详情页
  312 + $data['urls_details'] = DB::connection('custom_mysql')->table('gl_product')
  313 + ->where('project_id', $project_id)->where('status', Product::STATUS_ON)
  314 + ->whereNull('deleted_at')
  315 + ->pluck('route', 'id')->toArray();
  316 +
  317 + $data['urls_cats'] = array_merge($data['urls_cats'], $data['urls_page']);
  318 + if(empty($data['urls_cats'])){
  319 + $data['urls_cats'] = $data['urls_details'];
  320 + }
  321 + DB::disconnect('custom_mysql');
  322 + return $data;
  323 + }
  324 +
  325 + /**
  326 + * 获取地区IP
  327 + */
  328 + protected function getIpAreas($project_ids, $time)
  329 + {
  330 + //本地时间为7-23点的地区
  331 + $h = date('H', strtotime($time));
  332 + $areas = [];
  333 + $list = DB::table('gl_area_timezone')->get();
  334 + $time_zones = [];
  335 + foreach ($list as $v) {
  336 + $v = (array)$v;
  337 + $country_hour = $h + $v['diff'];
  338 + if ($country_hour < 0) {
  339 + $country_hour = 24 + $country_hour;
  340 + }
  341 + if ($country_hour >= 7 && $country_hour < 23) {
  342 + $areas[] = $v['name'];
  343 + $time_zones[$v['name']] = $v['diff'];
  344 + }
  345 + }
  346 + $data = [];
  347 + foreach ($project_ids as $project_id){
  348 + //引流配置
  349 + $config = WebTrafficConfig::getCacheInfoByProjectId($project_id);
  350 + $main_countries = !empty($config->main_countries) ? explode(',',$config->main_countries) : [];
  351 + $filter_countries = !empty($config->filter_countries) ? explode(',',$config->filter_countries) : [];
  352 +
  353 + //根据地区随机取该地区的IP
  354 + $ipdata = DB::table('gl_xunpan_ipdata')->whereIn('ip_area', $areas)
  355 + ->where(function ($query) use ($main_countries, $filter_countries){
  356 + if($main_countries){
  357 + $query->whereIn('ip_area', $main_countries);
  358 + }
  359 + if($filter_countries){
  360 + $query->whereNotIn('ip_area', $main_countries);
  361 + }
  362 + })->inRandomOrder()->first();
  363 + if(!$ipdata){
  364 + continue;
  365 + }
  366 + $ipdata = (array)$ipdata ?: [];
  367 + $ipdata['diff'] = $time_zones[$ipdata['ip_area']];
  368 + $data[] = $ipdata;
  369 + }
  370 + return $data;
  371 + }
  372 +
  373 + /**
  374 + * 概率算法
  375 + */
  376 + protected function get_rand($proArr) {
  377 + $result = '';
  378 + $proSum = array_sum($proArr);
  379 + foreach ($proArr as $key => $proCur) {
  380 + $randNum = mt_rand(1, $proSum);
  381 + if ($randNum <= $proCur) {
  382 + $result = $key;
  383 + break;
  384 + } else {
  385 + $proSum -= $proCur;
  386 + }
  387 + }
  388 + unset ($proArr);
  389 + return $result;
  390 + }
  391 +
  392 + /**
  393 + * 根据随机访问深度 随机获取访问页面
  394 + */
  395 + protected function getVisitUrls($project_urls){
  396 + //没有分类页 就只访问首页
  397 + if(!$project_urls['urls_cats']){
  398 + $url[] = $project_urls['home'];
  399 + return $url;
  400 + }
  401 + //随机访问深度
  402 + $res_sdzb = $this->get_rand($this->sdzb);
  403 + //随机访问页面类型
  404 + $res_ymzb = $this->get_rand($this->ymzb);
  405 +
  406 + $all_url = array_merge($project_urls['urls_cats'],$project_urls['urls_details']);
  407 + if(!$all_url){
  408 + $url[] = $project_urls['home'];
  409 + return $url;
  410 + }
  411 +
  412 + $url = [];
  413 + if($res_sdzb == 0){//深度一页
  414 + $url[] = $project_urls[$res_ymzb] ? Arr::random($project_urls[$res_ymzb]) : '';
  415 + }elseif($res_sdzb == 1){//深度两页
  416 + $url[] = $project_urls['home'];
  417 + $url[] = $project_urls[$res_ymzb] ? Arr::random($project_urls[$res_ymzb]) : '';
  418 + }elseif($res_sdzb == 2){//深度3-6页
  419 + $yms = rand(2,5); //随机页面数
  420 + $url = Arr::random($all_url, $yms);
  421 + $url = Arr::prepend($url, $project_urls['home']);//首页加到最前面去
  422 + }elseif($res_sdzb == 3){//深度7-11页
  423 + $yms = rand(6,10); //随机页面数
  424 + $url = Arr::random($all_url, $yms);
  425 + $url = Arr::prepend($url, $project_urls['home']);//首页加到最前面去
  426 + }
  427 + foreach ($url as &$v){
  428 + if(!Str::contains($v, $project_urls['home'])){
  429 + if (FALSE === strpos($v, '.htm')) {
  430 + $v .= '/';
  431 + }
  432 + $v = $project_urls['home'] . $v;
  433 + }
  434 + }
  435 +
  436 + Log::channel('traffic')->info('project_id:访问深度' . $res_sdzb, $url);
  437 +
  438 + return array_unique(array_filter($url));
  439 + }
  440 +
  441 + /**
  442 + * 获取访问来路
  443 + */
  444 + protected function getReferer($ip_area, $lang){
  445 +
  446 + if($lang == 'ru'){
  447 + return $this->get_rand($this->eylyzb);
  448 + }
  449 +
  450 + if($ip_area == '美国'){
  451 + $referer = $this->get_rand($this->lyzb);
  452 + }else{
  453 + $referer = 'https://www.google.com/';
  454 +
  455 + $suffix = array_search($ip_area, $this->suffix);
  456 + if($suffix){
  457 + $res_qtzb = $this->get_rand($this->otherzb);
  458 + if($res_qtzb == 1){
  459 + $referer = 'https://www.google.'.$suffix.'/';
  460 + }
  461 + }
  462 + }
  463 + return $referer;
  464 + }
  465 +}