<?php

namespace App\Http\Middleware;

use Closure;
use App\Services\UtilService;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $isWx = UtilService::getIsWx($_SERVER['HTTP_USER_AGENT']);
        if(!$isWx) return redirect(route('wxIndex'));


        if (!$request->session()->get("uid")) {
            if ($request->expectsJson()) {
                $result = [];
                $result['error'] = 1019;
                $result['message'] = "登陆失效或未登陆，请重新登陆";
                $result['data'] = null;
                return response()->json($result);
            }else {
                return redirect(route("login"));
            }
        }
        return $next($request);
    }
}
