Login.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\store;
  12. use app\Request;
  13. use crmeb\utils\Captcha;
  14. use crmeb\services\CacheService;
  15. use app\services\store\LoginServices;
  16. use think\exception\ValidateException;
  17. use app\validate\api\user\RegisterValidates;
  18. use think\facade\Cache;
  19. use think\facade\Config;
  20. /**
  21. * 登录
  22. * Class AuthController
  23. * @package app\api\controller
  24. */
  25. class Login
  26. {
  27. protected $services = NUll;
  28. /**
  29. * LoginController constructor.
  30. * @param LoginServices $services
  31. */
  32. public function __construct(LoginServices $services)
  33. {
  34. $this->services = $services;
  35. }
  36. /**
  37. * @param Request $request
  38. * @return mixed
  39. * @author 等风来
  40. * @email 136327134@qq.com
  41. * @date 2022/10/11
  42. */
  43. public function getAjCaptcha(Request $request)
  44. {
  45. [$account,] = $request->postMore([
  46. 'account',
  47. ], true);
  48. $key = 'store_login_captcha_' . $account;
  49. return app('json')->success(['is_captcha' => Cache::get($key) > 2]);
  50. }
  51. /**
  52. * @return mixed
  53. */
  54. public function ajcaptcha(Request $request)
  55. {
  56. $captchaType = $request->get('captchaType');
  57. return app('json')->success(aj_captcha_create($captchaType));
  58. }
  59. /**
  60. * 一次验证
  61. * @return mixed
  62. */
  63. public function ajcheck(Request $request)
  64. {
  65. [$token, $pointJson, $captchaType] = $request->postMore([
  66. ['token', ''],
  67. ['pointJson', ''],
  68. ['captchaType', ''],
  69. ], true);
  70. try {
  71. aj_captcha_check_one($captchaType, $token, $pointJson);
  72. return app('json')->success();
  73. } catch (\Throwable $e) {
  74. return app('json')->fail(400336);
  75. }
  76. }
  77. /**
  78. * 获取后台登录页轮播图以及LOGO
  79. * @return mixed
  80. */
  81. public function info()
  82. {
  83. return app('json')->success($this->services->getLoginInfo());
  84. }
  85. /**
  86. * 验证码
  87. * @return \app\controller\admin\Login|\think\Response
  88. */
  89. public function captcha()
  90. {
  91. return app()->make(Captcha::class)->create();
  92. }
  93. /**
  94. * H5账号登陆
  95. * @param Request $request
  96. * @return mixed
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\ModelNotFoundException
  99. * @throws \think\exception\DbException
  100. */
  101. public function login(Request $request)
  102. {
  103. [$account, $password, $captchaType, $captchaVerification] = $request->postMore([
  104. 'account',
  105. 'pwd',
  106. ['captchaType', ''],
  107. ['captchaVerification', '']
  108. ], true);
  109. validate(\app\validate\store\StoreAdminValidate::class)->scene('get')->check(['account' => $account, 'pwd' => $password]);
  110. $key = 'store_login_captcha_' . $account;
  111. if (Cache::has($key) && Cache::get($key) > 2) {
  112. if (!$captchaType || !$captchaVerification) {
  113. return app('json')->fail('请拖动滑块验证');
  114. }
  115. //二次验证
  116. try {
  117. aj_captcha_check_two($captchaType, $captchaVerification);
  118. } catch (\Throwable $e) {
  119. return app('json')->fail($e->getError());
  120. }
  121. }
  122. $res = $this->services->login($account, $password, 'store');
  123. if ($res) {
  124. Cache::delete($key);
  125. }
  126. return app('json')->success($res);
  127. }
  128. /**
  129. * 退出登录
  130. * @param Request $request
  131. * @return mixed
  132. * @throws \Psr\SimpleCache\InvalidArgumentException
  133. */
  134. public function logout(Request $request)
  135. {
  136. $key = trim(ltrim($request->header(Config::get('cookie.token_name')), 'Bearer'));
  137. CacheService::redisHandler()->delete(md5($key));
  138. return app('json')->success();
  139. }
  140. /**
  141. * 密码修改
  142. * @param Request $request
  143. * @return mixed
  144. */
  145. public function reset(Request $request)
  146. {
  147. [$account, $captcha, $password] = $request->postMore([['account', ''], ['captcha', ''], ['password', '']], true);
  148. try {
  149. validate(RegisterValidates::class)->scene('register')->check(['account' => $account, 'captcha' => $captcha, 'password' => $password]);
  150. } catch (ValidateException $e) {
  151. return app('json')->fail($e->getError());
  152. }
  153. $verifyCode = CacheService::get('code_' . $account);
  154. if (!$verifyCode)
  155. return app('json')->fail('请先获取验证码');
  156. $verifyCode = substr($verifyCode, 0, 6);
  157. if ($verifyCode != $captcha) {
  158. return app('json')->fail('验证码错误');
  159. }
  160. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 16)
  161. return app('json')->fail('密码必须是在6到16位之间');
  162. if ($password == '123456') return app('json')->fail('密码太过简单,请输入较为复杂的密码');
  163. $resetStatus = $this->services->reset($account, $password);
  164. if ($resetStatus) {
  165. CacheService::delete('code_' . $account);
  166. return app('json')->success('修改成功');
  167. }
  168. return app('json')->fail('修改失败');
  169. }
  170. }