KefuHandler.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\webscoket\handler;
  12. use app\services\kefu\LoginServices;
  13. use app\services\message\service\StoreServiceRecordServices;
  14. use app\services\message\service\StoreServiceServices;
  15. use app\services\user\UserServices;
  16. use app\webscoket\BaseHandler;
  17. use app\webscoket\Manager;
  18. use app\webscoket\Response;
  19. use crmeb\exceptions\AuthException;
  20. /**
  21. * Class KefuHandler
  22. * @package app\webscoket\handler
  23. */
  24. class KefuHandler extends BaseHandler
  25. {
  26. /**
  27. * 客服登录
  28. * @param array $data
  29. * @param Response $response
  30. * @return bool|mixed|\think\response\Json|null
  31. * @throws \Psr\SimpleCache\InvalidArgumentException
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function login(array $data, Response $response)
  37. {
  38. if (!isset($data['token']) || !$token = $data['token']) {
  39. return $response->fail('授权失败!');
  40. }
  41. try {
  42. /** @var LoginServices $services */
  43. $services = app()->make(LoginServices::class);
  44. $kefuInfo = $services->parseToken($token);
  45. } catch (AuthException $e) {
  46. return $response->fail($e->getMessage());
  47. }
  48. /** @var UserServices $userService */
  49. $userService = app()->make(UserServices::class);
  50. $user = $userService->get($kefuInfo['uid'], ['uid', 'nickname']);
  51. if (!isset($user['uid'])) {
  52. return $response->fail('您登录的客服用户不存在');
  53. }
  54. /** @var StoreServiceRecordServices $service */
  55. $service = app()->make(StoreServiceRecordServices::class);
  56. $service->updateRecord(['to_uid' => $user['uid']], ['online' => 1]);
  57. /** @var StoreServiceServices $service */
  58. $service = app()->make(StoreServiceServices::class);
  59. $service->update(['uid' => $user['uid']], ['online' => 1]);
  60. return $response->success($user->toArray());
  61. }
  62. /**
  63. * 兼容前端方法
  64. * @param array $data
  65. * @param Response $response
  66. */
  67. public function kefu_login(array $data = [], Response $response)
  68. {
  69. return $this->login($data, $response);
  70. }
  71. /**
  72. * 上下线
  73. * @param array $data
  74. * @param Response $response
  75. */
  76. public function online(array $data = [], Response $response)
  77. {
  78. $online = $data['online'] ?? 0;
  79. $user = $this->room->get($this->fd);
  80. if ($user) {
  81. /** @var StoreServiceServices $service */
  82. $service = app()->make(StoreServiceServices::class);
  83. $service->update(['uid' => $user['uid']], ['online' => $online]);
  84. if ($user['to_uid']) {
  85. $fd = $this->manager->userFd('', $user['to_uid']);
  86. //给当前正在聊天的用户发送上下线消息
  87. $this->manager->pushing($fd, $response->message('online', [
  88. 'online' => $online,
  89. 'uid' => $user['uid']
  90. ])->getData());
  91. }
  92. }
  93. }
  94. /**
  95. * 客服转接
  96. * @param array $data
  97. * @param Response $response
  98. * @return \think\response\Json
  99. */
  100. public function transfer(array $data, Response $response)
  101. {
  102. $data = $data['data'] ?? [];
  103. $uid = $data['recored']['uid'] ?? 0;
  104. if ($uid && $this->manager->userFd('', $uid)) {
  105. $data['recored']['online'] = 1;
  106. } else {
  107. $data['recored']['online'] = 0;
  108. }
  109. return $response->message('transfer', $data);
  110. }
  111. /**
  112. * 退出登录
  113. * @param array $data
  114. * @param Response $response
  115. */
  116. public function logout(array $data = [], Response $response)
  117. {
  118. $user = $this->room->get($this->fd);
  119. $uid = $user['uid'] ?? 0;
  120. if ($uid) {
  121. /** @var StoreServiceServices $service */
  122. $service = app()->make(StoreServiceServices::class);
  123. $service->update(['uid' => $user['uid']], ['online' => 0]);
  124. /** @var StoreServiceRecordServices $service */
  125. $service = app()->make(StoreServiceRecordServices::class);
  126. $service->updateRecord(['to_uid' => $uid], ['online' => 0]);
  127. $this->manager->pushing($this->manager->userFd(Manager::KEFU_TYPE_NUM), $response->message('online', [
  128. 'online' => 0,
  129. 'uid' => $uid
  130. ]), $this->fd);
  131. }
  132. }
  133. }