<?php
namespace app\api\middleware;
use app\api\middleware\Rsa;


class WxMiddleware {
    /*
* 中间件验证
*/

    public function handle($request, \Closure $next)
    {
        $rsa = new Rsa();
// 1、获取参数
        $data = $request->post();
        if(empty($data['signData'])){
            return json('非法请求！');
        }else{
            //        2.解参数
            $privDecrypt = $rsa->privDecrypt($data['signData']);
            $sign = substr($privDecrypt,0,36);
            $timestamp = substr($privDecrypt,36);
            if($sign!='DQEBAQUAA4GNADCBiQKBgQCk7WKdggwBOtte'){
                return  json('密钥错误！');
            }else{
                // 时间判断 大于3600s返回 系统错误
                $time_limit = 300;
//                $ttl= time();
                list($msec, $sec) = explode(' ', microtime());
                $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
                $msectimes = substr($msectime,0,13);
                if(abs((int)$timestamp - $msectimes) > $time_limit){
                    // var_dump($msectimes);
                    // var_dump($timestamp);
                    return  json('超时请求');
                }else{
                    return $next($request);
                }
            }
        }


//        return($publicDecrypt);

    }

}
