<?php

namespace App\Http\Middleware;

use Closure;
use DB;
class ChannelOperateRecord
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $param = '';
        $param_arr = $request->except('_url');
        if($param_arr){
            $param = http_build_query($param_arr);
        }
        $url = $request->url();
        try{
            $distribution_channel_id = $this->getChannelId();
            DB::table('channel_operate_record')->insert([
                'distribution_channel_id'=>$distribution_channel_id,
                'url'=>$url,
                'param'=>$param,
                'created_at'=>date('Y-m-d H:i:s'),
                'updated_at'=>date('Y-m-d H:i:s'),
            ]);
        }catch (\Exception $e){

        }
        return $next($request);
    }

    //获取分销渠道ID
    function getChannelId() {
        if(empty(session('ydychannel'))) {
            if(env('APP_ENV') =='local')
                return 1;
            else
                return 0;
        }
        $distribution_channel = unserialize(session('ydychannel'));
        return $distribution_channel->id;
    }
}
