123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?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);
- }
- }
|