VisitController.php 4.2 KB
<?php

namespace App\Http\Controllers\Cside\Visit;

use App\Enums\Common\Code;
use App\Http\Controllers\Cside\BaseController;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class VisitController extends BaseController
{
    /**
     * 客户访问埋点接口
     */
    public function customerVisit(Request $request): \Illuminate\Http\JsonResponse
    {
        $data = $request->all();

        if ($this->filter($request)){
            $data = $this->visitInfoHandle($data);
            //异步处理
            if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_VISIT, $data)){
                $this->responseA([], 400, 'error');
            }
        }

        //埋点成功
        return response()->json([
            'code' => Code::SUCCESS_NUM,
            'msg' => '客户访问',
        ]);
    }

    public function filter($request){
        if($request->getClientIp() == "127.0.0.1"){
            return false;
        }
        //判断是否是爬虫
        $isReptile = $this->isReptile($request);
        if($isReptile){
            return false;
        }
        //是否允许测试环境
        $projectDomain = Project::getProjectByDomain($request->getHost());
        $project = Project::find($projectDomain['project_id']??0);
        if(empty($project)){
            return false;
        }
        // 测试环境返回信息
        if (FALSE !== strpos($request->getHost(), 'globalso.site') && !$project->is_record_test_visit) {
            return false;
        }
        return true;
    }

    /**
     * 埋点信息处理
     */
    public function visitInfoHandle($data)
    {
        //referrer
        if(preg_match('/google|facebook|bing|yahoo|youtobe|linkedin|messefrankfurt|yandex|tiktok|twitter|instagram|reddit|telegram|pinterest|tumblr/', $data['referrer_url'])){
        }else if($data['referrer_url'] == null){
            //直访用户
            $data['referrer_url'] = "";
        }else{
            $data['referrer_url'] = "https://www.google.com/";
        }
        return $data;
    }

    /**
     * 是否是爬虫访问
     */
    public function isReptile($request): bool
    {
        $agent = $request->header('User-Agent');
        if (!empty($agent)) {
            $spiderSite= array(
                "TencentTraveler",
                "Baiduspider+",
                "BaiduGame",
                "Googlebot",
                "msnbot",
                "Sosospider+",
                "Sogou web spider",
                "ia_archiver",
                "Yahoo! Slurp",
                "YoudaoBot",
                "Yahoo Slurp",
                "MSNBot",
                "Java (Often spam bot)",
                "BaiDuSpider",
                "Voila",
                "Yandex bot",
                "BSpider",
                "twiceler",
                "Sogou Spider",
                "Speedy Spider",
                "Google AdSense",
                "Heritrix",
                "Python-urllib",
                "Alexa (IA Archiver)",
                "Ask",
                "Exabot",
                "Custo",
                "OutfoxBot/YodaoBot",
                "yacy",
                "SurveyBot",
                "legs",
                "lwp-trivial",
                "Nutch",
                "StackRambler",
                "The web archive (IA Archiver)",
                "Perl tool",
                "MJ12bot",
                "Netcraft",
                "MSIECrawler",
                "WGet tools",
                "larbin",
                "Fish search",
                "yandex.com/bots",
                "google.com/bot",
                "bingbot",
                "YandexMobileBot",
                "BingPreview",
                "AhrefsBot",
                "bot"
            );
            $flag = 0;
            foreach($spiderSite as $val) {
                $str = strtolower($val);
                if (strpos($agent, $str) !== false) {
                    $flag = 1;
                }
            }
            if($flag == 1){
                return true;
            }else{
                return false;
            }
        } else {
            return false;
        }
    }
}