ChatHandle.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // 检查用户是否已登录
  66. if (!isset($connection->user) || !$connection->user) {
  67. return $response->send('err_tip', ['msg' => '请先登录']);
  68. }
  69. // 优先从消息数据获取 to_uid,其次从 URL 参数获取(聊天对象UID)
  70. $to_uid = $res['data']['to_uid'] ?? $connection->chatToUid ?? 0;
  71. $msn = $res['data']['msn'] ?? '';
  72. $msn_type = $res['data']['type'] ?? 1; // 1=文字, 2=语音, 3=图片
  73. if (!$to_uid) {
  74. return $response->send('err_tip', ['msg' => '用户不存在']);
  75. }
  76. if ($to_uid == ($connection->user->uid ?? 0)) {
  77. return $response->send('err_tip', ['msg' => '不能和自己聊天']);
  78. }
  79. $uid = $connection->user->uid ?? 0;
  80. $data = [
  81. 'from_uid' => $uid,
  82. 'to_uid' => $to_uid,
  83. 'msn' => $msn,
  84. 'type' => $msn_type,
  85. 'add_time' => time(),
  86. 'nickname' => $connection->user->nickname ?? '用户',
  87. 'avatar' => $connection->user->avatar ?? '',
  88. ];
  89. // 给自己回复
  90. $response->send('chat', $data);
  91. // 发送给目标用户
  92. if (isset($this->service->user()[$to_uid])) {
  93. $response->connection($this->service->user()[$to_uid])->send('reply', $data);
  94. }
  95. }
  96. }