作者 Your Name
  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 + ProjectServer::useProject($project['project_id']);
  179 + if($project['project_id'] <= 135){
  180 + continue;
  181 + }
  182 + echo 'project_id:' . $project['project_id'] . PHP_EOL;
  183 + 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){
  186 + continue;
  187 + }
  188 +
  189 + $randomTime = [];
  190 + for ($i=0;$i<$need_num-$ip_num;$i++){
  191 + $randomTime[] = Carbon::make($date)->addSeconds(rand(0, 86400))->toDateTimeString();
  192 + }
  193 + sort($randomTime);
  194 + echo 'count:' . count($randomTime) . PHP_EOL;
  195 + $project_urls = $this->getProductUrls($project['project_id']);
  196 + $project_urls['home'] = $project['domain'];
  197 +
  198 + foreach ($randomTime as $time){
  199 + //随机引流间隔
  200 + $res_sjjg = $this->get_rand($this->sjjg);
  201 + if ($res_sjjg == 1) {
  202 + continue;
  203 + }
  204 +
  205 + //随机访问页面
  206 + $project['visit_urls'] = $this->getVisitUrls($project_urls);
  207 + //随机客户端
  208 + $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);
  210 +
  211 + $project['ip'] = $this->getIpAreas([$project['project_id']], $time)[0] ?? '';
  212 +
  213 + foreach ($project['visit_urls'] as $url){
  214 + $time = Carbon::make($time)->addSeconds(rand(2, 15))->toDateTimeString();
  215 + $url_array = parse_url($project['domain']);
  216 + $referrer_url = $this->getReferer($project['ip']['ip_area'], $project['lang']);
  217 + $array = [
  218 + 'ip' => $project['ip']['ip'],
  219 + 'domain' => $url_array['host'] ?? '',
  220 + 'referer' => $referrer_url,
  221 + 'user_agent' => $project['user_agent'],
  222 + 'data' => [
  223 + 'url' => $url,
  224 + 'domain' => empty($url_array['host']) ? '' : $url_array['scheme'] . '://' . $url_array['host'],
  225 + 'device_port' => in_array($project['device_port'], array_keys(Visit::deviceMap())) ? $project['device_port'] : Visit::DEVICE_PC,
  226 + 'referrer_url' => $referrer_url
  227 + ],
  228 + ];
  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();
  236 + }
  237 + }
  238 + }
  239 + Log::info('web_traffic_fix finish');
  240 + }catch (\Exception $e){
  241 + dump($e->getMessage());
  242 + Log::info('web_traffic_fix error:' . $e->getMessage());
  243 + }
  244 + }
  245 +
  246 + /**
  247 + * 非俄语站的引流的项目
  248 + */
  249 + protected function getProjectList($type){
  250 + $ru_lang_id = WebLanguage::getIdByLang('ru');
  251 +
  252 + //推广项目
  253 + $list = Project::with('domainInfo')
  254 + ->leftJoin('gl_project_deploy_optimize as pdo', 'pdo.project_id', '=', 'gl_project.id')
  255 + ->leftJoin('gl_project_online_check as poc', 'poc.project_id', '=', 'gl_project.id')
  256 + ->where('pdo.domain', '>', 0)
  257 + ->where('poc.qa_status', OnlineCheck::STATUS_ONLINE_TRUE)
  258 + ->whereIn('gl_project.type', [Project::TYPE_TWO, Project::TYPE_FOUR])
  259 + ->where('gl_project.is_upgrade', 0) //非升级项目
  260 + ->where('gl_project.main_lang_id', '<>', $ru_lang_id) //非俄语站
  261 + ->where(function ($query) use ($type) {
  262 + if($type == 1){
  263 + //1-3个月项目
  264 + $startTime = Carbon::now()->addMonths(-4)->toDateString();
  265 + $endTime = Carbon::now()->addMonths(-1)->toDateString();
  266 + $query->whereBetween('pdo.start_date', [$startTime,$endTime]);
  267 + }elseif($type == 2){
  268 + //4-8个月项目
  269 + $startTime = Carbon::now()->addMonths(-9)->startOfDay()->toDateTimeString();
  270 + $endTime = Carbon::now()->addMonths(-4)->endOfDay()->toDateTimeString();
  271 + $query->whereBetween('pdo.start_date', [$startTime,$endTime]);
  272 + }else{
  273 + //大于9个月项目
  274 + $startTime = Carbon::now()->addMonths(-9)->startOfDay()->toDateTimeString();
  275 + $query->where('pdo.start_date', '<', $startTime);
  276 + }
  277 + })->select(['pdo.project_id','gl_project.main_lang_id','gl_project.id'])
  278 + ->orderBy('project_id')
  279 + ->get();
  280 + //其他地方在引流的域名
  281 + $other = DB::connection('projects_mysql')->table('projects')->where('switch', 1)->pluck('domain')->toArray();
  282 + $data = [];
  283 + foreach ($list as $project) {
  284 + $lang = WebLanguage::getLangById($project['main_lang_id']??1)['short'];
  285 + if(empty($project->domainInfo['domain'])){
  286 + continue;
  287 + }
  288 + //其他地方在引流就不再引流了
  289 + if(in_array($project->domainInfo['domain'], $other)){
  290 + continue;
  291 + }
  292 + $data[] = [
  293 + 'project_id' => $project['project_id'],
  294 + 'domain' => 'https://' . $project->domainInfo['domain'] . '/',
  295 + 'lang' => $lang
  296 + ];
  297 + }
  298 + return $data;
  299 + }
  300 +
  301 + /**
  302 + * 获取产品分类、单页和详情链接
  303 + */
  304 + protected function getProductUrls($project_id){
  305 + //已发布产品分类页面
  306 + $data['urls_cats'] = DB::connection('custom_mysql')->table('gl_product_category')
  307 + ->where('project_id', $project_id)->where('status', Category::STATUS_ACTIVE)
  308 + ->whereNull('deleted_at')
  309 + ->pluck('route','id')->toArray();
  310 +
  311 + //已发布单页面
  312 + $data['urls_page'] = [];
  313 +// $data['urls_page'] = DB::connection('custom_mysql')->table('gl_web_custom_template')
  314 +// ->where('project_id', $project_id)->where('url', '<>', '404')->where('status', BCustomTemplate::STATUS_ACTIVE)->pluck('url', 'id')->toArray();
  315 +
  316 + //已发布产品详情页
  317 + $data['urls_details'] = DB::connection('custom_mysql')->table('gl_product')
  318 + ->where('project_id', $project_id)->where('status', Product::STATUS_ON)
  319 + ->whereNull('deleted_at')
  320 + ->pluck('route', 'id')->toArray();
  321 +
  322 + $data['urls_cats'] = array_merge($data['urls_cats'], $data['urls_page']);
  323 + if(empty($data['urls_cats'])){
  324 + $data['urls_cats'] = $data['urls_details'];
  325 + }
  326 + DB::disconnect('custom_mysql');
  327 + return $data;
  328 + }
  329 +
  330 + /**
  331 + * 获取地区IP
  332 + */
  333 + protected function getIpAreas($project_ids, $time)
  334 + {
  335 + //本地时间为7-23点的地区
  336 + $h = date('H', strtotime($time));
  337 + $areas = [];
  338 + $list = DB::table('gl_area_timezone')->get();
  339 + $time_zones = [];
  340 + foreach ($list as $v) {
  341 + $v = (array)$v;
  342 + $country_hour = $h + $v['diff'];
  343 + if ($country_hour < 0) {
  344 + $country_hour = 24 + $country_hour;
  345 + }
  346 + if ($country_hour >= 7 && $country_hour < 23) {
  347 + $areas[] = $v['name'];
  348 + $time_zones[$v['name']] = $v['diff'];
  349 + }
  350 + }
  351 + $data = [];
  352 + foreach ($project_ids as $project_id){
  353 + //引流配置
  354 + $config = WebTrafficConfig::getCacheInfoByProjectId($project_id);
  355 + $main_countries = !empty($config->main_countries) ? explode(',',$config->main_countries) : [];
  356 + $filter_countries = !empty($config->filter_countries) ? explode(',',$config->filter_countries) : [];
  357 +
  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){
  362 + $query->whereIn('ip_area', $main_countries);
  363 + }
  364 + if($filter_countries){
  365 + $query->whereNotIn('ip_area', $main_countries);
  366 + }
  367 + })->inRandomOrder()->first();
  368 + if(!$ipdata){
  369 + continue;
  370 + }
  371 + $ipdata = (array)$ipdata ?: [];
  372 + $ipdata['diff'] = $time_zones[$ipdata['ip_area']];
  373 + $data[] = $ipdata;
  374 + }
  375 + return $data;
  376 + }
  377 +
  378 + /**
  379 + * 概率算法
  380 + */
  381 + protected function get_rand($proArr) {
  382 + $result = '';
  383 + $proSum = array_sum($proArr);
  384 + foreach ($proArr as $key => $proCur) {
  385 + $randNum = mt_rand(1, $proSum);
  386 + if ($randNum <= $proCur) {
  387 + $result = $key;
  388 + break;
  389 + } else {
  390 + $proSum -= $proCur;
  391 + }
  392 + }
  393 + unset ($proArr);
  394 + return $result;
  395 + }
  396 +
  397 + /**
  398 + * 根据随机访问深度 随机获取访问页面
  399 + */
  400 + protected function getVisitUrls($project_urls){
  401 + //没有分类页 就只访问首页
  402 + if(!$project_urls['urls_cats']){
  403 + $url[] = $project_urls['home'];
  404 + return $url;
  405 + }
  406 + //随机访问深度
  407 + $res_sdzb = $this->get_rand($this->sdzb);
  408 + //随机访问页面类型
  409 + $res_ymzb = $this->get_rand($this->ymzb);
  410 +
  411 + $all_url = array_merge($project_urls['urls_cats'],$project_urls['urls_details']);
  412 + if(!$all_url){
  413 + $url[] = $project_urls['home'];
  414 + return $url;
  415 + }
  416 +
  417 + $url = [];
  418 + if($res_sdzb == 0){//深度一页
  419 + $url[] = $project_urls[$res_ymzb] ? Arr::random($project_urls[$res_ymzb]) : '';
  420 + }elseif($res_sdzb == 1){//深度两页
  421 + $url[] = $project_urls['home'];
  422 + $url[] = $project_urls[$res_ymzb] ? Arr::random($project_urls[$res_ymzb]) : '';
  423 + }elseif($res_sdzb == 2){//深度3-6页
  424 + $yms = rand(2,5); //随机页面数
  425 + $url = Arr::random($all_url, $yms);
  426 + $url = Arr::prepend($url, $project_urls['home']);//首页加到最前面去
  427 + }elseif($res_sdzb == 3){//深度7-11页
  428 + $yms = rand(6,10); //随机页面数
  429 + $url = Arr::random($all_url, $yms);
  430 + $url = Arr::prepend($url, $project_urls['home']);//首页加到最前面去
  431 + }
  432 + foreach ($url as &$v){
  433 + if(!Str::contains($v, $project_urls['home'])){
  434 + if (FALSE === strpos($v, '.htm')) {
  435 + $v .= '/';
  436 + }
  437 + $v = $project_urls['home'] . $v;
  438 + }
  439 + }
  440 +
  441 + Log::channel('traffic')->info('project_id:访问深度' . $res_sdzb, $url);
  442 +
  443 + return array_unique(array_filter($url));
  444 + }
  445 +
  446 + /**
  447 + * 获取访问来路
  448 + */
  449 + protected function getReferer($ip_area, $lang){
  450 +
  451 + if($lang == 'ru'){
  452 + return $this->get_rand($this->eylyzb);
  453 + }
  454 +
  455 + if($ip_area == '美国'){
  456 + $referer = $this->get_rand($this->lyzb);
  457 + }else{
  458 + $referer = 'https://www.google.com/';
  459 +
  460 + $suffix = array_search($ip_area, $this->suffix);
  461 + if($suffix){
  462 + $res_qtzb = $this->get_rand($this->otherzb);
  463 + if($res_qtzb == 1){
  464 + $referer = 'https://www.google.'.$suffix.'/';
  465 + }
  466 + }
  467 + }
  468 + return $referer;
  469 + }
  470 +}