feat: 文件表格支持批量删除文件

This commit is contained in:
Mr.Huan 2022-01-25 15:17:14 +08:00
parent d91fa33330
commit 70f1258bab
3 changed files with 132 additions and 0 deletions

View File

@ -12,6 +12,8 @@ use App\Models\FileUser;
use App\Models\User;
use App\Module\Base;
use App\Module\Ihttp;
use App\Tasks\BatchRemoveFileTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Illuminate\Support\Facades\DB;
use Request;
@ -371,6 +373,33 @@ class FileController extends AbstractController
return Base::retSuccess('删除成功', $file);
}
/**
* @api {get} api/file/batch/remove 批量删除文件
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup file
* @apiName batchRemove
*
* @apiParam {Array} ids 文件ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*
* @return array
*/
public function batch__remove(): array
{
$ids = Request::input('ids');
if ( empty($ids) || !is_array($ids) ) {
return Base::retError("请选择要删除的文件");
}
$task = new BatchRemoveFileTask($ids, User::userid());
Task::deliver($task);
return Base::retSuccess('success');
}
/**
* @api {get} api/file/content 08. 获取文件内容
*

View File

@ -0,0 +1,55 @@
<?php
namespace App\Tasks;
use App\Exceptions\ApiException;
use App\Models\File;
use App\Models\Tmp;
use App\Models\User;
use App\Models\WebSocketTmpMsg;
use Carbon\Carbon;
use Log;
use Throwable;
/**
* 删除过期临时数据任务
* Class DeleteTmpTask
* @package App\Tasks
*/
class BatchRemoveFileTask extends AbstractTask
{
protected array $_ids = [];
protected int $_userid;
public function __construct(array $ids, $userid)
{
$this->_ids = $ids;
$this->_userid = $userid;
}
public function start()
{
foreach ($this->_ids as $id) {
Log::info("---------- $id ----------");
Log::info("尝试删除Id为[$id]的文件");
$file = File::find($id);
if (empty($file)) {
Log::warning("Id为[$id]的文件不存在或已被删除");
continue;
}
Log::info("获取到文件名为[" . $file->name . "],类型为[" . ( $file->type ?: $file->ext ) . "]");
$permission = $file->getPermission($this->_userid);
if ($permission < 1000) {
Log::warning("文件[$id][" . $file->name . "]仅限所有者或创建者操作");
continue;
}
try {
$file->deleteFile();
Log::info("删除Id为[$id]的文件成功");
} catch (Throwable $throwable) {
Log::error("删除Id为[$id]的文件失败,原因是:" . $throwable->getMessage());
}
}
}
}

View File

@ -37,6 +37,8 @@
"<em>{{shearFile.name}}</em>"
</div>
</Button>
<Button v-if="selectFile.length > 0" size="small" type="info">剪切</Button>
<Button v-if="selectFile.length > 0" size="small" type="error" @click="deleteSelectFile">删除</Button>
<div v-if="loadIng > 0" class="nav-load"><Loading/></div>
<div class="flex-full"></div>
<div :class="['switch-button', tableMode ? 'table' : '']" @click="tableMode=!tableMode">
@ -53,6 +55,10 @@
:no-data-text="$L('没有任何文件')"
@on-cell-click="clickRow"
@on-contextmenu="handleContextMenu"
@on-select="handleTableSelect"
@on-select-cancel="handleTableSelect"
@on-select-all-cancel="handleTableSelect"
@on-select-all="handleTableSelect"
context-menu
stripe/>
</div>
@ -428,6 +434,8 @@ export default {
top: 0,
left: 0
},
selectFile: [],
}
},
@ -541,6 +549,11 @@ export default {
methods: {
initLanguage() {
this.columns = [
{
type: 'selection',
width: 60,
align: 'center'
},
{
title: this.$L('文件名'),
key: 'name',
@ -814,6 +827,8 @@ export default {
},
clickRow(row) {
//
this.selectFile = [];
this.dropFile(row, 'open');
},
@ -1239,6 +1254,39 @@ export default {
this.uploadShow = true;
return true;
},
handleTableSelect(selection, row) {
this.selectFile = selection;
},
deleteSelectFile() {
if ( this.selectFile.length <= 0 ) {
$A.messageError("未选择任何文件或文件夹");
return false;
}
let s_ids = this.selectFile.map( (item, index) => {
return item.id;
} );
$A.modalConfirm({
title: '批量删除',
content: '你确定要删除这些文件吗?',
loading: true,
onOk: () => {
this.$store.dispatch("call", {
url: 'file/batch/remove',
data: {
ids: s_ids,
},
}).then(({msg}) => {
this.$Modal.remove();
$A.messageSuccess("已提交至后台处理,请稍后再回来查看结果吧");
}).catch(({msg}) => {
$A.modalError(msg, 301);
this.$Modal.remove();
});
}
});
}
}
}
</script>