UserHandler.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\message\service\StoreServiceRecordServices;
  13. use app\services\user\UserAuthServices;
  14. use app\webscoket\BaseHandler;
  15. use app\webscoket\Manager;
  16. use app\webscoket\Response;
  17. use crmeb\exceptions\AuthException;
  18. /**
  19. * Class UserHandler
  20. * @package app\webscoket\handler
  21. */
  22. class UserHandler extends BaseHandler
  23. {
  24. /**
  25. * 用户登陆
  26. * @param array $data
  27. * @param Response $response
  28. * @return bool|mixed|\think\response\Json|null
  29. */
  30. public function login(array $data, Response $response)
  31. {
  32. // 游客登陆
  33. if (isset($data['tourist']) && $data['tourist']) {
  34. return $response->success();
  35. }
  36. if (!isset($data['token']) || !$token = $data['token']) {
  37. return $response->fail('授权失败!');
  38. }
  39. try {
  40. /** @var UserAuthServices $services */
  41. $services = app()->make(UserAuthServices::class);
  42. $authInfo = $services->parseToken($token);
  43. } catch (AuthException $e) {
  44. return $response->fail($e->getMessage());
  45. }
  46. $user = $authInfo['user'];
  47. /** @var StoreServiceRecordServices $service */
  48. $service = app()->make(StoreServiceRecordServices::class);
  49. $service->updateRecord(['to_uid' => $user->uid], ['online' => 1, 'type' => $res['form_type'] ?? 1]);
  50. //给所有在线客服人员发送当前用户上线消息
  51. $this->manager->pushing($this->manager->userFd(Manager::KEFU_TYPE_NUM), $response->message('user_online', [
  52. 'uid' => $user->uid,
  53. 'online' => 1
  54. ])->getData(), $this->fd);
  55. return $response->success('login', $user->toArray());
  56. }
  57. }