ChatHandle.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $this->service->setUser($connection);
  53. return $response->success();
  54. }
  55. /**
  56. * 发送消息
  57. */
  58. public function chat(TcpConnection &$connection, array $res, Response $response)
  59. {
  60. $to_uid = $res['data']['to_uid'] ?? 0;
  61. $msn = $res['data']['msn'] ?? '';
  62. $msn_type = $res['data']['type'] ?? 1; // 1=文本, 2=图片, 3=语音
  63. if (!$to_uid) {
  64. return $response->send('err_tip', ['msg' => '用户不存在']);
  65. }
  66. if ($to_uid == ($connection->user->uid ?? 0)) {
  67. return $response->send('err_tip', ['msg' => '不能和自己聊天']);
  68. }
  69. $uid = $connection->user->uid ?? 0;
  70. $data = [
  71. 'from_uid' => $uid,
  72. 'to_uid' => $to_uid,
  73. 'msn' => $msn,
  74. 'type' => $msn_type,
  75. 'add_time' => time(),
  76. 'nickname' => $connection->user->nickname ?? '用户',
  77. 'avatar' => $connection->user->avatar ?? '',
  78. ];
  79. // 给自己回复
  80. $response->send('chat', $data);
  81. // 发送给目标用户
  82. if (isset($this->service->user()[$to_uid])) {
  83. $this->response->connection($this->service->user()[$to_uid])->send('reply', $data);
  84. }
  85. }
  86. }