TrafficController.php 1.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2024/1/6
 * Time: 16:06
 */
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Repositories\SyncSubmitRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * Class TrafficController
 * @package App\Http\Controllers\Api
 */
class TrafficController extends Controller
{
    /**
     * 特殊访问引流接口
     * @param Request $request
     * @return string
     */
    public function trafficVisit(Request $request)
    {
        //获取参数
        $data = $request->all();
        $ip = $request->input('ip');
        $url = $request->input('url');
        $device_port = intval($request->input('device_port'));
        $referrer_url = $request->input('referrer_url');
        $user_agent = $request->input('user_agent');

        // FIXME 些日志 观察数据, 完成以后删除代码
        file_put_contents(storage_path('logs/trafficVisit' . date('Y-m-d') . '.log'), var_export($data, true));
        //验证参数
        $validator = Validator::make($data, [
            'ip' => 'required',
            'url' => 'required',
            'user_agent' => 'required',
        ], [
            'ip.required' => 'IP不能为空',
            'url.required' => '访问链接不能为空',
            'user_agent.required' => '请求头信息user_agent不能为空',
        ]);
        //判断参数是否正确
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $result = app(SyncSubmitRepository::class)->trafficVisit($ip, $url, $user_agent, $referrer_url, $device_port, 1);
        return $result ? $this->success() : $this->error();
    }


}