Login.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/20
  6. * @time: 11:21
  7. */
  8. namespace app\controller\admin;
  9. use app\common\AdminBaseController;
  10. use app\services\system\admin\SystemAdminServices;
  11. use app\validate\admin\SystemAdminValidate;
  12. use qiniu\services\CacheService;
  13. use qiniu\services\SystemConfigService;
  14. use qiniu\utils\ApiErrorCode;
  15. use qiniu\utils\Captcha;
  16. use think\facade\Cache;
  17. use think\facade\Config;
  18. class Login extends AdminBaseController
  19. {
  20. /**
  21. * 验证码
  22. * @return $this|\think\Response
  23. */
  24. public function captcha()
  25. {
  26. return app()->make(Captcha::class)->create();
  27. }
  28. /**
  29. * 获取验证码
  30. * @return mixed
  31. */
  32. public function getAjCaptcha()
  33. {
  34. [$account,] = $this->request->postMore([
  35. 'account',
  36. ], true);
  37. $key = 'login_captcha_' . $account;
  38. return $this->success(['is_captcha' => Cache::get($key) > 2]);
  39. }
  40. /**
  41. * 获取后台登录页轮播图以及LOGO
  42. * @return mixed
  43. */
  44. public function info()
  45. {
  46. $data = SystemConfigService::more(['site_name','admin_login_slide', 'site_logo_square', 'site_logo', 'login_logo'],true);
  47. return $this->success([
  48. 'slide' => sys_config('admin_login_slide') ?? [],
  49. 'logo_square' => $data['site_logo_square'] ?? '',//透明
  50. 'logo_rectangle' => $data['site_logo'] ?? '',//方形
  51. 'login_logo' => $data['login_logo'] ?? '',//登陆
  52. 'site_name' => $data['site_name'],
  53. 'upload_file_size_max' => config('upload.filesize'),//文件上传大小kb
  54. ]);
  55. }
  56. /**
  57. * @return mixed
  58. */
  59. public function ajcaptcha()
  60. {
  61. $captchaType = $this->request->get('captchaType');
  62. return $this->success(aj_captcha_create($captchaType));
  63. }
  64. /**
  65. * 一次验证
  66. * @return mixed
  67. */
  68. public function ajcheck()
  69. {
  70. [$token, $pointJson, $captchaType] = $this->request->postMore([
  71. ['token', ''],
  72. ['pointJson', ''],
  73. ['captchaType', ''],
  74. ], true);
  75. try {
  76. aj_captcha_check_one($captchaType, $token, $pointJson);
  77. return $this->success();
  78. } catch (\Throwable $e) {
  79. return $this->error();
  80. }
  81. }
  82. /**
  83. * 登陆
  84. * @return mixed
  85. */
  86. public function login(SystemAdminServices $service)
  87. {
  88. [$account, $password, $captchaType, $captchaVerification] = $this->request->postMore([
  89. 'account',
  90. 'pwd',
  91. ['captchaType', ''],
  92. ['captchaVerification', ''],
  93. ], true);
  94. $key = 'login_captcha_' . $account;
  95. if (Cache::has($key) && Cache::get($key) > 2) {
  96. if (!$captchaType || !$captchaVerification) {
  97. return $this->error('请拖动滑块验证');
  98. }
  99. //二次验证
  100. try {
  101. aj_captcha_check_two($captchaType, $captchaVerification);
  102. } catch (\Throwable $e) {
  103. return $this->error($e->getError());
  104. }
  105. }
  106. validate(SystemAdminValidate::class)->scene('get')->check(['account' => $account, 'pwd' => $password]);
  107. $res = $service->login($account, $password, 'admin');
  108. if ($res) {
  109. Cache::delete($key);
  110. }
  111. return $this->success($res);
  112. }
  113. public function logout()
  114. {
  115. $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
  116. CacheService::redisHandler()->delete(md5($key));
  117. return $this->success();
  118. }
  119. }