ChatHandle.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace crmeb\services\workerman\chat;
  3. use app\models\store\StoreServiceLog;
  4. use app\models\user\User;
  5. use app\models\user\WechatUser;
  6. use crmeb\exceptions\AuthException;
  7. use crmeb\repositories\UserRepository;
  8. use crmeb\services\SystemConfigService;
  9. use crmeb\services\WechatService;
  10. use crmeb\services\workerman\ChannelService;
  11. use crmeb\services\workerman\Response;
  12. use think\facade\Log;
  13. use think\facade\Route as Url;
  14. use think\facade\Session;
  15. use Workerman\Connection\TcpConnection;
  16. class ChatHandle
  17. {
  18. protected $service;
  19. public function __construct(ChatService &$service)
  20. {
  21. $this->service = &$service;
  22. }
  23. public function login(TcpConnection &$connection, array $res, Response $response)
  24. {
  25. if (!isset($res['data']) || !$token = $res['data']) {
  26. return $response->close([
  27. 'msg' => '授权失败!'
  28. ]);
  29. }
  30. try {
  31. $authInfo = UserRepository::parseToken($token);
  32. } catch (AuthException $e) {
  33. return $response->close([
  34. 'msg' => $e->getMessage()
  35. ]);
  36. }
  37. $connection->user = $authInfo['user'];
  38. $connection->tokenData = $authInfo['tokenData'];
  39. $this->service->setUser($connection);
  40. return $response->success();
  41. }
  42. public function to_chat(TcpConnection &$connection, array $res)
  43. {
  44. $connection->chatToUid = $res['data']['id'] ?? 0;
  45. }
  46. public function chat(TcpConnection &$connection, array $res, Response $response)
  47. {
  48. $to_uid = $res['data']['to_uid'] ?? 0;
  49. $msn_type = $res['data']['type'] ?? 0;
  50. $msn = $res['data']['msn'] ?? '';
  51. $uid = $connection->user->uid;
  52. if (!$to_uid) return $response->send('err_tip', ['msg' => '用户不存在']);
  53. if ($to_uid == $uid) return $response->send('err_tip', ['msg' => '不能和自己聊天']);
  54. if (!in_array($msn_type, [1, 2, 3, 4])) return $response->send('err_tip', ['msg' => '格式错误']);
  55. $msn = htmlspecialchars($msn);
  56. $data = compact('to_uid', 'msn_type', 'msn', 'uid');
  57. $data['add_time'] = time();
  58. $connections = $this->service->user();
  59. $online = isset($connections[$to_uid]) && isset($connections[$to_uid]->chatToUid) && $connections[$to_uid]->chatToUid == $uid;
  60. $data['type'] = $online ? 1 : 0;
  61. StoreServiceLog::create($data);
  62. $_userInfo = User::getUserInfo($data['uid'], 'nickname,avatar');
  63. $data['nickname'] = $_userInfo['nickname'];
  64. $data['avatar'] = $_userInfo['avatar'];
  65. $response->send('chat', $data);
  66. if ($online) {
  67. $response->connection($this->service->user()[$to_uid])->send('reply', $data);
  68. } else {
  69. $userInfo = WechatUser::where('uid', $to_uid)->field('nickname,subscribe,openid,headimgurl')->find();
  70. if ($userInfo && $userInfo['subscribe'] && $userInfo['openid']) {
  71. $head = '客服提醒';
  72. $description = '您有新的消息,请注意查收!';
  73. $url = SystemConfigService::get('site_url') . '/customer/chat/' . $uid;
  74. $message = WechatService::newsMessage($head, $description, $url, $_userInfo['avatar']);
  75. $userInfo = $userInfo->toArray();
  76. try {
  77. WechatService::staffService()->message($message)->to($userInfo['openid'])->send();
  78. } catch (\Exception $e) {
  79. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  80. }
  81. }
  82. }
  83. }
  84. }