Home >WeChat Applet >Mini Program Development >Example of how to implement multiple customer service access systems for mini programs through LayuiAdmin&LayIM&Thinkphp&Gateway

Example of how to implement multiple customer service access systems for mini programs through LayuiAdmin&LayIM&Thinkphp&Gateway

不言
不言Original
2018-08-25 09:25:105590browse

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.

Example of how to implement multiple customer service access systems for mini programs through LayuiAdmin&LayIM&Thinkphp&Gateway 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(&#39;display_errors&#39;, &#39;on&#39;);
if(strpos(strtolower(PHP_OS), &#39;win&#39;) === 0)
{
    exit("start.php not support windows.\n");
}
// 检查扩展
if(!extension_loaded(&#39;pcntl&#39;))
{
    exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
}
if(!extension_loaded(&#39;posix&#39;))
{
    exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
}
define(&#39;APP_PATH&#39;, __DIR__ . &#39;/application/&#39;);//如果修改了也要跟着修改,tp的application
define(&#39;BIND_MODULE&#39;,&#39;chat/Run&#39;);//这个位置是你唯一要自定义的
// 加载框架引导文件
require __DIR__ . &#39;/thinkphp/start.php&#39;;

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(&#39;text://0.0.0.0:1236&#39;);
        //初始化 bussinessWorker 进程
        $worker = new BusinessWorker();
        $worker->name = &#39;WebIMBusinessWorker&#39;;
        $worker->count = 4;
        $worker->registerAddress = &#39;127.0.0.1:1236&#39;;
        //设置处理业务的类,此处制定Events的命名空间
        $worker->eventHandler = &#39;\app\chat\controller\Events&#39;;
        // 初始化 gateway 进程
        $gateway = new Gateway("websocket://0.0.0.0:8282");
        $gateway->name = &#39;WebIMGateway&#39;;
        $gateway->count = 4;
        $gateway->lanIp = &#39;127.0.0.1&#39;;
        $gateway->startPort = 2900;
        $gateway->registerAddress = &#39;127.0.0.1:1236&#39;;
        $gateway->pingInterval = 55;
        $gateway->pingNotResponseLimit = 1;
        $gateway->pingData = &#39;{"emit":"ping"}&#39;;//此处为心跳包数据
        //运行所有Worker;
        if(!defined(&#39;GLOBAL_START&#39;))
        {
            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[&#39;emit&#39;];
        
        switch($message_type) {
            case &#39;init&#39;:
                // uid
                //根据token获取uid
                $tokenCache =   new TokenCache();
                $user   =   $tokenCache->where(&#39;token&#39;,&#39;eq&#39;,$message[&#39;token&#39;])->order(&#39;id DESC&#39;)->find();
                if(!$user->uid||$user->date+$user->lifetime<time()){
                    self::onClose($client_id);
                }
                $wechatMsgUser  =   new WechatMsgUser();
                $msgUser    =    $wechatMsgUser->where(&#39;openid&#39;,&#39;eq&#39;,$user->uid)->where(&#39;type&#39;,&#39;eq&#39;,0)->find();
                if(!$msgUser->id){
                    self::onClose($client_id);
                }
                //*客服上线,设置数据库状态字段为在线状态
                $msgUser->status    =   1;
                $msgUser->save();
                $uid    =   $msgUser->id;
                // 设置session,这个$_SESSION我是为了下面的onclose方法里设置客服离线状态
                $_SESSION = [
                    &#39;id&#39;       => $uid,
                ];
                // 将当前$client_id与uid绑定
                Gateway::bindUid($client_id, $uid);
                $msgService = new MsgService();
                $msgService->checkLeavedMessage($uid);
                return;
                break;
            case &#39;ping&#39;:
                $pingData=[
                    &#39;emit&#39;=>&#39;pong&#39;,
                    &#39;data&#39;=>$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn