<?php


namespace App\Http\Middleware;

use Closure;
use App\Http\helper\ApiSign as Sign;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class NewApiSign
{
    /**
     * 验证签名 API接口
     * @param \Illuminate\Http\Request $request
     * @param Closure $next
     */
    public function handle($request, Closure $next)
    {

        $params = $request->all();



        $sign = $params['sign'] ?? '';

        unset($params['sign']);

        $apiSign = Sign::newSign($params);

        if (!empty($params) && $sign != $apiSign) {
            // 生成用户请求信息日志
            if (is_array($params)) {
                $params['sign'] =$sign;
                $request_extra = \GuzzleHttp\json_encode($params);
            } else {
                $request_extra = '';
            }
            Log::info('new 服务端-客户端-所有请求信息 ' . $request_extra);
            Log::info('new 服务端签名信息-签名失败：' . $apiSign);
            return apiReturnFail('Payment error_1');
        }

        $rand_str = $params['rand_str'] ?? '';
//        Redis::select(2);
//        $redis = Redis::setnx($rand_str, $rand_str);
//        if (!$redis) {
//            return apiReturnSuc('Payment error_8_timeOut');
//        }
        return $next($request);

    }
}
