Skip to content

Commit

Permalink
修复:上传本地文件时响应错误
Browse files Browse the repository at this point in the history
  • Loading branch information
swling committed Aug 23, 2022
1 parent 501dd07 commit b7006d0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
22 changes: 9 additions & 13 deletions includes/action/common/wnd-upload-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function execute(): array{
* 遍历文件上传
* @since 2019.05.06 改写
*/
$return_array = []; // 定义图片信息返回数组
$return_array = ['status' => 1, 'msg' => __('上传成功', 'wnd'), 'data' => []]; // 定义图片信息返回数组
$files = $_FILES['wnd_file']; //暂存原始上传信息,后续将重写$_FILES全局变量以适配WordPress上传方式

foreach ($files['name'] as $key => $value) {
Expand All @@ -63,14 +63,14 @@ protected function execute(): array{

// 单文件错误检测
if ($_FILES['temp_key']['error'] > 0) {
$return_array[] = ['status' => 0, 'msg' => 'Error: ' . $_FILES['temp_key']['error']];
$$return_array['data'] = ['status' => 0, 'msg' => 'Error: ' . $_FILES['temp_key']['error']];
continue;
}

// 文件格式限制
$extension = pathinfo($_FILES['temp_key']['name'])['extension'] ?? '';
if (!wnd_is_allowed_extension($extension)) {
$return_array[] = ['status' => 0, 'msg' => 'Error: File types not allowed'];
$$return_array['data'] = ['status' => 0, 'msg' => 'Error: File types not allowed'];
continue;
}

Expand All @@ -90,7 +90,7 @@ protected function execute(): array{

// 上传失败
if (is_wp_error($file_id)) {
$return_array[] = ['status' => 0, 'msg' => $file_id->get_error_message()];
$$return_array['data'] = ['status' => 0, 'msg' => $file_id->get_error_message()];
continue;
}

Expand All @@ -104,16 +104,12 @@ protected function execute(): array{

// 将当前上传的图片信息写入数组
$temp_array = [
'status' => 1,
'data' => [
'url' => $url,
'thumbnail' => $thumbnail ?? 0,
'id' => $file_id,
'post' => get_post($file_id),
],
'msg' => __('上传成功', 'wnd'),
'url' => $url,
'thumbnail' => $thumbnail ?? 0,
'id' => $file_id,
'post' => get_post($file_id),
];
$return_array[] = $temp_array;
$return_array['data'][] = $temp_array;

/**
* @since 2019.02.13 当存在meta key时,表明上传文件为特定用途存储,仅允许上传单个文件
Expand Down
1 change: 0 additions & 1 deletion includes/action/wnd-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ final public function do_action(): array{
$defender->write_log();

// 响应
$execute['time'] = timer_stop();
return $execute;
}

Expand Down
6 changes: 4 additions & 2 deletions includes/controller/wnd-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,10 @@ public static function handle_action(WP_REST_Request $request): array{
}

try {
$action = new $class($request);
return $action->do_action();
$action = new $class($request);
$res = $action->do_action();
$res['time'] = timer_stop();
return $res;
} catch (Exception $e) {
return ['status' => $e->getCode(), 'msg' => $e->getMessage()];
}
Expand Down
13 changes: 7 additions & 6 deletions static/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,16 +376,17 @@ function _wnd_render_form(container, form_json, add_class = '') {
field.help.text = response.data.msg;
field.help.class = 'is-danger';
} else {
for (let i = 0, n = response.data.length; i < n; i++) {
if (response.data[i].status <= 0) {
field.help.text = response.data[i].msg;
let data = response.data.data;
for (let i = 0, n = data.length; i < n; i++) {
if (data[i].status <= 0) {
field.help.text = data[i].msg;
field.help.class = 'is-danger';
} else {
field.help.text = wnd.msg.upload_successfully;
field.help.class = 'is-success';
field.thumbnail = response.data[i].data.thumbnail;
field.file_id = response.data[i].data.id;
field.file_name = wnd.msg.upload_successfully + '&nbsp<a href="' + response.data[i].data.url + '" target="_blank">' + wnd.msg.view + '</a>';
field.thumbnail = data[i].thumbnail;
field.file_id = data[i].id;
field.file_name = wnd.msg.upload_successfully + '&nbsp<a href="' + data[i].url + '" target="_blank">' + wnd.msg.view + '</a>';
}

// 单个图片
Expand Down
Loading

0 comments on commit b7006d0

Please sign in to comment.