Login.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\admin;
  12. use app\Request;
  13. use crmeb\services\CacheService;
  14. use crmeb\utils\Captcha;
  15. use app\services\system\admin\SystemAdminServices;
  16. use think\facade\Cache;
  17. /**
  18. * 后台登陆
  19. * Class Login
  20. * @package app\controller\admin
  21. */
  22. class Login
  23. {
  24. /**
  25. * Login constructor.
  26. * @param SystemAdminServices $services
  27. */
  28. public function __construct(SystemAdminServices $services)
  29. {
  30. $this->services = $services;
  31. }
  32. /**
  33. * 验证码
  34. * @return $this|\think\Response
  35. */
  36. public function captcha()
  37. {
  38. return app()->make(Captcha::class)->create();
  39. }
  40. /**
  41. * @param Request $request
  42. * @return mixed
  43. * @author 等风来
  44. * @email 136327134@qq.com
  45. * @date 2022/10/11
  46. */
  47. public function getAjCaptcha(Request $request)
  48. {
  49. [$account,] = $request->postMore([
  50. 'account',
  51. ], true);
  52. $key = 'login_captcha_' . $account;
  53. return app('json')->success(['is_captcha' => Cache::get($key) > 2]);
  54. }
  55. /**
  56. * 登陆
  57. * @return mixed
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function login(Request $request)
  63. {
  64. [$account, $password, $captchaType, $captchaVerification] = $request->postMore([
  65. 'account',
  66. 'pwd',
  67. ['captchaType', ''],
  68. ['captchaVerification', ''],
  69. ], true);
  70. $key = 'login_captcha_' . $account;
  71. if (Cache::has($key) && Cache::get($key) > 2) {
  72. if (!$captchaType || !$captchaVerification) {
  73. return app('json')->fail('请拖动滑块验证');
  74. }
  75. //二次验证
  76. try {
  77. aj_captcha_check_two($captchaType, $captchaVerification);
  78. } catch (\Throwable $e) {
  79. return app('json')->fail($e->getError());
  80. }
  81. }
  82. validate(\app\validate\admin\setting\SystemAdminValidate::class)->scene('get')->check(['account' => $account, 'pwd' => $password]);
  83. $res = $this->services->login($account, $password, 'admin');
  84. if ($res) {
  85. Cache::delete($key);
  86. }
  87. return app('json')->success($res);
  88. }
  89. /**
  90. * 短信验证码登录
  91. * @param Request $request
  92. * @return mixed
  93. * @throws \Psr\SimpleCache\InvalidArgumentException
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function mobileLogin(Request $request)
  99. {
  100. [$phone, $code] = $request->postMore([
  101. 'phone', 'code',
  102. ], true);
  103. if (!$code) {
  104. return app('json')->fail('请输入验证码');
  105. }
  106. //验证验证码
  107. $verifyCode = CacheService::get('code_' . $phone);
  108. if (!$verifyCode)
  109. return app('json')->fail('请先获取验证码');
  110. $verifyCode = substr($verifyCode, 0, 6);
  111. if ($verifyCode != $code) {
  112. CacheService::delete('code_' . $phone);
  113. return app('json')->fail('验证码错误');
  114. }
  115. return app('json')->success($this->services->login($phone, $code, 'admin', true));
  116. }
  117. /**
  118. * 短信重置密码
  119. * @param Request $request
  120. * @return mixed
  121. * @throws \Psr\SimpleCache\InvalidArgumentException
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. */
  126. public function resetPwd(Request $request)
  127. {
  128. [$phone, $code, $newPwd] = $request->postMore([
  129. 'phone', 'code', 'new_pwd'
  130. ], true);
  131. if (!$phone) {
  132. return app('json')->fail('请输入手机号');
  133. }
  134. if (!check_phone($phone)) {
  135. return app('json')->fail('请输入正确的手机号!');
  136. }
  137. if (!$code) {
  138. return app('json')->fail('请输入验证码');
  139. }
  140. if (!$newPwd) {
  141. return app('json')->fail('请输入新密码');
  142. }
  143. //验证验证码
  144. $verifyCode = CacheService::get('code_' . $phone);
  145. if (!$verifyCode)
  146. return app('json')->fail('请先获取验证码');
  147. $verifyCode = substr($verifyCode, 0, 6);
  148. if ($verifyCode != $code) {
  149. CacheService::delete('code_' . $phone);
  150. return app('json')->fail('验证码错误');
  151. }
  152. $this->services->resetPwd($phone, $newPwd);
  153. return app('json')->success('重置成功,请重新登录');
  154. }
  155. /**
  156. * 获取后台登录页轮播图以及LOGO
  157. * @return mixed
  158. */
  159. public function info()
  160. {
  161. return app('json')->success($this->services->getLoginInfo());
  162. }
  163. /**
  164. * @return mixed
  165. */
  166. public function ajcaptcha(Request $request)
  167. {
  168. $captchaType = $request->get('captchaType');
  169. return app('json')->success(aj_captcha_create($captchaType));
  170. }
  171. /**
  172. * 一次验证
  173. * @return mixed
  174. */
  175. public function ajcheck(Request $request)
  176. {
  177. [$token, $pointJson, $captchaType] = $request->postMore([
  178. ['token', ''],
  179. ['pointJson', ''],
  180. ['captchaType', ''],
  181. ], true);
  182. try {
  183. aj_captcha_check_one($captchaType, $token, $pointJson);
  184. return app('json')->success();
  185. } catch (\Throwable $e) {
  186. return app('json')->fail(400336);
  187. }
  188. }
  189. }