马上注册,免费下载更多dz插件网资源。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
之前很多坛友需求客服系统的,实际是很简单的哈?
那怎么做呢?写个API接口就可以了- <?phpdefined('ECMSAPI_MOD') or exit;header('Access-Control-Allow-Origin: ' . rtrim($public_r['add_murl'], '/'));header('Content-Type: application/json; charset=utf-8');require_once ECMS_PATH.'e/extend/chat/config.php';require_once(ECMS_PATH.'e/class/ip.php');require_once ECMS_PATH.'e/extend/workermanchat/Applications/GatewayClient/Gateway.php';use GatewayClient\Gateway;Gateway::$registerAddress = '127.0.0.1:1236';function hashUsername($username) {return hash('sha256', RepPostStr2($username));}$action = isset($_POST['action']) ? RepPostStr($_POST['action']) : '';$client_id = isset($_POST['client_id']) ? RepPostStr($_POST['client_id']) : '';$userid = isset($_POST['userid']) ? intval($_POST['userid']) : 0;$username = isset($_POST['username']) ? RepPostStr($_POST['username']) : '';$groupid = isset($_POST['groupid']) ? intval($_POST['groupid']) : 0;$avatar = isset($_POST['avatar']) ? RepPostStr($_POST['avatar']) : '/e/extend/pcpinglun/assets/nouserpic.gif';$msg = isset($_POST['msg']) ? RepPostStr($_POST['msg']) : '';$replyusername = isset($_POST['replyusername']) ? RepPostStr($_POST['replyusername']) : '';$chat_group = 'customer_service_group'; // 客服分组名$current_uid = hashUsername($username);$target_uid = hashUsername($replyusername);if (empty($client_id) || !Gateway::isOnline($client_id)) {$api->load('fun')->json(0 , '客户端连接失效');}if (empty($username) || empty($avatar)) {$api->load('fun')->json(0 , '用户信息不完整或UID生成失败');}if ($action == 'service_bind') {// 客服绑定:加入客服分组if (!in_array($groupid, [4,5])) {$api->load('fun')->json(0 , '非客服账号(权限不足)');}Gateway::unbindUid($client_id, $current_uid);Gateway::bindUid($client_id, $current_uid);Gateway::joinGroup($client_id, $chat_group);Gateway::setSession($client_id, ['iskefu'=>'1','userid'=>$userid,'username'=>$username,'groupid'=>$groupid,'avatar'=>$avatar]);$api->load('fun')->json(1 ,['client_id'=>$client_id,'username'=>$username,'bind_uid'=>$current_uid,'avatar'=>$avatar],'客服绑定成功');}if ($action == 'user_bind') {// 用户绑定Gateway::unbindUid($client_id, $current_uid);Gateway::bindUid($client_id, $current_uid);Gateway::setSession($client_id, ['iskefu'=>'0','userid'=>$userid,'username'=>$username,'groupid'=>$groupid,'avatar'=>$avatar]);$push_data = json_encode(['type'=>'user_online','bind_uid'=>$current_uid,'username'=>$username,'avatar'=>$avatar,'client_id'=>$client_id,'time'=>date('Y-m-d H:i:s')], JSON_UNESCAPED_UNICODE);Gateway::sendToGroup($chat_group, $push_data);$api->load('fun')->json(1 ,['client_id'=>$client_id,'username'=>$username,'bind_uid'=>$current_uid,'avatar'=>$avatar],'用户绑定成功');}if ($action == 'user_send_msg') {// 用户发消息$user_session = Gateway::getSession($client_id);if (empty($user_session) || $user_session['iskefu'] != '0') {$api->load('fun')->json(0 , '用户未绑定');}if (empty($msg)) {$api->load('fun')->json(0 , '消息内容不能为空');}$push_data = json_encode(['type'=>'user_msg','bind_uid'=>$current_uid,'username'=>$user_session['username'],'avatar'=>$user_session['avatar'],'client_id'=>$client_id,'msg'=>$msg,'time'=>date('Y-m-d H:i:s')], JSON_UNESCAPED_UNICODE);Gateway::sendToGroup($chat_group, $push_data);$online_service_sessions = Gateway::getClientSessionsByGroup($chat_group);$has_online_service = !empty($online_service_sessions);if (!$has_online_service) {$apiKey = $public_r['add_siliconflow'] ?? "sk-qehvnsybkfkydiuqfhmxwxqygckhvflmhjtglrmovfcpgqvn";$ai_res = ecmsCurlReadtext('https://api.siliconflow.cn/v1/chat/completions','POST',json_encode(["model"=>"Qwen/Qwen2.5-7B-Instruct","messages"=>[["role"=>"user","content"=>$msg]]]), ['Content-Type: application/json','Authorization: Bearer '.$apiKey]);$ai_data = json_decode($ai_res, true);$ai_reply = $ai_data['choices'][0]['message']['content'] ?? '客服暂未在线';Gateway::sendToUid($current_uid, json_encode(['type'=>'ai_reply','msg'=>$ai_reply,'avatar'=>'/e/extend/pcpinglun/assets/nouserpic.gif','time'=>date('Y-m-d H:i:s')]));$api->load('fun')->json(1 , 'AI回复已发送');} else {$api->load('fun')->json(1 , '消息已转发给客服');}}if ($action == 'service_reply_msg') {// 客服回复用户$service_session = Gateway::getSession($client_id);if (empty($service_session) || $service_session['iskefu'] != '1' || !in_array($service_session['groupid'], [4,5])) {$api->load('fun')->json(0 , '非客服账号(权限不足)');}if (empty($replyusername) || empty($msg) || empty($target_uid)) {$api->load('fun')->json(0 , '回复参数不完整');}if (!Gateway::isUidOnline($target_uid)) {$api->load('fun')->json(0 , '用户已离线,无法发送消息');}$push_data = json_encode(['type'=>'service_reply','service_name'=>$service_session['username'],'avatar'=>$service_session['avatar'],'msg'=>$msg,'time'=>date('Y-m-d H:i:s')], JSON_UNESCAPED_UNICODE);Gateway::sendToUid($target_uid, $push_data);$api->load('fun')->json(1 , '回复发送成功');}if ($action == 'get_online_users') {// 获取在线用户列表$service_session = Gateway::getSession($client_id);if (empty($service_session) || $service_session['iskefu'] != '1' || !in_array($service_session['groupid'], [4,5])) {$api->load('fun')->json(0 , '非客服无权访问');}$online_users = [];$all_online_sessions = Gateway::getAllClientSessions();foreach ($all_online_sessions as $cid => $sess) {if ($sess['iskefu'] != '0') {continue;}$username = $sess['username'];$avatar = $sess['avatar'];$bind_uid = hashUsername($username);$isOnline = Gateway::isUidOnline($bind_uid);$online_users[] = ['client_id' => $cid,'username' => $username,'avatar' => $avatar,'bind_uid' => $bind_uid, 'status' => $isOnline ? 'online' : 'offline'];}$api->load('fun')->json(1 , $online_users, '在线用户获取成功');}if ($action == 'service_leave') {// 客服退出$service_session = Gateway::getSession($client_id);if (empty($service_session) || $service_session['iskefu'] != '1') {$api->load('fun')->json(0 , '非客服账号');}Gateway::leaveGroup($client_id, $chat_group);$uid_arr = Gateway::getUidByClientId($client_id);if (!empty($uid_arr)) {Gateway::unbindUid($client_id, $uid_arr[0]);}$api->load('fun')->json(1 , '客服已退出');}
复制代码 116行都可以做到了。
©DZ插件网所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。 本站内容为站长个人技术研究记录或网络,不提供用户交互功能,所有内容版权归原作者所有。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,得到更好的正版服务。 您在本站任何的赞助购买、下载、查阅、回复等行为等均表示接受并同意签订《DZ插件网免责声明协议》。 如有侵权请邮件与我们联系处理: discuzaddons@vip.qq.com 并出示相关证明以便删除。敬请谅解!
|
|