AdminHandler.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-29
  7. *
  8. *
  9. */
  10. namespace app\webscoket\handler;
  11. use app\common\repositories\system\admin\AdminRepository;
  12. use crmeb\services\JwtTokenService;
  13. use Swoole\Server;
  14. use think\db\exception\DataNotFoundException;
  15. use think\db\exception\DbException;
  16. use think\db\exception\ModelNotFoundException;
  17. use Throwable;
  18. /**
  19. * Class Handler
  20. * @package app\webscoket
  21. * @author xaboy
  22. * @day 2020-04-29
  23. */
  24. class AdminHandler
  25. {
  26. /**
  27. * @var Server
  28. */
  29. protected $server;
  30. /**
  31. * AdminHandler constructor.
  32. * @param Server $server
  33. */
  34. public function __construct(Server $server)
  35. {
  36. $this->server = $server;
  37. }
  38. /**
  39. * @param array $data
  40. * @return mixed
  41. * @author xaboy
  42. * @day 2020-05-06
  43. */
  44. public function test(array $data)
  45. {
  46. return app('json')->success($data);
  47. }
  48. /**
  49. * @param array $data
  50. * @return mixed
  51. * @throws DataNotFoundException
  52. * @throws DbException
  53. * @throws ModelNotFoundException
  54. * @author xaboy
  55. * @day 2020-05-06
  56. */
  57. public function login(array $data)
  58. {
  59. $token = $data['token'] ?? '';
  60. if (!$token) return app('json')->fail('token 无效');
  61. /**
  62. * @var AdminRepository $repository
  63. */
  64. $repository = app()->make(AdminRepository::class);
  65. $service = new JwtTokenService();
  66. try {
  67. $payload = $service->parseToken($token);
  68. } catch (Throwable $e) {//Token 过期
  69. return app('json')->fail('token 已过期');
  70. }
  71. if ('admin' != $payload->jti[1])
  72. return app('json')->fail('无效的 token');
  73. $admin = $repository->get($payload->jti[0]);
  74. if (!$admin)
  75. return app('json')->fail('账号不存在');
  76. if (!$admin['status'])
  77. return app('json')->fail('账号已被禁用');
  78. return app('json')->success(['uid' => $admin->admin_id, 'data' => $admin->toArray()]);
  79. }
  80. }