MerchantHandler.php 3.1 KB

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