优化error和success方法

This commit is contained in:
augushong
2020-12-06 22:12:43 +08:00
parent 542bcc3e22
commit 0b7dbe7163

View File

@@ -8,7 +8,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
declare (strict_types = 1); declare(strict_types=1);
namespace app; namespace app;
@@ -64,7 +64,8 @@ abstract class BaseController
// 初始化 // 初始化
protected function initialize() protected function initialize()
{} {
}
/** /**
* 验证数据 * 验证数据
@@ -103,68 +104,70 @@ abstract class BaseController
return $v->failException(true)->check($data); return $v->failException(true)->check($data);
} }
public function success($msg = '操作成功',$jump_to_url = null,$code = 200,$params = []) public function success($msg = '操作成功', $jump_to_url = null, $code = 200, $params = [])
{ {
if(is_null($jump_to_url)){ if (is_null($jump_to_url)) {
$jump_to_url = \request()->server('HTTP_REFERER'); $jump_to_url = \request()->server('HTTP_REFERER');
}else{ } else {
if($jump_to_url instanceof Url){ if ($jump_to_url instanceof Url) {
$jump_to_url = $jump_to_url; $jump_to_url = (string)$jump_to_url;
}else{ } else {
if (strpos($jump_to_url, 'http') !== 0) {
$jump_to_url = url($jump_to_url); $jump_to_url = url($jump_to_url);
} }
} }
}
$data = [ $data = [
'msg'=>$msg, 'msg' => $msg,
'jump_to_url'=>$jump_to_url, 'jump_to_url' => $jump_to_url,
'params'=>$params 'params' => $params
]; ];
if(\request()->isAjax()){ if (\request()->isAjax()) {
$data['jump_to_url'] = (string)$jump_to_url; $data['jump_to_url'] = $jump_to_url;
if($code == 200){ if ($code == 200) {
$code = 0; $code = 0;
} }
throw new HttpResponseException(json_message($data,$code,$msg)); throw new HttpResponseException(json_message($data, $code, $msg));
} }
View::assign($data); View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/success'),$code)); throw new HttpResponseException(response(View::fetch('common@tpl/success'), $code));
} }
public function error($msg = '操作失败',$jump_to_url = null,$code = 200,$params = []) public function error($msg = '操作失败', $jump_to_url = null, $code = 200, $params = [])
{ {
if(is_null($jump_to_url)){ if (is_null($jump_to_url)) {
$jump_to_url = \request()->server('HTTP_REFERER'); $jump_to_url = \request()->server('HTTP_REFERER');
}else{ } else {
if($jump_to_url instanceof Url){ if ($jump_to_url instanceof Url) {
$jump_to_url = $jump_to_url; $jump_to_url = (string)$jump_to_url;
}else{ } else {
if (strpos($jump_to_url, 'http') !== 0) {
$jump_to_url = url($jump_to_url); $jump_to_url = url($jump_to_url);
}
} }
} }
$data = [ $data = [
'msg'=>$msg, 'msg' => $msg,
'jump_to_url'=>$jump_to_url, 'jump_to_url' => $jump_to_url,
'params'=>$params 'params' => $params
]; ];
if(\request()->isAjax()){ if (\request()->isAjax()) {
$data['jump_to_url'] = (string)$jump_to_url; $data['jump_to_url'] = $jump_to_url;
if($code == 200){ if ($code == 200) {
$code = 500; $code = 500;
} }
throw new HttpResponseException(json_message($data,$code,$msg)); throw new HttpResponseException(json_message($data, $code, $msg));
} }
View::assign($data); View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/error'),$code)); throw new HttpResponseException(response(View::fetch('common@tpl/error'), $code));
} }
} }