Skip to content

Commit

Permalink
修改上传结果响应
Browse files Browse the repository at this point in the history
  • Loading branch information
moxuandi committed Aug 6, 2019
1 parent be9e556 commit 4f800f2
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions UploaderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use yii\base\Action;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\Response;
use moxuandi\helpers\Uploader;

/**
Expand Down Expand Up @@ -32,8 +33,8 @@ public function init()
}

/**
* @throws \yii\base\ErrorException
* @throws \yii\base\Exception
* @throws yii\base\ErrorException
* @throws yii\base\Exception
*/
public function run()
{
Expand All @@ -51,10 +52,10 @@ public function run()
// 文件在线管理器
case 'listfile': $result = self::actionList(); break;
// 配置参数
case 'config': $result = Json::encode($this->config); break;
case 'config': $result = $this->config; break;
// 抓取远程图片
//case 'catchimage': $result = []; break;
default: $result = Json::encode(['state'=>'请求地址出错']); break;
default: $result = ['state'=>'请求地址出错']; break;
}

// 输出结果
Expand All @@ -64,17 +65,20 @@ public function run()
}else{
echo Json::encode(['state'=>'callback 参数不合法']);
}
exit();
}else{
echo $result;
$response = Yii::$app->response;
$response->format = Response::FORMAT_JSON;
$response->data = $result;
$response->send();
}
exit();
}

/**
* 处理上传
* @return string
* @throws \yii\base\ErrorException
* @throws \yii\base\Exception
* @return array
* @throws yii\base\ErrorException
* @throws yii\base\Exception
*/
private function actionUpload()
{
Expand Down Expand Up @@ -129,19 +133,19 @@ private function actionUpload()

// 生成上传实例对象并完成上传, 返回结果数据
$upload = new Uploader($fieldName, $config, $base64);
return Json::encode([
return [
'original' => $upload->realName, // 原始文件名, eg: 'img_6.jpg'
'title' => $upload->fileName, // 新文件名, eg: '171210_054500_8166.jpg'
'url' => $upload->fullName, // 返回的地址, eg: '/uploads/image/201712/171210_054500_8166.jpg'
'size' => $upload->fileSize, // 文件大小, eg: 108527
'type' => '.' . $upload->fileExt, // 文件类型, eg: '.jpg'
'state' => $upload->stateInfo, // 上传状态, 上传成功时必须返回'SUCCESS'
]);
];
}

/**
* 图片/文件在线管理器
* @return string
* @return array
*/
private function actionList()
{
Expand Down Expand Up @@ -178,7 +182,7 @@ private function actionList()
$files = self::getFiles($path, $rootPath, $allowFiles);
$length = count($files); // 文件总数
if($length === 0){ // 如果没有文件
return Json::encode(['state' => '没有匹配的文件', 'list' => [], 'start' => $start, 'total' => $length]); // 'state' => 'no match file'
return ['state' => '没有匹配的文件', 'list' => [], 'start' => $start, 'total' => $length]; // 'state' => 'no match file'
}

// 获取指定范围的文件列表
Expand All @@ -189,7 +193,7 @@ private function actionList()
}

// 返回数据
return Json::encode(['state' => 'SUCCESS', 'list' => $list, 'start' => $start, 'total' => $length]);
return ['state' => 'SUCCESS', 'list' => $list, 'start' => $start, 'total' => $length];
}

/**
Expand Down

0 comments on commit 4f800f2

Please sign in to comment.