MerchantHandler.php 2.5 KB

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