Add bulk asset resource deletion

This commit is contained in:
2026-07-03 12:01:49 +08:00
parent 465cba3788
commit 26e6cc433b
2 changed files with 34 additions and 6 deletions
+26 -3
View File
@@ -32,8 +32,12 @@
<!-- 列表区域 --> <!-- 列表区域 -->
<el-card class="!border-none mt-4" shadow="never"> <el-card class="!border-none mt-4" shadow="never">
<div class="mb-4"> <div class="mb-4 flex items-center gap-2">
<el-button type="primary" @click="handleAdd">上传资源</el-button> <el-button type="primary" @click="handleAdd">上传资源</el-button>
<el-button type="danger" :disabled="selectedIds.length === 0" @click="handleBatchDelete">
批量删除
</el-button>
<span v-if="selectedIds.length > 0" class="text-sm text-gray-500">已选 {{ selectedIds.length }} </span>
</div> </div>
<el-tabs v-model="queryParams.type" @tab-change="handleTabChange"> <el-tabs v-model="queryParams.type" @tab-change="handleTabChange">
@@ -42,7 +46,8 @@
<el-tab-pane label="语音" name="3"></el-tab-pane> <el-tab-pane label="语音" name="3"></el-tab-pane>
</el-tabs> </el-tabs>
<el-table :data="tableData" v-loading="loading"> <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" />
<!-- <el-table-column prop="id" label="ID" width="80" /> --> <!-- <el-table-column prop="id" label="ID" width="80" /> -->
<el-table-column prop="title" label="标题" min-width="80" /> <el-table-column prop="title" label="标题" min-width="80" />
<el-table-column label="预览" width="100"> <el-table-column label="预览" width="100">
@@ -129,8 +134,9 @@ import MaterialPicker from '@/components/material/picker.vue'
import Upload from '@/components/upload/index.vue' import Upload from '@/components/upload/index.vue'
const loading = ref(false) const loading = ref(false)
const tableData = ref([]) const tableData = ref<any[]>([])
const total = ref(0) const total = ref(0)
const selectedIds = ref<number[]>([])
const searchData = reactive<any>({ const searchData = reactive<any>({
title: '', title: '',
@@ -198,6 +204,7 @@ const getList = async () => {
const res = await apiAssetResourceList(buildQueryParams()) const res = await apiAssetResourceList(buildQueryParams())
tableData.value = res.lists tableData.value = res.lists
total.value = res.count total.value = res.count
selectedIds.value = []
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} finally { } finally {
@@ -269,6 +276,22 @@ const handleDelete = async (row: any) => {
} }
} }
const handleSelectionChange = (rows: any[]) => {
selectedIds.value = rows.map((row) => Number(row.id)).filter((id) => id > 0)
}
const handleBatchDelete = async () => {
if (!selectedIds.value.length) return
try {
await ElMessageBox.confirm(`确定要删除选中的 ${selectedIds.value.length} 个资源吗?用户将无法再看到。`, '提示', { type: 'warning' })
await apiAssetResourceDelete({ id: selectedIds.value })
ElMessage.success('删除成功')
getList()
} catch (e) {
// canceled
}
}
const submitForm = async () => { const submitForm = async () => {
if (!formRef.value) return if (!formRef.value) return
await formRef.value.validate(async (valid: boolean) => { await formRef.value.validate(async (valid: boolean) => {
@@ -169,14 +169,19 @@ class AssetResourceController extends BaseAdminController
public function delete() public function delete()
{ {
$id = $this->request->post('id'); $id = $this->request->post('id');
if (empty($id)) { $ids = is_array($id) ? $id : [$id];
$ids = array_values(array_unique(array_filter(array_map('intval', $ids), static function (int $item): bool {
return $item > 0;
})));
if (empty($ids)) {
return $this->fail('缺少参数'); return $this->fail('缺少参数');
} }
Db::startTrans(); Db::startTrans();
try { try {
AssetResource::destroy($id); AssetResource::destroy($ids);
AssetUserResource::where('resource_id', $id)->delete(); AssetUserResource::whereIn('resource_id', $ids)->delete();
Db::commit(); Db::commit();
return $this->success('删除成功'); return $this->success('删除成功');
} catch (\Exception $e) { } catch (\Exception $e) {