增加批量删除模型功能

This commit is contained in:
RockYang
2025-08-11 13:53:03 +08:00
parent c651103eff
commit e7db720986
2 changed files with 95 additions and 3 deletions

View File

@@ -8,6 +8,7 @@ package admin
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
import (
"fmt"
"geekai/core"
"geekai/core/types"
"geekai/handler"
@@ -36,6 +37,7 @@ func (h *ChatModelHandler) RegisterRoutes() {
group.POST("set", h.Set)
group.POST("sort", h.Sort)
group.GET("remove", h.Remove)
group.POST("batch-remove", h.BatchRemove)
}
func (h *ChatModelHandler) Save(c *gin.Context) {
@@ -211,3 +213,33 @@ func (h *ChatModelHandler) Remove(c *gin.Context) {
}
resp.SUCCESS(c)
}
// BatchRemove 批量删除模型
func (h *ChatModelHandler) BatchRemove(c *gin.Context) {
var data struct {
Ids []uint `json:"ids"`
}
if err := c.ShouldBindJSON(&data); err != nil {
resp.ERROR(c, types.InvalidArgs)
return
}
if len(data.Ids) == 0 {
resp.ERROR(c, "请选择要删除的模型")
return
}
// 执行批量删除
err := h.DB.Where("id IN ?", data.Ids).Delete(&model.ChatModel{}).Error
if err != nil {
logger.Error("批量删除模型失败:", err)
resp.ERROR(c, "批量删除失败:"+err.Error())
return
}
resp.SUCCESS(c, gin.H{
"message": fmt.Sprintf("成功删除 %d 个模型", len(data.Ids)),
"deleted_count": len(data.Ids),
})
}

View File

@@ -11,10 +11,24 @@
<el-button :icon="Search" @click="fetchData">搜索</el-button>
<el-button type="primary" :icon="Plus" @click="add">新增</el-button>
<el-button
type="danger"
:icon="Delete"
:disabled="selectedItems.length === 0"
@click="batchRemove"
>
批量删除 ({{ selectedItems.length }})
</el-button>
</div>
<el-row>
<el-table :data="items" :row-key="(row) => row.id" table-layout="auto">
<el-table
:data="items"
:row-key="(row) => row.id"
table-layout="auto"
@selection-change="handleSelectionChange"
ref="tableRef"
>
<el-table-column type="selection" width="38"></el-table-column>
<el-table-column prop="name" label="模型名称">
<template #default="scope">
@@ -216,9 +230,9 @@
<script setup>
import { httpGet, httpPost } from '@/utils/http'
import { dateFormat, removeArrayItem, substr } from '@/utils/libs'
import { DocumentCopy, InfoFilled, Plus, Search } from '@element-plus/icons-vue'
import { DocumentCopy, InfoFilled, Plus, Search, Delete } from '@element-plus/icons-vue'
import ClipboardJS from 'clipboard'
import { ElMessage } from 'element-plus'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Sortable } from 'sortablejs'
import { onMounted, onUnmounted, reactive, ref } from 'vue'
@@ -390,6 +404,44 @@ const remove = function (row) {
ElMessage.error('删除失败:' + e.message)
})
}
const selectedItems = ref([])
const tableRef = ref(null)
const handleSelectionChange = (val) => {
selectedItems.value = val
}
const batchRemove = () => {
if (selectedItems.value.length === 0) {
ElMessage.warning('请选择要删除的模型')
return
}
// 构建确认信息
ElMessageBox.confirm('确定要删除选中模型吗?<br/> 此操作不可恢复!', '批量删除确认', {
confirmButtonText: '确定删除',
cancelButtonText: '取消',
type: 'warning',
dangerouslyUseHTMLString: true,
customClass: 'batch-delete-confirm',
})
.then(() => {
const ids = selectedItems.value.map((item) => item.id)
httpPost('/api/admin/model/batch-remove', { ids: ids })
.then((res) => {
ElMessage.success(`批量删除成功!已删除 ${res.data.deleted_count} 个模型`)
fetchData()
selectedItems.value = []
})
.catch((e) => {
ElMessage.error('批量删除失败:' + e.message)
})
})
.catch(() => {
// 用户取消
})
}
</script>
<style lang="scss" scoped>
@@ -401,6 +453,14 @@ const remove = function (row) {
max-width: 150px;
margin-right: 10px;
}
.el-button {
margin-right: 10px;
&:last-child {
margin-right: 0;
}
}
}
.cell {