Login.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller;
  12. use crmeb\services\CacheService;
  13. use crmeb\utils\Rsa;
  14. use think\facade\App;
  15. use crmeb\utils\Captcha;
  16. use app\services\system\admin\SystemAdminServices;
  17. use think\facade\Cache;
  18. /**
  19. * 后台登陆
  20. * Class Login
  21. * @package app\adminapi\controller
  22. */
  23. class Login extends AuthController
  24. {
  25. /**
  26. * Login constructor.
  27. * @param App $app
  28. * @param SystemAdminServices $services
  29. */
  30. public function __construct(App $app, SystemAdminServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. protected function initialize()
  36. {
  37. // TODO: Implement initialize() method.
  38. }
  39. /**
  40. * 验证码
  41. * @return $this|\think\Response
  42. */
  43. public function captcha()
  44. {
  45. return app()->make(Captcha::class)->create();
  46. }
  47. /**
  48. * @return mixed
  49. */
  50. public function ajcaptcha()
  51. {
  52. $captchaType = $this->request->get('captchaType');
  53. return app('json')->success(aj_captcha_create($captchaType));
  54. }
  55. /**
  56. * 一次验证
  57. * @return mixed
  58. */
  59. public function ajcheck()
  60. {
  61. [$token, $pointJson, $captchaType] = $this->request->postMore([
  62. ['token', ''],
  63. ['pointJson', ''],
  64. ['captchaType', ''],
  65. ], true);
  66. try {
  67. aj_captcha_check_one($captchaType, $token, $pointJson);
  68. return app('json')->success();
  69. } catch (\Throwable $e) {
  70. return app('json')->fail(400336);
  71. }
  72. }
  73. /**
  74. * 登陆
  75. * @return mixed
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function login(Rsa $rsa)
  81. {
  82. [$account, $password, $key, $captchaVerification, $captchaType] = $this->request->postMore([
  83. 'account',
  84. 'pwd',
  85. ['key', ''],
  86. ['captchaVerification', ''],
  87. ['captchaType', '']
  88. ], true);
  89. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 32) {
  90. return app('json')->fail(400762);
  91. }
  92. if ($captchaVerification != '') {
  93. try {
  94. aj_captcha_check_two($captchaType, $captchaVerification);
  95. } catch (\Throwable $e) {
  96. return app('json')->fail(400336);
  97. }
  98. }
  99. $this->validate(['account' => $account, 'pwd' => $password], \app\adminapi\validate\setting\SystemAdminValidata::class, 'get');
  100. try {
  101. $password = $rsa->privateDecrypt($password);
  102. } catch (\Throwable $e) {
  103. return app('json')->fail($e->getMessage());
  104. }
  105. $result = $this->services->login($account, $password, 'admin', $key);
  106. if (!$result) {
  107. $num = CacheService::get('login_captcha', 1);
  108. if ($num > 1) {
  109. return app('json')->fail(400140, ['login_captcha' => 1]);
  110. }
  111. CacheService::set('login_captcha', $num + 1, 60);
  112. return app('json')->fail(400140, ['login_captcha' => 0]);
  113. }
  114. CacheService::delete('login_captcha');
  115. return app('json')->success($result);
  116. }
  117. /**
  118. * 获取后台登录页轮播图以及LOGO
  119. * @return mixed
  120. */
  121. public function info()
  122. {
  123. return app('json')->success($this->services->getLoginInfo());
  124. }
  125. }