Login.php 4.0 KB

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