checkApiSign(); if($this->checkTokenOpen){ $this->checkToken(); } } /** * 获取token * @return array|mixed|string|null */ protected function getToken(){ $token=null; if(!$token){ //from header $token=request()->header("token"); } if(!$token){ //from url $token=input("token"); } return $token; } /** * 检测token * token规则 * token由base64编码,解码后分为密文、主键、过期时间(时间戳)三部分,用竖线|隔开 */ public function checkToken() { $token=$this->getToken(); if(!$token){ $this->error(ErrorCode::getError(13),13); } $tokerReal=base64_decode($token); $tokenArr=explode("|",$tokerReal);//拆分token if(count($tokenArr)!=3){ $this->error(ErrorCode::getError(14),14); } //判断token有没有超时 if(time()>$tokenArr[2]){ $this->error(ErrorCode::getError(11),11); } //以下部分根据自己的业务实现 } /** * 返回成功信息 * @param $data * @param string $msg */ public function success($data,$msg=""){ $res=returnFormat(0,$msg,$data); Log::record("response:" . mb_substr(json_encode($res, JSON_UNESCAPED_UNICODE), 0, 1000) . ",code:0" , "debug"); throw new HttpResponseException(Response::create($res,"json")); } /** * 简易错误提示 * @param $code */ public function errorSimple($code){ $this->error(ErrorCode::getError($code),$code); } /** * 返回失败信息 * @param $msg * @param int $code * @param array $data */ public function error($msg,$code=999,$data=[]){ $res=returnFormat($code,$msg,$data); Log::record("response:" . mb_substr(json_encode($res, JSON_UNESCAPED_UNICODE), 0, 1000) . ",code:" . $code, "debug"); throw new HttpResponseException(Response::create($res,"json")); } /** * 自动校验 * @param $class * @param $param */ public function autoValid($class,$param){ try { validate($class)->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 $this->error($e->getError()); } } /** * 检查签名 */ public function checkApiSign(){ $timestampLimit=20; $param = request()->param(); $this->autoValid([ "_timestamp"=>"require", "_sign"=>"require", ], $param); if(!($param["_timestamp"]>=time()-$timestampLimit*60 && $param["_timestamp"]<=time()+$timestampLimit*60)){ $this->error("时间戳不合法,请刷新"); } $sign=$param["_sign"]; unset($param["_sign"]); ksort($param); $param['_timestamp'] = $param['_timestamp']; $secret=config("common.api_sign_secret"); $signStr=stripslashes(json_encode($param,JSON_UNESCAPED_UNICODE).$secret); $sign2=md5($signStr); if($sign!==$sign2){ $this->error("签名错误。".$signStr); } } }