Login.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\controller\service;
  3. use app\common\repositories\store\service\StoreServiceRepository;
  4. use crmeb\basic\BaseController;
  5. use Gregwar\Captcha\CaptchaBuilder;
  6. use Gregwar\Captcha\PhraseBuilder;
  7. use think\facade\Cache;
  8. class Login extends BaseController
  9. {
  10. /**
  11. * 扫码登录
  12. * @return \think\response\Json
  13. * @throws \Exception
  14. * @author wuhaotian
  15. * @email 442384644@qq.com
  16. * @date 2024/7/8
  17. */
  18. public function scanLogin()
  19. {
  20. $uni = uniqid(true, false) . random_int(1, 100000000);
  21. $key = 'S' . md5(time() . $uni);
  22. $siteUrl = rtrim(systemConfig('site_url'), '/');
  23. $timeout = 600;
  24. Cache::set('_scan_ser_login' . $key, 0, $timeout);
  25. return app('json')->success(['timeout' => $timeout, 'key' => $key, 'qrcode' => $siteUrl . '/pages/chat/customer_login/index?key=' . $key]);
  26. }
  27. /**
  28. * 验证扫码
  29. * @return \think\response\Json
  30. * @author wuhaotian
  31. * @email 442384644@qq.com
  32. * @date 2024/7/8
  33. */
  34. public function checkScanLogin()
  35. {
  36. $key = (string)$this->request->param('key');
  37. if ($key) {
  38. $uid = Cache::get('_scan_ser_login' . $key);
  39. if ($uid) {
  40. Cache::delete('_scan_ser_login' . $key);
  41. $repository = app()->make(StoreServiceRepository::class);
  42. $user = $repository->get($uid);
  43. if (!$user) {
  44. return app('json')->status(400, '登录失败');
  45. }
  46. if (!$user['is_open'])
  47. return app('json')->status(400, '登录失败');
  48. if (!$user['status'])
  49. return app('json')->status(400, '登录失败');
  50. $tokenInfo = $repository->createToken($user);
  51. $user = $user->toArray();
  52. unset($user['pwd']);
  53. $data = [
  54. 'token' => $tokenInfo['token'],
  55. 'exp' => $tokenInfo['out'],
  56. 'admin' => $user
  57. ];
  58. return app('json')->status(200, $data);
  59. }
  60. }
  61. return app('json')->status(201, '未登录');
  62. }
  63. /**
  64. * 登录
  65. * @param StoreServiceRepository $repository
  66. * @return \think\response\Json
  67. * @author wuhaotian
  68. * @email 442384644@qq.com
  69. * @date 2024/7/8
  70. */
  71. public function login(StoreServiceRepository $repository)
  72. {
  73. $data = $this->request->params(['account', 'password', 'key', 'code']);
  74. if (Cache::get('ser_login_freeze_' . $data['account']))
  75. return app('json')->fail('账号或密码错误次数太多,请稍后在尝试');
  76. $repository->checkCode($data['key'], $data['code']);
  77. $service = $repository->getWhere(['account' => $data['account'], 'is_del' => 0]);
  78. if (!$service) {
  79. return app('json')->fail('账号不存在');
  80. }
  81. if (!$service['is_open'])
  82. return app('json')->fail('账号未开启');
  83. if (!$service['status'])
  84. return app('json')->fail('账号已被禁用');
  85. if (!password_verify($data['password'], $service['pwd'])) {
  86. return $this->loginFailure($data['account']);
  87. }
  88. $tokenInfo = $repository->createToken($service);
  89. $admin = $service->toArray();
  90. unset($admin['pwd']);
  91. $data = [
  92. 'token' => $tokenInfo['token'],
  93. 'exp' => $tokenInfo['out'],
  94. 'admin' => $admin
  95. ];
  96. return app('json')->success($data);
  97. }
  98. /**
  99. * 退出登录
  100. * @param StoreServiceRepository $repository
  101. * @return \think\response\Json
  102. * @author wuhaotian
  103. * @email 442384644@qq.com
  104. * @date 2024/7/8
  105. */
  106. public function logout(StoreServiceRepository $repository)
  107. {
  108. if ($this->request->isLogin())
  109. $repository->clearToken($this->request->token());
  110. return app('json')->success('退出登录');
  111. }
  112. /**
  113. * 获取验证码
  114. * @return mixed
  115. * @author xaboy
  116. * @day 2020-04-09
  117. */
  118. public function getCaptcha(StoreServiceRepository $repository)
  119. {
  120. $codeBuilder = new CaptchaBuilder(null, new PhraseBuilder(4));
  121. $key = $repository->createLoginKey($codeBuilder->getPhrase());
  122. $captcha = $codeBuilder->build()->inline();
  123. return app('json')->success(compact('key', 'captcha'));
  124. }
  125. /**
  126. * 登录尝试次数限制
  127. * @param $account
  128. * @param int $number
  129. * @param int $n
  130. * @author Qinii
  131. * @day 7/6/21
  132. */
  133. public function loginFailure($account, $number = 5, $n = 3)
  134. {
  135. $key = 'ser_login_failuree_' . $account;
  136. $numb = Cache::get($key) ?? 0;
  137. $numb++;
  138. if ($numb >= $number) {
  139. $fail_key = 'ser_login_freeze_' . $account;
  140. Cache::set($fail_key, 1, 15 * 60);
  141. return app('json')->fail('账号或密码错误次数太多,请稍后在尝试');
  142. }
  143. Cache::set($key, $numb, 5 * 60);
  144. $msg = '账号或密码错误';
  145. $_n = $number - $numb;
  146. if ($_n <= $n) {
  147. $msg .= ',还可尝试' . $_n . '次';
  148. }
  149. return app('json')->fail($msg);
  150. }
  151. }