Login.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\admin;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\admin\AdminRepository;
  14. use app\validate\admin\LoginValidate;
  15. use Gregwar\Captcha\CaptchaBuilder;
  16. use Gregwar\Captcha\PhraseBuilder;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\facade\Cache;
  22. /**
  23. * 管理员登录
  24. */
  25. class Login extends BaseController
  26. {
  27. protected $repository;
  28. public function __construct(App $app, AdminRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * 判断是否需要滑块验证码
  35. * @return \think\response\Json
  36. * @author Qinii
  37. * @day 2022/10/11
  38. */
  39. public function ajCaptchaStatus()
  40. {
  41. $data = $this->request->params(['account']);
  42. $key = 'sys_login_failuree_' . $data['account'];
  43. $numb = (Cache::get($key) ?? 0);
  44. return app('json')->success(['status' => $numb > 2]);
  45. }
  46. /**
  47. * 登陆
  48. * @param LoginValidate $validate
  49. * @return mixed
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. * @author xaboy
  54. * @day 2020-04-10
  55. */
  56. public function login(LoginValidate $validate)
  57. {
  58. $data = $this->request->params(['account', 'password', 'code', 'key', ['captchaType', ''], ['captchaVerification', ''], 'token']);
  59. $validate->check($data);
  60. //图形验证码废弃
  61. #$this->repository->checkCode($data['key'], $data['code']);
  62. $key = 'sys_login_failuree_' . $data['account'];
  63. $numb = Cache::get($key) ?? 0;
  64. if ($numb > 2) {
  65. if (!$data['captchaType'] || !$data['captchaVerification']) {
  66. return app('json')->fail('请滑动滑块验证');
  67. }
  68. try {
  69. aj_captcha_check_two($data['captchaType'], $data['captchaVerification']);
  70. } catch (\Throwable $e) {
  71. return app('json')->fail($e->getMessage());
  72. }
  73. }
  74. $adminInfo = $this->repository->login($data['account'], $data['password']);
  75. $tokenInfo = $this->repository->createToken($adminInfo);
  76. $admin = $adminInfo->toArray();
  77. unset($admin['pwd']);
  78. $data = [
  79. 'token' => $tokenInfo['token'],
  80. 'exp' => $tokenInfo['out'],
  81. 'admin' => $admin
  82. ];
  83. Cache::delete($key);
  84. return app('json')->success($data);
  85. }
  86. /**
  87. * 推出登陆
  88. * @return mixed
  89. * @author xaboy
  90. * @day 2020-04-10
  91. */
  92. public function logout()
  93. {
  94. if ($this->request->isLogin())
  95. $this->repository->clearToken($this->request->token());
  96. return app('json')->success('退出登录');
  97. }
  98. /**
  99. * 获取验证码
  100. * @return mixed
  101. * @author xaboy
  102. * @day 2020-04-09
  103. */
  104. public function getCaptcha()
  105. {
  106. $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));
  107. $key = $this->repository->createLoginKey($codeBuilder->getPhrase());
  108. $captcha = $codeBuilder->build()->inline();
  109. return app('json')->success(compact('key', 'captcha'));
  110. }
  111. }