ChatHandle.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Chat 消息处理类
  4. // +----------------------------------------------------------------------
  5. namespace app\services\workerman\chat;
  6. use app\services\workerman\Response;
  7. use Workerman\Connection\TcpConnection;
  8. class ChatHandle
  9. {
  10. protected $service;
  11. public function __construct(ChatService &$service)
  12. {
  13. $this->service = &$service;
  14. }
  15. /**
  16. * 客服登录
  17. */
  18. public function kefu_login(TcpConnection &$connection, array $res, Response $response)
  19. {
  20. if (!isset($res['data']) || !$token = $res['data']) {
  21. return $response->close(['msg' => '授权失败!']);
  22. }
  23. // TODO: 根据你的系统实现客服token验证
  24. // 示例:$kefuInfo = your_kefu_auth_service::parseToken($token);
  25. // 临时示例
  26. $kefuInfo = [
  27. 'uid' => $res['data']['uid'] ?? 1,
  28. 'name' => $res['data']['name'] ?? '客服',
  29. ];
  30. $connection->kefuUser = (object)$kefuInfo;
  31. $connection->user = (object)['uid' => $kefuInfo['uid'], 'nickname' => $kefuInfo['name'] ?? '客服'];
  32. $this->service->setKefuUser($connection);
  33. return $response->success();
  34. }
  35. /**
  36. * 用户登录
  37. */
  38. public function login(TcpConnection &$connection, array $res, Response $response)
  39. {
  40. if (!isset($res['data']) || !$token = $res['data']) {
  41. return $response->close(['msg' => '授权失败!']);
  42. }
  43. // TODO: 根据你的系统实现用户token验证
  44. // 示例:$authInfo = your_user_auth_service::parseToken($token);
  45. // 临时示例
  46. $userInfo = [
  47. 'uid' => $res['data']['uid'] ?? 1,
  48. 'nickname' => $res['data']['nickname'] ?? '用户',
  49. 'avatar' => $res['data']['avatar'] ?? '',
  50. ];
  51. $connection->user = (object)$userInfo;
  52. // 从 URL 参数获取聊天对象 UID(在 socket.php onWebSocketConnect 中设置)
  53. $connection->chatToUid = $connection->to_uid ?? 0;
  54. $this->service->setUser($connection);
  55. return $response->success([
  56. 'msg' => '登录成功',
  57. 'to_uid' => $connection->chatToUid, // 返回当前聊天对象
  58. ]);
  59. }
  60. /**
  61. * 发送消息
  62. */
  63. public function chat(TcpConnection &$connection, array $res, Response $response)
  64. {
  65. // 优先从消息数据获取 to_uid,其次从 URL 参数获取(聊天对象UID)
  66. $to_uid = $res['data']['to_uid'] ?? $connection->chatToUid ?? 0;
  67. $msn = $res['data']['msn'] ?? '';
  68. $msn_type = $res['data']['type'] ?? 1; // 1=文字, 2=语音, 3=图片
  69. if (!$to_uid) {
  70. return $response->send('err_tip', ['msg' => '用户不存在']);
  71. }
  72. if ($to_uid == ($connection->user->uid ?? 0)) {
  73. return $response->send('err_tip', ['msg' => '不能和自己聊天']);
  74. }
  75. $uid = $connection->user->uid ?? 0;
  76. $data = [
  77. 'from_uid' => $uid,
  78. 'to_uid' => $to_uid,
  79. 'msn' => $msn,
  80. 'type' => $msn_type,
  81. 'add_time' => time(),
  82. 'nickname' => $connection->user->nickname ?? '用户',
  83. 'avatar' => $connection->user->avatar ?? '',
  84. ];
  85. // 给自己回复
  86. $response->send('chat', $data);
  87. // 发送给目标用户
  88. if (isset($this->service->user()[$to_uid])) {
  89. $response->connection($this->service->user()[$to_uid])->send('reply', $data);
  90. }
  91. }
  92. }