* @Date:2018-05-22 * @return [type] [description] */ protected function getStatusCode() { return $this->statusCode; } /** * [setStatusCode description] * @Author: * @Date:2018-05-22 * @param [type] $statusCode [description] */ protected function setStatusCode($statusCode) { $this->statusCode = $statusCode; return $this; } /** * 成功的返回 * @Author: * @Date:2018-05-22 * @param [type] $data [description] * @param string $status [description] * @return [type] [description] */ public function success($data = true, $status = "SUCCESS") { return $this->status($status, compact('data')); } /** * 200 返回消息 * @Author: * @Date:2018-05-22 * @param [type] $message [description] * @param string $status [description] * @return [type] [description] */ public function message($message, $status = "SUCCESS") { return $this->status($status, [ 'message' => $message, ]); } /** * 400 失败 * @Author: * @Date:2018-05-22 * @param [type] $message [description] * @param [type] $code [description] * @param string $status [description] * @return [type] [description] */ public function failed($message, $code = 4000, $status = 'ERROR') { return $this->setStatusCode($code)->message($message, $status); } /** * 404 错误 * @Author: * @Date:2018-05-22 * @param string $message [description] * @return [type] [description] */ public function notFond($message = 'Not Fond!') { return $this->failed($message, 4004); } /** * 500 错误 * @Author: * @Date:2018-05-22 * @param string $message [description] * @return [type] [description] */ public function internalError($message = "Internal Error!") { return $this->failed($message, 5000); } private function status($status, array $data, $code = null) { if ($code) { $this->setStatusCode($code); } $status = [ 'status' => $status, 'error_code' => $this->statusCode, ]; if ($this->statusCode == 200) { $data = $data->additional([ 'status' => $status, 'error_code' => $this->statusCode, ]); } else { $data = array_merge($status, $data); } return $this->respond($data); } private function respond($data, $header = []) { $rt = microtime(true) - LARAVEL_START; $header = array_merge($header, ['rt' => round($rt * 1000, 2) . 'ms', 'qps' => round(1 / $rt, 1)]); return Response::json($data, 200, $header); } }