Login.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\controller\merchant\system\admin;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\system\merchant\MerchantAdminRepository;
  5. use app\validate\admin\LoginValidate;
  6. use ln\services\SwooleTaskService;
  7. use Gregwar\Captcha\CaptchaBuilder;
  8. use Gregwar\Captcha\PhraseBuilder;
  9. use think\App;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\facade\Cache;
  14. class Login extends BaseController
  15. {
  16. protected $repository;
  17. public function __construct(App $app, MerchantAdminRepository $repository)
  18. {
  19. parent::__construct($app);
  20. $this->repository = $repository;
  21. }
  22. /**
  23. * @param LoginValidate $validate
  24. * @return mixed
  25. * @throws DataNotFoundException
  26. * @throws DbException
  27. * @throws ModelNotFoundException
  28. * @author zfy
  29. * @day 2020-04-10
  30. */
  31. public function login(LoginValidate $validate)
  32. {
  33. $data = $this->request->params(['account', 'password', 'code', 'key']);
  34. $validate->check($data);
  35. if(Cache::get('mer_login_freeze_'.$data['account']))
  36. return app('json')->fail('账号或密码错误次数太多,请稍后在尝试');
  37. $this->repository->checkCode($data['key'], $data['code']);
  38. $adminInfo = $this->repository->login($data['account'], $data['password']);
  39. $tokenInfo = $this->repository->createToken($adminInfo);
  40. $admin = $adminInfo->toArray();
  41. unset($admin['pwd']);
  42. $data = [
  43. 'token' => $tokenInfo['token'],
  44. 'exp' => $tokenInfo['out'],
  45. 'admin' => $admin
  46. ];
  47. return app('json')->success($data);
  48. }
  49. /**
  50. * @return mixed
  51. * @author zfy
  52. * @day 2020-04-10
  53. */
  54. public function logout()
  55. {
  56. if ($this->request->isLogin())
  57. $this->repository->clearToken($this->request->token());
  58. return app('json')->success('退出登录');
  59. }
  60. /**
  61. * @return mixed
  62. * @author zfy
  63. * @day 2020-04-09
  64. */
  65. public function getCaptcha()
  66. {
  67. $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));
  68. $key = $this->repository->createLoginKey($codeBuilder->getPhrase());
  69. $captcha = $codeBuilder->build()->inline();
  70. return app('json')->success(compact('key', 'captcha'));
  71. }
  72. }