Login.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/20
  6. * @time: 11:17
  7. */
  8. namespace app\controller\api;
  9. use app\common\ApiBaseController;
  10. use app\Request;
  11. use app\services\user\LoginServices;
  12. use app\validate\api\user\RegisterValidates;
  13. use Exception;
  14. use Psr\SimpleCache\InvalidArgumentException;
  15. use qiniu\services\CacheService;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\exception\ValidateException;
  20. use think\facade\Config;
  21. class Login extends ApiBaseController
  22. {
  23. /**
  24. * LoginController constructor.
  25. * @param Request $request
  26. * @param LoginServices $services
  27. */
  28. public function __construct(Request $request, LoginServices $services)
  29. {
  30. parent::__construct($request);
  31. $this->service = $services;
  32. }
  33. /**
  34. * H5账号登陆
  35. * @param Request $request
  36. * @return mixed
  37. * @throws DataNotFoundException|ModelNotFoundException|DbException
  38. */
  39. public function login(Request $request)
  40. {
  41. [$account, $password, $spread_uid, $login_type] = $request->postMore([
  42. 'account', 'password', 'spread_uid', ['login_type', 'account']
  43. ], true);
  44. if (!$account || !$password) {
  45. return app('json')->fail('请输入账号和密码');
  46. }
  47. validate(\app\validate\api\LoginValidate::class)->check(['account' => $account, 'pwd' => $password]);
  48. if ($login_type == 'phone') {
  49. if (!check_phone($account)) return app('json')->fail('请输入正确的手机号码');
  50. }
  51. return app('json')->success('登录成功', $this->service->login($account, $login_type, $password, $spread_uid));
  52. }
  53. /**
  54. * 退出登录
  55. * @param Request $request
  56. * @return mixed
  57. * @throws InvalidArgumentException
  58. */
  59. public function logout(Request $request)
  60. {
  61. $key = trim(ltrim($request->header(Config::get('cookie.token_name')), 'Bearer'));
  62. CacheService::redisHandler()->delete(md5($key));
  63. return app('json')->success('成功');
  64. }
  65. public function verifyCode()
  66. {
  67. $unique = password_hash(uniqid(true), PASSWORD_BCRYPT);
  68. CacheService::set('sms.key.' . $unique, 0, 300);
  69. $time = sys_config('verify_expire_time', 1);
  70. return app('json')->success(['key' => $unique, 'expire_time' => $time]);
  71. }
  72. public function captcha(Request $request)
  73. {
  74. ob_clean();
  75. $rep = captcha();
  76. $key = app('session')->get('captcha.key');
  77. $uni = $request->get('key');
  78. if ($uni)
  79. CacheService::set('sms.key.cap.' . $uni, $key, 300);
  80. return $rep;
  81. }
  82. /**
  83. * 验证验证码是否正确
  84. *
  85. * @param $uni
  86. * @param string $code
  87. * @return bool
  88. * @throws InvalidArgumentException
  89. */
  90. protected function checkCaptcha($uni, string $code): bool
  91. {
  92. $cacheName = 'sms.key.cap.' . $uni;
  93. if (!CacheService::has($cacheName)) {
  94. return false;
  95. }
  96. $key = CacheService::get($cacheName);
  97. $code = mb_strtolower($code, 'UTF-8');
  98. $res = password_verify($code, $key);
  99. if ($res) {
  100. CacheService::delete($cacheName);
  101. }
  102. return $res;
  103. }
  104. /**
  105. * @return mixed
  106. */
  107. public function ajcaptcha(Request $request)
  108. {
  109. $captchaType = $request->get('captchaType');
  110. return app('json')->success(aj_captcha_create((string)$captchaType));
  111. }
  112. /**
  113. * 一次验证
  114. * @return mixed
  115. */
  116. public function ajcheck(Request $request)
  117. {
  118. [$token, $pointJson, $captchaType] = $request->postMore([
  119. ['token', ''],
  120. ['pointJson', ''],
  121. ['captchaType', ''],
  122. ], true);
  123. try {
  124. aj_captcha_check_one($captchaType, $token, $pointJson);
  125. return app('json')->success();
  126. } catch (\Throwable $e) {
  127. return app('json')->fail(400336);
  128. }
  129. }
  130. /**
  131. * 验证码发送
  132. * @param Request $request
  133. * @return mixed
  134. * @throws DataNotFoundException
  135. * @throws DbException
  136. * @throws InvalidArgumentException
  137. * @throws ModelNotFoundException
  138. */
  139. public function verify(Request $request)
  140. {
  141. [$phone, $type, $key, $captchaType, $captchaVerification] = $request->postMore([
  142. ['phone', 0],
  143. ['type', ''],
  144. ['key', ''],
  145. ['captchaType', ''],
  146. ['captchaVerification', ''],
  147. ], true);
  148. $keyName = 'sms.key.' . $key;
  149. $nowKey = 'sms.' . date('YmdHi');
  150. if (!CacheService::has($keyName))
  151. return $this->error('发送验证码失败,请刷新页面重新获取');
  152. $total = 1;
  153. if (CacheService::has($nowKey)) {
  154. $total = CacheService::get($nowKey);
  155. if ($total > Config::get('sms.maxMinuteCount', 20))
  156. return app('json')->success('触发分钟级流控:' . Config::get('sms.maxMinuteCount', 20));
  157. }
  158. //二次验证
  159. try {
  160. aj_captcha_check_two($captchaType, $captchaVerification);
  161. } catch (\Throwable $e) {
  162. return app('json')->fail($e->getError());
  163. }
  164. try {
  165. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  166. } catch (ValidateException $e) {
  167. return app('json')->fail($e->getError());
  168. }
  169. $time = sys_config('verify_expire_time', 1);
  170. $smsCode = $this->service->verify($phone, $type, $time, app()->request->ip());
  171. if ($smsCode) {
  172. CacheService::set('code_' . $phone . '_' . $type, $smsCode, $time * 60);
  173. CacheService::set($nowKey, $total, 61);
  174. return app('json')->success('发送成功');
  175. } else {
  176. return app('json')->fail('发送失败');
  177. }
  178. }
  179. /**
  180. * H5注册新用户
  181. * @param Request $request
  182. * @return mixed
  183. * @throws InvalidArgumentException
  184. */
  185. public function register(Request $request)
  186. {
  187. [$phone, $captcha, $password, $nickname, $spread_uid] = $request->postMore([
  188. ['phone', ''],//手机号
  189. ['captcha', ''],//验证码
  190. ['password', ''],//密码
  191. ['nickname', ''],//密码
  192. ['spread_uid', ''],//推荐人ID
  193. ], true);
  194. try {
  195. validate(RegisterValidates::class)->scene('register')->check([
  196. 'phone' => $phone,
  197. 'captcha' => $captcha,
  198. 'password' => $password,
  199. ]);
  200. } catch (ValidateException $e) {
  201. return app('json')->fail($e->getError());
  202. }
  203. check_sms_captcha($phone, 'register', $captcha);
  204. $user_type = $request->getFromType() ? $request->getFromType() : 'h5';
  205. $registerStatus = $this->service->register($phone, $password, $spread_uid, $user_type, $nickname);
  206. if ($registerStatus) {
  207. return app('json')->success('注册成功');
  208. }
  209. return app('json')->fail('注册失败');
  210. }
  211. /**
  212. * 密码修改
  213. * @param Request $request
  214. * @return mixed
  215. * @throws DataNotFoundException
  216. * @throws DbException
  217. * @throws ModelNotFoundException
  218. */
  219. public function reset(Request $request)
  220. {
  221. [$account, $captcha, $password] = $request->postMore([['phone', ''], ['captcha', ''], ['password', '']], true);
  222. check_sms_captcha($account, 'reset', $captcha);
  223. try {
  224. validate(RegisterValidates::class)->scene('register')->check([
  225. 'phone' => $account,
  226. 'captcha' => $captcha,
  227. 'password' => $password,
  228. ]);
  229. } catch (ValidateException $e) {
  230. return app('json')->fail($e->getError());
  231. }
  232. $resetStatus = $this->service->reset($account, $password);
  233. if ($resetStatus) {
  234. return app('json')->success('修改成功');
  235. }
  236. return app('json')->fail('修改失败');
  237. }
  238. /**
  239. * 交易密码修改
  240. * @param Request $request
  241. * @return mixed
  242. * @throws DataNotFoundException
  243. * @throws DbException
  244. * @throws ModelNotFoundException
  245. */
  246. public function reset_trade_pwd(Request $request)
  247. {
  248. [$captcha, $password] = $request->postMore([['captcha', ''], ['password', '']], true);
  249. $account = $request->user()['phone'] ?? '0';
  250. check_sms_captcha($account, 'reset', $captcha);
  251. if (!check_trade_password($password))
  252. return app('json')->fail('交易密码为6位数字');
  253. $resetStatus = $this->service->trade_reset($account, $password);
  254. if ($resetStatus) {
  255. return app('json')->success('修改成功');
  256. }
  257. return app('json')->fail('修改失败');
  258. }
  259. /**
  260. * 手机号登录
  261. * @param Request $request
  262. * @return mixed
  263. * @throws Exception
  264. */
  265. public function mobile(Request $request)
  266. {
  267. [$phone, $captcha, $spread_uid] = $request->postMore([['phone', ''], ['captcha', ''], ['spread_uid', 0]], true);
  268. //验证手机号
  269. try {
  270. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  271. } catch (ValidateException $e) {
  272. return app('json')->fail($e->getError());
  273. }
  274. check_sms_captcha($phone, 'mobile', $captcha);
  275. $user_type = $request->getFromType() ? $request->getFromType() : 'h5';
  276. $token = $this->service->mobile($phone, $spread_uid, $user_type);
  277. if ($token) {
  278. return app('json')->success('登录成功', $token);
  279. } else {
  280. return app('json')->fail('登录失败');
  281. }
  282. }
  283. }