Home > Article > WeChat Applet > Example of how to implement multiple customer service access systems for mini programs through LayuiAdmin&LayIM&Thinkphp&Gateway
This article brings you an example of how to implement multiple customer service access systems for mini programs through LayuiAdmin&LayIM&Thinkphp&Gateway. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Let’s take a look at the renderings first.
Functions implemented:
1. Mini program customer service conversations are received in real time and pushed to Layim
2. Mini program customer service conversations can be responded to in real time through Layim
3. You can add multiple mini programs and multiple customer service
4. Online customer service allocates conversations in order. If the customer service is not online, the message will be processed
5. Automatic reply function
Implementation logic:
Use the openid of the WeChat visitor as the unique identifier to create a new layim user and make it a customer service friend. The backend TP serves as the middle layer for message reception and forwarding.
Implementation steps (focus on the gateway part):
1. Install TP, composer installs workerman, gateway
2. Create server.php (others) in the root directory of tp Any name will work)#!/usr/bin/env php
<?php ini_set('display_errors', 'on'); if(strpos(strtolower(PHP_OS), 'win') === 0) { exit("start.php not support windows.\n"); } // 检查扩展 if(!extension_loaded('pcntl')) { exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n"); } if(!extension_loaded('posix')) { exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n"); } define('APP_PATH', __DIR__ . '/application/');//如果修改了也要跟着修改,tp的application define('BIND_MODULE','chat/Run');//这个位置是你唯一要自定义的 // 加载框架引导文件 require __DIR__ . '/thinkphp/start.php';
3. Create a module chat in the application directory of tp and create a Run controller. In addition to the Events namespace, other There is basically no need to change. Changing the port is a bit tricky. It is recommended to use the default
class Run { public function __construct() { //注册register new Register('text://0.0.0.0:1236'); //初始化 bussinessWorker 进程 $worker = new BusinessWorker(); $worker->name = 'WebIMBusinessWorker'; $worker->count = 4; $worker->registerAddress = '127.0.0.1:1236'; //设置处理业务的类,此处制定Events的命名空间 $worker->eventHandler = '\app\chat\controller\Events'; // 初始化 gateway 进程 $gateway = new Gateway("websocket://0.0.0.0:8282"); $gateway->name = 'WebIMGateway'; $gateway->count = 4; $gateway->lanIp = '127.0.0.1'; $gateway->startPort = 2900; $gateway->registerAddress = '127.0.0.1:1236'; $gateway->pingInterval = 55; $gateway->pingNotResponseLimit = 1; $gateway->pingData = '{"emit":"ping"}';//此处为心跳包数据 //运行所有Worker; if(!defined('GLOBAL_START')) { Worker::runAll(); } } }
4 of the gateway. 4. Create the controller class of Event.php. Event.php is the main logic processing class. I will only briefly talk about it here. My onmessage method:
public static function onMessage($client_id, $data){ $message = json_decode($data, true); $message_type = $message['emit']; switch($message_type) { case 'init': // uid //根据token获取uid $tokenCache = new TokenCache(); $user = $tokenCache->where('token','eq',$message['token'])->order('id DESC')->find(); if(!$user->uid||$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; } }
Because I am using jwt verification, I have gone through an extra layer. I first find the uid of layuiadmin through token, then find the customer service ID through uid, and combine the customer service ID and Client_id binding, so that gateway::sendToUid can be used directly to push messages in the back-end php.
5. tp backend, use GatewayClient to actively push messages where messages need to be pushed.
This is the entire process of receiving messages: WeChat open interface requests the message push interface url→php gets the data, stores it, and actively pushes it to the designated customer service through GatewayClient→the front end gets the data and renders it to the view through layim
In fact, I only did two things in this Event.php, one is heartbeat detection, and the other is to bind the customer service ID and client_id after logging in.
I use ajax http method to send messages, and do not use websocket.
Function that has not been implemented:
Processing of message status, unread/read
layim invisible/online
Currently only text messages, no Pictures and card messages
Related recommendations:
WeChat mini program robot automatic customer service function
WeChat public platform development: many Customer Service Interface Description
The above is the detailed content of Example of how to implement multiple customer service access systems for mini programs through LayuiAdmin&LayIM&Thinkphp&Gateway. For more information, please follow other related articles on the PHP Chinese website!