catchOptions()) { die("this is option"); } //启用调试模式 } protected function catchOptions() { $request = request(); return $request->method() == "OPTIONS"; } //===end 处理跨域==== /** * 成功返回内容 * @param $data * @param string $msg * @param int $code */ public function success($data,$msg="",$code=0) { if($msg==""){ $msg=ErrorCode::getError($code); } $response = returnFormat($code, $msg, $data); Log::record(json_encode($response, JSON_UNESCAPED_UNICODE), "response"); throw new HttpResponseException(Response::create($response, "json")); } /** * 失败返回内容 * @param int $code * @param null $data */ public function error($code = 9999,$msg="", $data = null) { if($msg==""){ $msg=ErrorCode::getError($code); } if ($data === null) { $data = new \stdClass(); } $response = returnFormat($code, $msg, $data); Log::record(json_encode($response, JSON_UNESCAPED_UNICODE), "response"); throw new HttpResponseException(Response::create($response, "json")); } /** * 自动校验 * @param $class * @param $param */ protected function autoValid($class, $param) { try { validate($class)->check($param); } catch (ValidateException $e) { // 验证失败 输出错误信息 return returnFormat(999,$e->getError()); //$this->error($e->getError()); } return returnFormat(0); } /** * 接口签名校验 */ protected function checkApiSign() { $param = request()->param(); $validRes= $this->autoValid([ "_timestamp" => "require", "_sign" => "require", ], $param); if($validRes['code']!==0){ return $validRes; } $effectMinute=2;//有效时间,分钟数 if (!($param["_timestamp"] >= time() - $effectMinute * 60 && $param["_timestamp"] <= time() + $effectMinute * 60)) { return returnFormat(9004); } $sign = $param["_sign"]; unset($param["_sign"]);//删除原签名字段,用来重新生成签名 ksort($param); $secret = config("common.api_sign_secret"); $sign2 = md5(json_encode($param, JSON_UNESCAPED_UNICODE) . $secret); if ($sign !== $sign2) { return returnFormat(9002," $secret 原签名:$sign 新签名:$sign2 "); } return returnFormat(0); } }