feat: 初始化雄戈商城服务端代码与Webhook自动部署配置
这个提交包含在:
@@ -0,0 +1,267 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\chat\traits;
|
||||
|
||||
use GatewayWorker\Lib\Gateway;
|
||||
use addons\shopro\library\chat\Online;
|
||||
use addons\shopro\model\chat\User as ChatUser;
|
||||
|
||||
/**
|
||||
* 获取各种连接者身份列表
|
||||
*/
|
||||
trait GetLinkerTrait
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取所有在线客服的 client_ids
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineCustomerServiceClientIds() {
|
||||
$onlineCustomerServiceClientIds = Gateway::getClientIdListByGroup(Online::getGrouponName('online_customer_service'));
|
||||
|
||||
return $onlineCustomerServiceClientIds ? : [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有在线客服 的sessions,一个客服多浏览器登录会重复,因为是session这里都保留
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineCustomerServiceSessions()
|
||||
{
|
||||
// 获取所有在线客服 session
|
||||
$customerServiceUserSessions = Gateway::getClientSessionsByGroup(Online::getGrouponName('online_customer_service'));
|
||||
|
||||
return $customerServiceUserSessions ? : [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有在线客服的信息,自动过滤重复的信息,因为一个客服可以多浏览器登录
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineCustomerServices() {
|
||||
$customerServiceSessions = self::onlineCustomerServiceSessions();
|
||||
$customerServices = array_column($customerServiceSessions, 'user');
|
||||
|
||||
// 过滤重复
|
||||
$newServices = [];
|
||||
foreach ($customerServices as $customerService) {
|
||||
$newServices[$customerService['id']] = $customerService;
|
||||
}
|
||||
|
||||
return array_values($newServices);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过 客服 id 获取客服 session, 一个客服多浏览器登录会重复,因为是session这里都保留,当前的客服的session 是老的,不会更新
|
||||
*
|
||||
* @param int $customer_service_id
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineCustomerServiceSessionById($customer_service_id)
|
||||
{
|
||||
$client_ids = Gateway::getClientIdByUid(Online::getUId($customer_service_id, 'customer_service'));
|
||||
|
||||
$customerServiceSessions = [];
|
||||
// 当前客服绑定的所有session
|
||||
foreach ($client_ids as $client_id) {
|
||||
$customerServiceSessions[] = Gateway::getSession($client_id);
|
||||
}
|
||||
|
||||
return $customerServiceSessions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过 客服 id 获取客服 session, 一个客服多浏览器登录会重复,因为是session这里都保留,通过 client_id 判断并拿到当前客服最新session
|
||||
*
|
||||
* @param int $customer_service_id
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineCustomerServiceNewSessionById($customer_service_id, $current_client_id)
|
||||
{
|
||||
$client_ids = Gateway::getClientIdByUid(Online::getUId($customer_service_id, 'customer_service'));
|
||||
|
||||
$customerServiceSessions = [];
|
||||
// 当前客服绑定的所有session
|
||||
foreach ($client_ids as $client_id) {
|
||||
if ($client_id == $current_client_id) {
|
||||
$customerServiceSession = $_SESSION;
|
||||
} else {
|
||||
// 如果不是自己
|
||||
$customerServiceSession = Gateway::getSession($client_id);
|
||||
}
|
||||
|
||||
$customerServiceSessions[] = $customerServiceSession;
|
||||
}
|
||||
|
||||
return $customerServiceSessions;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过客服 id 获取 客服信息
|
||||
*
|
||||
* @param int $customer_service_id
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineCustomerServiceById($customer_service_id)
|
||||
{
|
||||
// 客服多浏览器登录的时候是多个 session
|
||||
$customerServiceSessions = self::onlineCustomerServiceSessionById($customer_service_id);
|
||||
|
||||
// 只需要获取第一条的 user 信息
|
||||
$customerService = (isset($customerServiceSessions[0]) && isset($customerServiceSessions[0]['user'])) ? $customerServiceSessions[0]['user'] : [];
|
||||
|
||||
return $customerService;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 当前正在等待接入的用户 session
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineWaitingUserSessions () {
|
||||
$onlineWaitingUserSessions = Gateway::getClientSessionsByGroup(Online::getGrouponName('online_waiting'));
|
||||
|
||||
return $onlineWaitingUserSessions ? : [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 当前正在等待接入的用户
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function onlineWaitingUsers() {
|
||||
$waitingSessionUsers = self::onlineWaitingUserSessions();
|
||||
|
||||
$waitings = array_column($waitingSessionUsers, 'chat_user');
|
||||
|
||||
// 过滤重复
|
||||
$waitingUsers = [];
|
||||
foreach ($waitings as $waiting) {
|
||||
$waitingUsers[$waiting['id']] = $waiting;
|
||||
}
|
||||
|
||||
return array_values($waitingUsers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前客服正在服务的用户 session, 这里同一个用户可能会存在两个 session(多端登录)
|
||||
*
|
||||
* @param array $customerService
|
||||
* @return array
|
||||
*/
|
||||
public static function customerServiceUserSessions($customerService) {
|
||||
// 获取当前客服 group_name
|
||||
$customerServiceGroupName = Online::getGrouponName('customer_service_user', ['customer_service_id' => $customerService['id']]);
|
||||
// 获取当前客服正在服务的用户列表 client_id
|
||||
$customerServiceUserSessions = Gateway::getClientSessionsByGroup($customerServiceGroupName);
|
||||
|
||||
return $customerServiceUserSessions ? : [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前客服正在服务的用户 (去重)
|
||||
*
|
||||
* @param array $customerService
|
||||
* @return array
|
||||
*/
|
||||
public static function customerServiceOnlineUsers($customerService) {
|
||||
// 当前客服服务的所有用户 session
|
||||
$customerServiceUserSessions = self::customerServiceUserSessions($customerService);
|
||||
// 在线用户列表
|
||||
$onlines = array_column($customerServiceUserSessions, 'chat_user');
|
||||
|
||||
// 过滤重复
|
||||
$newUsers = [];
|
||||
foreach ($onlines as $online) {
|
||||
$newUsers[$online['id']] = $online;
|
||||
}
|
||||
|
||||
return array_values($newUsers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前客服正在服务的用户 client_ids
|
||||
*
|
||||
* @param array $customerService
|
||||
* @return array
|
||||
*/
|
||||
public static function customerServiceUserClientIds($customerService) {
|
||||
// 获取当前客服 group_name
|
||||
$customerServiceGroupName = Online::getGrouponName('customer_service_user', ['customer_service_id' => $customerService['id']]);
|
||||
// 获取当前客服正在服务的用户列表 client_id
|
||||
$userClientIds = Gateway::getClientIdListByGroup($customerServiceGroupName);
|
||||
|
||||
return $userClientIds ? : [];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前客服服务的历史用户
|
||||
*
|
||||
* @param array $customerService
|
||||
* @param array $exceptSessionIds
|
||||
* @return array
|
||||
*/
|
||||
public static function customerServiceHistoryUsers($customerService, Array $exceptSessionIds = []) {
|
||||
$histories = Online::historyByCustomerServiceId($customerService, ['except' => $exceptSessionIds]);
|
||||
foreach ($histories as $key => &$history) {
|
||||
$status = Gateway::isUidOnline(Online::getUId($history['session_id'], 'user'));
|
||||
$history['status'] = $status; // 在线状态
|
||||
$history['customer_service'] = null; // 如果在线,并且已经接入客服,当前客服信息
|
||||
if ($status) {
|
||||
// 在线,判断用户是否正在被客服接入
|
||||
$client_ids = Gateway::getClientIdByUid(Online::getUId($history['session_id'], 'user'));
|
||||
$client_id = current($client_ids);
|
||||
$historySession = Gateway::getSession($client_id);
|
||||
$customerService = $historySession['customer_service'] ? : null;
|
||||
$history['customer_service'] = $customerService;
|
||||
}
|
||||
$history['status'] = $status;
|
||||
}
|
||||
|
||||
return $histories;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 通过 session_id 获取连接的用户对应的 chatUser
|
||||
*
|
||||
* @param string $session_id
|
||||
* @return object
|
||||
*/
|
||||
public static function getChatUserBySessionId($session_id)
|
||||
{
|
||||
$chatUser = ChatUser::where('session_id', $session_id)->find();
|
||||
return $chatUser;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 通过 user_id 获取连接的用户对应的 chatUser
|
||||
*
|
||||
* @param string $user_id
|
||||
* @return object
|
||||
*/
|
||||
public static function getChatUserByUserId($user_id)
|
||||
{
|
||||
$chatUser = ChatUser::where('user_id', $user_id)->find();
|
||||
return $chatUser;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace addons\shopro\library\chat\traits;
|
||||
|
||||
use GatewayWorker\Lib\Gateway;
|
||||
use addons\shopro\library\chat\Online;
|
||||
|
||||
/**
|
||||
* 通知基础方法
|
||||
*/
|
||||
trait SenderTrait
|
||||
{
|
||||
/**
|
||||
* 可以同时给多个 uid 发送,支持 u_id 是数组
|
||||
*/
|
||||
public static function successById($u_id, array $content)
|
||||
{
|
||||
$result = [
|
||||
'code' => 1,
|
||||
'msg' => '',
|
||||
'type' => '',
|
||||
'data' => null
|
||||
];
|
||||
|
||||
$result = array_merge($result, $content);
|
||||
|
||||
Gateway::sendToUid($u_id, json_encode($result));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public static function successByCustomerServiceId($customer_service_id, array $content)
|
||||
{
|
||||
return self::successById(Online::getUId($customer_service_id, 'customer_service'), $content);
|
||||
}
|
||||
|
||||
|
||||
public static function successBySessionId($session_id, array $content)
|
||||
{
|
||||
return self::successById(Online::getUId($session_id, 'user'), $content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 给一个 client_id 发送消息
|
||||
*/
|
||||
public static function success($client_id, array $content)
|
||||
{
|
||||
$result = [
|
||||
'code' => 1,
|
||||
'msg' => '',
|
||||
'type' => '',
|
||||
'data' => null
|
||||
];
|
||||
|
||||
$result = array_merge($result, $content);
|
||||
Gateway::sendToClient($client_id, json_encode($result));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给所有 client_id 或指定 clientIds 发送
|
||||
*/
|
||||
public static function successAll(array $content, $clientIds = [])
|
||||
{
|
||||
$result = [
|
||||
'code' => 1,
|
||||
'msg' => '',
|
||||
'type' => '',
|
||||
'data' => null
|
||||
];
|
||||
|
||||
$result = array_merge($result, $content);
|
||||
Gateway::sendToAll(json_encode($result), $clientIds);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static function errorById($u_id, array $content)
|
||||
{
|
||||
$result = [
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'type' => '',
|
||||
'data' => null
|
||||
];
|
||||
|
||||
$result = array_merge($result, $content);
|
||||
Gateway::sendToUid($u_id, json_encode($result));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public static function error($client_id, array $content)
|
||||
{
|
||||
$result = [
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'type' => '',
|
||||
'data' => null
|
||||
];
|
||||
|
||||
$result = array_merge($result, $content);
|
||||
Gateway::sendToClient($client_id, json_encode($result));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
public static function __callStatic($name, $arguments)
|
||||
{
|
||||
// 需要存储数据库的消息,先存储数据库,再发送
|
||||
if (strpos($name, 'message') !== false) {
|
||||
// 存库
|
||||
$customerServiceLog = Online::addMessage($name, $arguments);
|
||||
|
||||
// 将 message 追加到 content 里面
|
||||
$content = $arguments[1] ?? [];
|
||||
$content['data'] = $content['data'] ?? [];
|
||||
$content['data']['message'] = $customerServiceLog->toArray();
|
||||
$arguments[1] = $content;
|
||||
|
||||
// 重载方法名
|
||||
$currentName = str_replace('message', 'success', $name);
|
||||
}
|
||||
|
||||
return self::$currentName(...$arguments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
在新工单中引用
屏蔽一个用户