<?php

namespace App\Http\Middleware;

use App\Consts\SysConsts;
use Closure;

class CheckSign
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $sign      = $request->post('sign', '');
        $timestamp = $request->post('timestamp', 0);
        $key       = 'a!A&AFRWT65Nb3NlklezUiqHyQAA@Z8M';
        $params    = $request->except('_url');
        if ($timestamp && time() - $timestamp <= (SysConsts::ONE_HOUR_SECONDS * 10) && $sign == _sign($params, $key)) {
            return $next($request);
        } else {
            return response()->error('QAPP_SIGN_ERROR');
        }
    }
}
