WorkermanHandle.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\workerman;
  12. use app\services\system\admin\AdminAuthServices;
  13. use crmeb\exceptions\AuthException;
  14. use Workerman\Connection\TcpConnection;
  15. class WorkermanHandle
  16. {
  17. protected $service;
  18. public function __construct(WorkermanService &$service)
  19. {
  20. $this->service = &$service;
  21. }
  22. public function login(TcpConnection &$connection, array $res, Response $response)
  23. {
  24. if (!isset($res['data']) || !$token = $res['data']) {
  25. return $response->close([
  26. 'msg' => '授权失败!'
  27. ]);
  28. }
  29. try {
  30. /** @var AdminAuthServices $adminAuthService */
  31. $adminAuthService = app()->make(AdminAuthServices::class);
  32. $authInfo = $adminAuthService->parseToken($token);
  33. } catch (AuthException $e) {
  34. return $response->close([
  35. 'msg' => $e->getMessage(),
  36. 'code' => $e->getCode()
  37. ]);
  38. }
  39. if (!$authInfo || !isset($authInfo['id'])) {
  40. return $response->close([
  41. 'msg' => '授权失败!'
  42. ]);
  43. }
  44. $connection->adminInfo = $authInfo;
  45. $connection->adminId = $authInfo['id'];
  46. $this->service->setUser($connection);
  47. return $response->success();
  48. }
  49. }