chore(addon/aikefu): 会话管理支持批量删除

This commit is contained in:
2025-12-08 17:12:00 +08:00
parent 4e1126bf45
commit 6c5f163287
2 changed files with 63 additions and 11 deletions

View File

@@ -158,11 +158,18 @@ class Kefu extends BaseShop
public function deleteConversation() public function deleteConversation()
{ {
$id = input("id/d", ""); $id = input("id/d", "");
$ids = input("ids/a", []);
if (empty($id)) { // 验证参数
if (empty($id) && empty($ids)) {
return $this->error('会话ID不能为空'); return $this->error('会话ID不能为空');
} }
// 处理单个ID或ID数组
if (!empty($id)) {
$ids = [$id];
}
$kefu_conversation_model = new KefuConversationModel(); $kefu_conversation_model = new KefuConversationModel();
$kefu_message_model = new KefuMessageModel(); $kefu_message_model = new KefuMessageModel();
@@ -170,22 +177,25 @@ class Kefu extends BaseShop
Db::startTrans(); Db::startTrans();
try { try {
// 删除会话关联的消 // 获取所有要删除会话
$conversation_info = $kefu_conversation_model->getConversationInfo([ $conversations = $kefu_conversation_model->getConversationList([
['id', '=', $id], ['id', 'in', $ids],
['site_id', '=', $this->site_id] ['site_id', '=', $this->site_id]
]); ]);
if (!empty($conversation_info)) { // 删除所有会话关联的消息
$kefu_message_model->deleteMessage([ if (!empty($conversations['data'])) {
['site_id', '=', $this->site_id], foreach ($conversations['data'] as $conversation) {
['conversation_id', '=', $conversation_info['conversation_id']] $kefu_message_model->deleteMessage([
]); ['site_id', '=', $this->site_id],
['conversation_id', '=', $conversation['conversation_id']]
]);
}
} }
// 删除会话 // 删除会话
$result = $kefu_conversation_model->deleteConversation([ $result = $kefu_conversation_model->deleteConversation([
['id', '=', $id], ['id', 'in', $ids],
['site_id', '=', $this->site_id] ['site_id', '=', $this->site_id]
]); ]);

View File

@@ -54,7 +54,7 @@
<script type="text/html" id="toolbarDemo"> <script type="text/html" id="toolbarDemo">
<div class="layui-btn-container"> <div class="layui-btn-container">
<!-- 可以在这里添加按钮 --> <button type="button" class="layui-btn layui-btn-danger" id="batchDeleteBtn">批量删除</button>
</div> </div>
</script> </script>
@@ -82,6 +82,7 @@
title: '会话管理', title: '会话管理',
width: '100%', width: '100%',
cols: [[ cols: [[
{type: 'checkbox', fixed: 'left'},
{field: 'conversation_id', title: '会话ID', width: 200, align: 'center'}, {field: 'conversation_id', title: '会话ID', width: 200, align: 'center'},
{field: 'user_id', title: '用户ID', width: 150, align: 'center'}, {field: 'user_id', title: '用户ID', width: 150, align: 'center'},
{field: 'name', title: '会话名称', width: 180, align: 'center'}, {field: 'name', title: '会话名称', width: 180, align: 'center'},
@@ -192,5 +193,46 @@
}); });
} }
}); });
// 批量删除按钮点击事件
$('#batchDeleteBtn').click(function() {
// 获取选中的行数据
var checkStatus = table.checkStatus('conversationTable');
var data = checkStatus.data;
if (data.length === 0) {
layer.msg('请选择要删除的会话', {icon: 2});
return;
}
// 提取选中的会话ID
var ids = [];
for (var i = 0; i < data.length; i++) {
ids.push(data[i].id);
}
// 确认删除
layer.confirm('确定要删除选中的 ' + data.length + ' 个会话吗?删除后将无法恢复', function(index) {
$.ajax({
url: ns.url("aikefu://shop/kefu/deleteConversation"),
type: 'POST',
data: {ids: ids},
dataType: 'json',
success: function(res) {
if (res.code === 0) {
layer.msg('删除成功', {icon: 1});
// 重新加载表格数据
tableIns.reload();
} else {
layer.msg('操作失败:' + res.message, {icon: 2});
}
},
error: function() {
layer.msg('请求失败,请稍后重试', {icon: 2});
}
});
layer.close(index);
});
});
}); });
</script> </script>