Login.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\merchant\system\admin;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\merchant\MerchantAdminRepository;
  14. use app\validate\admin\LoginValidate;
  15. use crmeb\services\SwooleTaskService;
  16. use Gregwar\Captcha\CaptchaBuilder;
  17. use Gregwar\Captcha\PhraseBuilder;
  18. use think\App;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\facade\Cache;
  23. class Login extends BaseController
  24. {
  25. protected $repository;
  26. public function __construct(App $app, MerchantAdminRepository $repository)
  27. {
  28. parent::__construct($app);
  29. $this->repository = $repository;
  30. }
  31. /**
  32. * 判断是否需要滑块验证码
  33. * @return \think\response\Json
  34. * @author Qinii
  35. * @day 2022/10/11
  36. */
  37. public function ajCaptchaStatus()
  38. {
  39. $data = $this->request->params(['account']);
  40. $key = 'mer_login_failuree_'.$data['account'];
  41. $numb = (Cache::get($key) ?? 0);
  42. return app('json')->success(['status' => $numb > 2 ]);
  43. }
  44. /**
  45. * 登录方法
  46. *
  47. * @param LoginValidate $validate 登录验证器
  48. * @return \think\response\Json
  49. */
  50. public function login(LoginValidate $validate)
  51. {
  52. $data = $this->request->params(['account', 'password', 'code', 'key', ['captchaType', ''], ['captchaVerification', ''], 'token']);
  53. // 验证请求参数
  54. $validate->check($data);
  55. //图形验证码废弃
  56. // if(Cache::get('mer_login_freeze_'.$data['account']))
  57. // return app('json')->fail('账号或密码错误次数太多,请稍后在尝试');
  58. // $this->repository->checkCode($data['key'], $data['code']);
  59. // 判断登录失败次数
  60. $key = 'mer_login_failuree_' . $data['account'];
  61. $numb = (Cache::get($key) ?? 0);
  62. if ($numb > 2) {
  63. // 如果登录失败次数超过2次,则需要进行滑块验证
  64. if (!$data['captchaType'] || !$data['captchaVerification'])
  65. return app('json')->fail('请滑动滑块验证');
  66. try {
  67. aj_captcha_check_two($data['captchaType'], $data['captchaVerification']);
  68. } catch (\Throwable $e) {
  69. return app('json')->fail($e->getMessage());
  70. }
  71. }
  72. // 调用登录方法
  73. $adminInfo = $this->repository->login($data['account'], $data['password']);
  74. // 创建 token
  75. $tokenInfo = $this->repository->createToken($adminInfo);
  76. // 构造返回数据
  77. $admin = $adminInfo->toArray();
  78. unset($admin['pwd']);
  79. $data = [
  80. 'token' => $tokenInfo['token'],
  81. 'exp' => $tokenInfo['out'],
  82. 'admin' => $admin
  83. ];
  84. // 删除登录失败次数缓存
  85. Cache::delete($key);
  86. // 返回成功结果
  87. return app('json')->success($data);
  88. }
  89. /**
  90. * 退出登录方法
  91. *
  92. * @return \Illuminate\Http\JsonResponse 返回 JSON 格式的退出登录结果
  93. */
  94. public function logout()
  95. {
  96. // 判断当前请求是否已经登录
  97. if ($this->request->isLogin())
  98. // 清除当前请求的 token
  99. $this->repository->clearToken($this->request->token());
  100. // 返回 JSON 格式的退出登录结果
  101. return app('json')->success('退出登录');
  102. }
  103. /**
  104. * 获取验证码
  105. *
  106. * @return \Illuminate\Http\JsonResponse
  107. */
  108. public function getCaptcha()
  109. {
  110. // 创建一个验证码构建器实例
  111. $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));
  112. // 生成登录密钥
  113. $key = $this->repository->createLoginKey($codeBuilder->getPhrase());
  114. // 生成验证码图片
  115. $captcha = $codeBuilder->build()->inline();
  116. // 返回 JSON 格式的成功响应,包含登录密钥和验证码图片
  117. return app('json')->success(compact('key', 'captcha'));
  118. }
  119. }