<?php
/**
 * Created by PhpStorm.
 * User: blade
 * Date: 2018/2/1
 * Time: 16:52
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Illuminate\Support\Facades\DB;

class TokenController extends Controller
{

    /**
     * @param $ver_data 含时间的数据数组
     * @param $ver_sign 传入验证字符串
     * @param $token 后台token
     * @return int
     */
      function check($ver_data,$ver_sign,$token){
        $res=$this->checkTime($ver_data['time']);
        if($res<0){
            return $res;
        }
        return $this->checkToken($ver_data,$ver_sign,$token);
    }


    /**
     * @param $ver_data 数据
     * @param $sign 传入的校验串
     * @param $token 正确的token
     * @return int
     */
    private function checkToken($ver_data,$sign,$token){
        krsort($ver_data);
        $right_sign=md5(http_build_query($ver_data).'&token='.$token);
        if($right_sign!=$sign){
            return -3020035024;//校验失败
        }
        return 1;
    }
    //时间检验
    private function checkTime($time){
        //时间检验
        if($time<(time()-10)){
            return -3020035023;//请求超时
        }
        return 1;
    }
}