Skip to content

Commit

Permalink
fix:response format
Browse files Browse the repository at this point in the history
  • Loading branch information
liufee committed Aug 16, 2020
1 parent 5737015 commit 9527a84
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions backend/actions/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use stdClass;
use backend\actions\helpers\Helper;
use yii\base\Exception;
use yii\web\Response;
use yii\web\UnprocessableEntityHttpException;

/**
Expand Down Expand Up @@ -103,6 +104,7 @@ public function run()
$createResult = call_user_func_array($this->doCreate, $createData);//call doCreate closure

if (Yii::$app->getRequest()->getIsAjax()) { //ajax
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
if ($createResult === true) {//only $createResult is true represent create success
return ['code' => 0, 'msg' => 'success', 'data' => new stdClass()];
} else {
Expand Down
3 changes: 3 additions & 0 deletions backend/actions/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use yii\base\Exception;
use yii\web\BadRequestHttpException;
use yii\web\MethodNotAllowedHttpException;
use yii\web\Response;
use yii\web\UnprocessableEntityHttpException;

/**
Expand Down Expand Up @@ -77,12 +78,14 @@ public function run()

if (count($errors) == 0) {
if( Yii::$app->getRequest()->getIsAjax() ) {
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
return ['code'=>0, 'msg'=>'success', 'data'=>new stdClass()];
}else {
return $this->controller->redirect(Yii::$app->getRequest()->getReferrer());
}
} else {
if( Yii::$app->getRequest()->getIsAjax() ){
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
throw new UnprocessableEntityHttpException(implode("<br>", $errors));
}else {
Yii::$app->getSession()->setFlash('error', implode("<br>", $errors));
Expand Down
2 changes: 2 additions & 0 deletions backend/actions/DoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use stdClass;
use backend\actions\helpers\Helper;
use yii\base\Exception;
use yii\web\Response;
use yii\web\UnprocessableEntityHttpException;

/**
Expand Down Expand Up @@ -91,6 +92,7 @@ public function run()

if (Yii::$app->getRequest()->getIsAjax()) { //ajax
if ($doResult === true) {//only $doResult is true represent create success
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
return ['code' => 0, 'msg' => 'success', 'data' => new stdClass()];
} else {//not ajax
throw new UnprocessableEntityHttpException(Helper::getErrorString($doResult));
Expand Down
2 changes: 2 additions & 0 deletions backend/actions/SortAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use yii\base\Exception;
use yii\base\InvalidArgumentException;
use yii\web\MethodNotAllowedHttpException;
use yii\web\Response;
use yii\web\UnprocessableEntityHttpException;

/**
Expand Down Expand Up @@ -72,6 +73,7 @@ public function run()
$result = call_user_func_array($this->doSort, [$condition, $value, $this]);

if (Yii::$app->getRequest()->getIsAjax()) {
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
if( $result === true ){
return ['code'=>0, 'msg'=>'success', 'data'=>new stdClass()];
}else{
Expand Down
2 changes: 2 additions & 0 deletions backend/actions/UpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Closure;
use backend\actions\helpers\Helper;
use yii\base\Exception;
use yii\web\Response;
use yii\web\UnprocessableEntityHttpException;

/**
Expand Down Expand Up @@ -108,6 +109,7 @@ public function run()
$updateResult = call_user_func_array($this->doUpdate, $updateData);//call doUpdate closure

if( Yii::$app->getRequest()->getIsAjax() ){ //ajax
Yii::$app->getResponse()->format = Response::FORMAT_JSON;
if( $updateResult === true ){//only $updateResult is true represent update success
return ['code'=>0, 'msg'=>'success', 'data'=>new stdClass()];
}else{
Expand Down
10 changes: 7 additions & 3 deletions backend/widgets/ueditor/UeditorAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,16 @@ protected function manage($allowFiles, $listSize, $path)
/* 获取文件列表 */
$path = Yii::getAlias('@ueditor') . (substr($path, 0, 1) == '/' ? '' : '/') . $path;
$files = $this->getFiles($path, $allowFiles);
if (! count($files)) {
$len = 0;
if( $files !== null ){
$len = count($files);
}
if ( $len == 0 ) {
$result = [
'state' => 'no match file',
'list' => [],
'start' => $start,
'total' => count($files),
'total' => $len,
];
return $result;
}
Expand All @@ -473,7 +477,7 @@ protected function manage($allowFiles, $listSize, $path)
'state' => 'SUCCESS',
'list' => $list,
'start' => $start,
'total' => count($files),
'total' => $len,
];
return $result;
}
Expand Down

0 comments on commit 9527a84

Please sign in to comment.