<?php
namespace App\Http\Middleware;

use Closure;
use App\Models\Setting;

class CheckToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$this->checkToken()) {
            return response()->json(['status' => 'error', 'message' => '您没有权限访问该页面']);
        }

        return $next($request);
    }
    
    private function checkToken()
    {
        $appToken = Setting::get('apptoken');
        $token    = request()->header('token');
        
        return $token == $appToken;
    }
}
