LoginController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1;
  12. use app\Request;
  13. use app\services\message\notice\SmsService;
  14. use app\services\wechat\WechatServices;
  15. use think\facade\Config;
  16. use crmeb\services\CacheService;
  17. use app\services\user\LoginServices;
  18. use think\exception\ValidateException;
  19. use app\api\validate\user\RegisterValidates;
  20. /**
  21. * 微信小程序授权类
  22. * Class AuthController
  23. * @package app\api\controller
  24. */
  25. class LoginController
  26. {
  27. protected $services;
  28. /**
  29. * LoginController constructor.
  30. * @param LoginServices $services
  31. */
  32. public function __construct(LoginServices $services)
  33. {
  34. $this->services = $services;
  35. }
  36. /**
  37. * H5账号登陆
  38. * @param Request $request
  39. * @return mixed
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. * @throws \think\exception\DbException
  43. */
  44. public function login(Request $request)
  45. {
  46. [$account, $password, $spread] = $request->postMore([
  47. 'account', 'password', 'spread'
  48. ], true);
  49. if (!$account || !$password) {
  50. return app('json')->fail(410000);
  51. }
  52. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 32) {
  53. return app('json')->fail(400762);
  54. }
  55. return app('json')->success(410001, $this->services->login($account, $password, $spread));
  56. }
  57. /**
  58. * 退出登录
  59. * @param Request $request
  60. * @return mixed
  61. */
  62. public function logout(Request $request)
  63. {
  64. $key = trim(ltrim($request->header(Config::get('cookie.token_name')), 'Bearer'));
  65. CacheService::delete(md5($key));
  66. return app('json')->success(410002);
  67. }
  68. /**
  69. * 获取发送验证码key
  70. * @return mixed
  71. */
  72. public function verifyCode()
  73. {
  74. $unique = password_hash(uniqid(true), PASSWORD_BCRYPT);
  75. CacheService::set('sms.key.' . $unique, 0, 300);
  76. $time = sys_config('verify_expire_time', 1);
  77. return app('json')->success(['key' => $unique, 'expire_time' => $time]);
  78. }
  79. /**
  80. * 获取图片验证码
  81. * @param Request $request
  82. * @return \think\Response
  83. */
  84. public function captcha(Request $request)
  85. {
  86. ob_clean();
  87. $rep = captcha();
  88. $key = app('session')->get('captcha.key');
  89. $uni = $request->get('key');
  90. if ($uni) {
  91. CacheService::set('sms.key.cap.' . $uni, $key, 300);
  92. }
  93. return $rep;
  94. }
  95. /**
  96. * 验证验证码是否正确
  97. * @param $uni
  98. * @param string $code
  99. * @return bool
  100. * @throws \Psr\SimpleCache\InvalidArgumentException
  101. */
  102. protected function checkCaptcha($uni, string $code): bool
  103. {
  104. $cacheName = 'sms.key.cap.' . $uni;
  105. if (!CacheService::has($cacheName)) {
  106. return false;
  107. }
  108. $key = CacheService::get($cacheName);
  109. $code = mb_strtolower($code, 'UTF-8');
  110. $res = password_verify($code, $key);
  111. if ($res) {
  112. CacheService::delete($cacheName);
  113. }
  114. return $res;
  115. }
  116. /**
  117. * 验证码发送
  118. * @param Request $request
  119. * @param SmsService $services
  120. * @return mixed
  121. */
  122. public function verify(Request $request, SmsService $services)
  123. {
  124. [$phone, $type, $key, $captchaType, $captchaVerification] = $request->postMore([
  125. ['phone', 0],
  126. ['type', ''],
  127. ['key', ''],
  128. ['captchaType', ''],
  129. ['captchaVerification', ''],
  130. ], true);
  131. $keyName = 'sms.key.' . $key;
  132. $nowKey = 'sms.' . date('YmdHi');
  133. if (!CacheService::has($keyName)) {
  134. return app('json')->fail(410003);
  135. }
  136. $total = 1;
  137. if (CacheService::has($nowKey)) {
  138. $total = CacheService::get($nowKey);
  139. if ($total > Config::get('sms.maxMinuteCount', 20))
  140. return app('json')->success(410006);
  141. }
  142. //二次验证
  143. try {
  144. aj_captcha_check_two($captchaType, $captchaVerification);
  145. } catch (\Throwable $e) {
  146. return app('json')->fail($e->getError());
  147. }
  148. try {
  149. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  150. } catch (ValidateException $e) {
  151. return app('json')->fail($e->getError());
  152. }
  153. $time = sys_config('verify_expire_time', 1);
  154. $smsCode = $this->services->verify($services, $phone, $type, $time, app()->request->ip());
  155. if ($smsCode) {
  156. CacheService::set('code_' . $phone, $smsCode, $time * 60);
  157. CacheService::set($nowKey, $total, 61);
  158. return app('json')->success(410007);
  159. } else {
  160. return app('json')->fail(410008);
  161. }
  162. }
  163. /**
  164. * H5注册新用户
  165. * @param Request $request
  166. * @return mixed
  167. * @throws \think\db\exception\DataNotFoundException
  168. * @throws \think\db\exception\DbException
  169. * @throws \think\db\exception\ModelNotFoundException
  170. */
  171. public function register(Request $request)
  172. {
  173. [$account, $captcha, $password, $spread] = $request->postMore([['account', ''], ['captcha', ''], ['password', ''], ['spread', 0]], true);
  174. try {
  175. validate(RegisterValidates::class)->scene('register')->check(['account' => $account, 'captcha' => $captcha, 'password' => $password]);
  176. } catch (ValidateException $e) {
  177. return app('json')->fail($e->getError());
  178. }
  179. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 32) {
  180. return app('json')->fail(400762);
  181. }
  182. $verifyCode = CacheService::get('code_' . $account);
  183. if (!$verifyCode)
  184. return app('json')->fail(410009);
  185. $verifyCode = substr($verifyCode, 0, 6);
  186. if ($verifyCode != $captcha)
  187. return app('json')->fail(410010);
  188. if (md5($password) == md5('123456')) return app('json')->fail(410012);
  189. $registerStatus = $this->services->register($account, $password, $spread, 'h5');
  190. if ($registerStatus) {
  191. return app('json')->success(410013);
  192. }
  193. return app('json')->fail(410014);
  194. }
  195. /**
  196. * 密码修改
  197. * @param Request $request
  198. * @return mixed
  199. * @throws \think\db\exception\DataNotFoundException
  200. * @throws \think\db\exception\DbException
  201. * @throws \think\db\exception\ModelNotFoundException
  202. */
  203. public function reset(Request $request)
  204. {
  205. [$account, $captcha, $password] = $request->postMore([['account', ''], ['captcha', ''], ['password', '']], true);
  206. try {
  207. validate(RegisterValidates::class)->scene('register')->check(['account' => $account, 'captcha' => $captcha, 'password' => $password]);
  208. } catch (ValidateException $e) {
  209. return app('json')->fail($e->getError());
  210. }
  211. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 32) {
  212. return app('json')->fail(400762);
  213. }
  214. $verifyCode = CacheService::get('code_' . $account);
  215. if (!$verifyCode)
  216. return app('json')->fail(410009);
  217. $verifyCode = substr($verifyCode, 0, 6);
  218. if ($verifyCode != $captcha) {
  219. return app('json')->fail(410010);
  220. }
  221. if ($password == '123456') return app('json')->fail(410012);
  222. $resetStatus = $this->services->reset($account, $password);
  223. if ($resetStatus) return app('json')->success(100001);
  224. return app('json')->fail(100007);
  225. }
  226. /**
  227. * 手机号登录
  228. * @param Request $request
  229. * @return mixed
  230. * @throws \think\db\exception\DataNotFoundException
  231. * @throws \think\db\exception\ModelNotFoundException
  232. * @throws \think\exception\DbException
  233. */
  234. public function mobile(Request $request)
  235. {
  236. [$phone, $captcha, $spread] = $request->postMore([['phone', ''], ['captcha', ''], ['spread', 0]], true);
  237. //验证手机号
  238. try {
  239. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  240. } catch (ValidateException $e) {
  241. return app('json')->fail($e->getError());
  242. }
  243. //验证验证码
  244. $verifyCode = CacheService::get('code_' . $phone);
  245. if (!$verifyCode)
  246. return app('json')->fail(410009);
  247. $verifyCode = substr($verifyCode, 0, 6);
  248. if ($verifyCode != $captcha) {
  249. return app('json')->fail(410010);
  250. }
  251. $user_type = $request->getFromType() ? $request->getFromType() : 'h5';
  252. $token = $this->services->mobile($phone, $spread, $user_type);
  253. if ($token) {
  254. CacheService::delete('code_' . $phone);
  255. return app('json')->success(410001, $token);
  256. } else {
  257. return app('json')->fail(410002);
  258. }
  259. }
  260. /**
  261. * H5切换登陆
  262. * @param Request $request
  263. * @return mixed
  264. * @throws \think\db\exception\DataNotFoundException
  265. * @throws \think\db\exception\ModelNotFoundException
  266. * @throws \think\exception\DbException
  267. */
  268. public function switch_h5(Request $request)
  269. {
  270. $from = $request->post('from', 'wechat');
  271. $user = $request->user();
  272. $token = $this->services->switchAccount($user, $from);
  273. if ($token) {
  274. $token['userInfo'] = $user;
  275. return app('json')->success(410001, $token);
  276. } else
  277. return app('json')->fail(410002);
  278. }
  279. /**
  280. * 绑定手机号
  281. * @param Request $request
  282. * @return mixed
  283. * @throws \think\db\exception\DataNotFoundException
  284. * @throws \think\db\exception\ModelNotFoundException
  285. * @throws \think\exception\DbException
  286. */
  287. public function binding_phone(Request $request)
  288. {
  289. list($phone, $captcha, $key) = $request->postMore([
  290. ['phone', ''],
  291. ['captcha', ''],
  292. ['key', '']
  293. ], true);
  294. //验证手机号
  295. try {
  296. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  297. } catch (ValidateException $e) {
  298. return app('json')->fail($e->getError());
  299. }
  300. if (!$key) {
  301. return app('json')->fail(100100);
  302. }
  303. if (!$phone) {
  304. return app('json')->fail(410015);
  305. }
  306. //验证验证码
  307. $verifyCode = CacheService::get('code_' . $phone);
  308. if (!$verifyCode)
  309. return app('json')->fail(410009);
  310. $verifyCode = substr($verifyCode, 0, 6);
  311. if ($verifyCode != $captcha) {
  312. return app('json')->fail(410010);
  313. }
  314. $re = $this->services->bindind_phone($phone, $key);
  315. if ($re) {
  316. CacheService::delete('code_' . $phone);
  317. return app('json')->success(410016, $re);
  318. } else
  319. return app('json')->fail(410017);
  320. }
  321. /**
  322. * 绑定手机号
  323. * @param Request $request
  324. * @return mixed
  325. * @throws \think\db\exception\DataNotFoundException
  326. * @throws \think\db\exception\ModelNotFoundException
  327. * @throws \think\exception\DbException
  328. */
  329. public function user_binding_phone(Request $request)
  330. {
  331. list($phone, $captcha, $step) = $request->postMore([
  332. ['phone', ''],
  333. ['captcha', ''],
  334. ['step', 0]
  335. ], true);
  336. //验证手机号
  337. try {
  338. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  339. } catch (ValidateException $e) {
  340. return app('json')->fail($e->getError());
  341. }
  342. if (!$step) {
  343. //验证验证码
  344. $verifyCode = CacheService::get('code_' . $phone);
  345. if (!$verifyCode)
  346. return app('json')->fail(410009);
  347. $verifyCode = substr($verifyCode, 0, 6);
  348. if ($verifyCode != $captcha)
  349. return app('json')->fail(410010);
  350. }
  351. $uid = (int)$request->uid();
  352. $re = $this->services->userBindindPhone($uid, $phone, $step);
  353. if ($re) {
  354. CacheService::delete('code_' . $phone);
  355. return app('json')->success($re['msg'] ?? 410016, $re['data'] ?? []);
  356. } else
  357. return app('json')->fail(410017);
  358. }
  359. public function update_binding_phone(Request $request)
  360. {
  361. [$phone, $captcha] = $request->postMore([
  362. ['phone', ''],
  363. ['captcha', ''],
  364. ], true);
  365. //验证手机号
  366. try {
  367. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  368. } catch (ValidateException $e) {
  369. return app('json')->fail($e->getError());
  370. }
  371. //验证验证码
  372. $verifyCode = CacheService::get('code_' . $phone);
  373. if (!$verifyCode)
  374. return app('json')->fail(410009);
  375. $verifyCode = substr($verifyCode, 0, 6);
  376. if ($verifyCode != $captcha)
  377. return app('json')->fail(410010);
  378. $uid = (int)$request->uid();
  379. $re = $this->services->updateBindindPhone($uid, $phone);
  380. if ($re) {
  381. CacheService::delete('code_' . $phone);
  382. return app('json')->success($re['msg'] ?? 100001, $re['data'] ?? []);
  383. } else
  384. return app('json')->fail(100007);
  385. }
  386. /**
  387. * 设置扫描二维码状态
  388. * @param string $code
  389. * @return mixed
  390. */
  391. public function setLoginKey(string $code)
  392. {
  393. if (!$code) {
  394. return app('json')->fail(410020);
  395. }
  396. $cacheCode = CacheService::get($code);
  397. if ($cacheCode === false || $cacheCode === null) {
  398. return app('json')->fail(410021);
  399. }
  400. CacheService::set($code, '0', 600);
  401. return app('json')->success();
  402. }
  403. /**
  404. * apple快捷登陆
  405. * @param Request $request
  406. * @param WechatServices $services
  407. * @return mixed
  408. * @throws \Psr\SimpleCache\InvalidArgumentException
  409. * @throws \think\db\exception\DataNotFoundException
  410. * @throws \think\db\exception\ModelNotFoundException
  411. */
  412. public function appleLogin(Request $request, WechatServices $services)
  413. {
  414. [$openId, $phone, $email, $captcha] = $request->postMore([
  415. ['openId', ''],
  416. ['phone', ''],
  417. ['email', ''],
  418. ['captcha', '']
  419. ], true);
  420. if ($phone) {
  421. if (!$captcha) {
  422. return app('json')->fail(410004);
  423. }
  424. //验证验证码
  425. $verifyCode = CacheService::get('code_' . $phone);
  426. if (!$verifyCode)
  427. return app('json')->fail(410009);
  428. $verifyCode = substr($verifyCode, 0, 6);
  429. if ($verifyCode != $captcha) {
  430. CacheService::delete('code_' . $phone);
  431. return app('json')->fail(410010);
  432. }
  433. }
  434. if ($email == '') $email = substr(md5($openId), 0, 12);
  435. $userInfo = [
  436. 'openId' => $openId,
  437. 'unionid' => '',
  438. 'avatarUrl' => sys_config('h5_avatar'),
  439. 'nickName' => $email,
  440. ];
  441. $token = $services->appAuth($userInfo, $phone, 'apple');
  442. if ($token) {
  443. return app('json')->success(410001, $token);
  444. } else if ($token === false) {
  445. return app('json')->success(410001, ['isbind' => true]);
  446. } else {
  447. return app('json')->fail(410019);
  448. }
  449. }
  450. /**
  451. * 滑块验证
  452. * @return mixed
  453. */
  454. public function ajcaptcha(Request $request)
  455. {
  456. $captchaType = $request->get('captchaType');
  457. return app('json')->success(aj_captcha_create($captchaType));
  458. }
  459. /**
  460. * 一次验证
  461. * @return mixed
  462. */
  463. public function ajcheck(Request $request)
  464. {
  465. [$token, $pointJson, $captchaType] = $request->postMore([
  466. ['token', ''],
  467. ['pointJson', ''],
  468. ['captchaType', ''],
  469. ], true);
  470. try {
  471. aj_captcha_check_one($captchaType, $token, $pointJson);
  472. return app('json')->success();
  473. } catch (\Throwable $e) {
  474. return app('json')->fail(400336);
  475. }
  476. }
  477. }