$user->date+$user->lifetime<time()){
self::onClose($client_id);
}
$wechatMsgUser = new WechatMsgUser();
$msgUser = $wechatMsgUser->where('openid','eq',$user->uid)->where('type','eq',0)->find();
if(!$msgUser->id){
self::onClose($client_id);
}
//*客服上线,设置数据库状态字段为在线状态
$msgUser->status = 1;
$msgUser->save();
$uid = $msgUser->id;
// 设置session,这个$_SESSION我是为了下面的onclose方法里设置客服离线状态
$_SESSION = [
'id' => $uid,
];
// 将当前$client_id与uid绑定
Gateway::bindUid($client_id, $uid);
$msgService = new MsgService();
$msgService->checkLeavedMessage($uid);
return;
break;
case 'ping':
$pingData=[
'emit'=>'pong',
'data'=>$client_id
];
Gateway::sendToClient($client_id, json_encode($pingData));
return;
default:
echo "unknown message $data" . PHP_EOL;
}
}
这里因为我用的是jwt验证,所以多绕了一层,先通过token找到layuiadmin的uid,再通过uid找到客服ID,把客服ID和client_id绑定,这样在后端php里就可以直接使用gateway::sendToUid来推送消息了。
5、tp后端,在需要推送消息的地方使用GatewayClient来主动推送消息。
这是接收消息的整个流程:微信开放接口请求消息推送接口url→php拿到数据,存库,通过GatewayClient主动推送到指定客服→前端拿到数据通过layim渲染到视图
其实这个Event.php我只做了两件事,一个是心跳检测,另一个就是登陆后将客服的ID和client_id绑定。
发送消息我用的是ajax的http方式发送,没有使用websocket。
未能实现的功能:
消息状态的处理,未读/已读
layim隐身/在线
目前只有文字消息,没有图片和卡片消息
相关推荐:
微信小程序机器人自动客服功能
微信公众平台开发:多客服接口说明
以上就是如何通过LayuiAdmin&LayIM&Thinkphp&Gateway实现小程序多客服接入系统的例子的详细内容,更多请关注php中文网其它相关文章!
小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用。
关键词:如何通过LayuiAdmin&LayIM&Thinkphp&Gateway完成小程序多客服接入系统的例子