LoginController.php 17 KB

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