<?php
 namespace TechOnline\Laravel\Http; use Illuminate\Http\JsonResponse; use TechOnline\Laravel\Exception\BizException; class Response { public static function tryGetData($ret) { if ($ret instanceof JsonResponse) { $ret = $ret->getData(true); } if (!isset($ret['code'])) { BizException::throws('ERROR_RESPONSE'); } if ($ret['code']) { BizException::throws($ret['msg']); } return $ret['data']; } public static function isSuccess($result) { return (isset($result['code']) && 0 === $result['code']); } public static function isError($result) { return !self::isSuccess($result); } public static function generate($code, $msg, $data = null) { $response = [ 'code' => $code, 'msg' => $msg, 'data' => $data, ]; if (null === $data) { unset($response['data']); } return $response; } public static function generateSuccessData($data) { return self::generate(0, 'ok', $data); } public static function generateSuccess($msg = 'ok') { return self::generate(0, $msg); } public static function generateError($msg = 'error') { return self::generate(-1, $msg); } public static function jsonSuccessData($data) { return self::json(0, 'ok', $data); } public static function jsonSuccess($msg = 'ok') { return self::json(0, $msg); } public static function jsonError($msg = 'error') { return self::json(-1, $msg); } public static function json($code, $msg, $data = null, $redirect = null) { $response = [ 'code' => $code, 'msg' => $msg, 'data' => $data, 'redirect' => $redirect, ]; if (null === $redirect) { unset($response['redirect']); } return \Illuminate\Support\Facades\Response::json($response); } public static function jsonFromGenerate($ret) { if ($ret['code']) { return self::json($ret['code'], $ret['msg']); } if (!isset($ret['msg'])) { $ret['msg'] = null; } if (isset($ret['data'])) { return self::json($ret['code'], $ret['msg'], $ret['data']); } return self::json($ret['code'], $ret['msg']); } public static function jsonRaw($data) { return \Illuminate\Support\Facades\Response::json($data); } public static function jsonp($data, $callback = null) { if (empty($callback)) { $callback = \Illuminate\Support\Facades\Input::get('callback', null); } if (empty($callback)) { return \Illuminate\Support\Facades\Response::json($data); } return \Illuminate\Support\Facades\Response::jsonp($callback, $data); } public static function sendFromResponse($ret, $msg = 'ok', $data = null, $redirect = null) { if ($ret['code']) { return self::send($ret['code'], $ret['msg'], empty($ret['data']) ? null : $ret['data'], empty($ret['redirect']) ? null : $ret['redirect']); } return self::send(0, $msg, $data, $redirect); } public static function sendError($msg, $data = null, $redirect = null) { return self::send(-1, $msg, $data, $redirect); } public static function send($code, $msg, $data = null, $redirect = null) { if (\Illuminate\Support\Facades\Request::ajax()) { return self::json($code, $msg, $data, $redirect); } else { if (empty($msg) && $redirect) { return redirect($redirect); } $response = [ 'code' => $code, 'msg' => $msg, 'redirect' => $redirect, 'data' => $data ]; if (null === $redirect) { unset($response['redirect']); } return view('base::msg', $response); } } public static function download($filename, $content, $headers = []) { $response = new \Illuminate\Http\Response($content); $disposition = $response->headers->makeDisposition( \Symfony\Component\HttpFoundation\ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename ); $response->headers->set('Content-Disposition', $disposition); foreach ($headers as $k => $v) { $response->headers->set($k, $v); } return $response; } public static function raw($content, $headers = []) { $response = new \Illuminate\Http\Response($content); foreach ($headers as $k => $v) { $response->headers->set($k, $v); } return $response; } }