Login.php 5.7 KB

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