chore(addon/aikefu): 变更API暴漏的端点
This commit is contained in:
118
src/addon/aikefu/event/KefuClearConversation.php
Normal file
118
src/addon/aikefu/event/KefuClearConversation.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace addon\aikefu\event;
|
||||
|
||||
use addon\aikefu\model\Conversation as KefuConversationModel;
|
||||
use addon\aikefu\model\Message as KefuMessageModel;
|
||||
|
||||
/**
|
||||
* 清除客服会话历史
|
||||
*/
|
||||
class KefuClearConversation
|
||||
{
|
||||
/**
|
||||
* 处理清除会话历史事件
|
||||
* @param array $data 事件数据
|
||||
* @return array
|
||||
*/
|
||||
public function handle($data)
|
||||
{
|
||||
try {
|
||||
$conversation_id = $data['conversation_id'] ?? '';
|
||||
$user_id = $data['user_id'] ?? '';
|
||||
$site_id = $data['site_id'] ?? 0;
|
||||
|
||||
// 验证参数
|
||||
if (empty($conversation_id) && empty($user_id)) {
|
||||
return [
|
||||
'code' => -1,
|
||||
'message' => '会话ID或用户ID不能为空',
|
||||
'data' => []
|
||||
];
|
||||
}
|
||||
|
||||
$conversation_model = new KefuConversationModel();
|
||||
$message_model = new KefuMessageModel();
|
||||
|
||||
$deleted_messages = 0;
|
||||
$deleted_conversations = 0;
|
||||
|
||||
if (!empty($conversation_id)) {
|
||||
// 删除指定会话的消息和会话记录
|
||||
|
||||
// 先删除该会话的所有消息
|
||||
$message_condition = [
|
||||
['site_id', '=', $site_id],
|
||||
['conversation_id', '=', $conversation_id]
|
||||
];
|
||||
|
||||
$message_result = $message_model->deleteMessage($message_condition);
|
||||
if ($message_result['code'] >= 0) {
|
||||
$deleted_messages = $message_result['data']['result'] ?? 0;
|
||||
}
|
||||
|
||||
// 再删除会话记录
|
||||
$conversation_condition = [
|
||||
['site_id', '=', $site_id],
|
||||
['conversation_id', '=', $conversation_id]
|
||||
];
|
||||
|
||||
$conversation_result = $conversation_model->deleteConversation($conversation_condition);
|
||||
if ($conversation_result['code'] >= 0) {
|
||||
$deleted_conversations = $conversation_result['data']['result'] ?? 0;
|
||||
}
|
||||
|
||||
} else if (!empty($user_id)) {
|
||||
// 删除指定用户的所有会话和消息
|
||||
|
||||
// 先获取该用户的所有会话ID
|
||||
$conversation_list = $conversation_model->getConversationList([
|
||||
['site_id', '=', $site_id],
|
||||
['user_id', '=', $user_id]
|
||||
], 'conversation_id');
|
||||
|
||||
$conversation_ids = array_column($conversation_list['data'], 'conversation_id');
|
||||
|
||||
if (!empty($conversation_ids)) {
|
||||
// 删除所有会话的消息
|
||||
$message_condition = [
|
||||
['site_id', '=', $site_id],
|
||||
['conversation_id', 'in', $conversation_ids]
|
||||
];
|
||||
|
||||
$message_result = $message_model->deleteMessage($message_condition);
|
||||
if ($message_result['code'] >= 0) {
|
||||
$deleted_messages = $message_result['data']['result'] ?? 0;
|
||||
}
|
||||
|
||||
// 删除所有会话记录
|
||||
$conversation_condition = [
|
||||
['site_id', '=', $site_id],
|
||||
['user_id', '=', $user_id]
|
||||
];
|
||||
|
||||
$conversation_result = $conversation_model->deleteConversation($conversation_condition);
|
||||
if ($conversation_result['code'] >= 0) {
|
||||
$deleted_conversations = $conversation_result['data']['result'] ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => 0,
|
||||
'message' => '清除成功',
|
||||
'data' => [
|
||||
'deleted_messages' => $deleted_messages,
|
||||
'deleted_conversations' => $deleted_conversations
|
||||
]
|
||||
];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return [
|
||||
'code' => -1,
|
||||
'message' => '清除失败:' . $e->getMessage(),
|
||||
'data' => []
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user