AuthController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. namespace app\api\controller;
  3. use app\admin\model\sms\SmsRecord;
  4. use app\http\validates\user\RegisterValidates;
  5. use app\models\user\User;
  6. use app\models\user\UserToken;
  7. use app\models\user\WechatUser;
  8. use app\Request;
  9. use crmeb\jobs\TestJob;
  10. use crmeb\repositories\ShortLetterRepositories;
  11. use crmeb\services\CacheService;
  12. use crmeb\services\SMSService;
  13. use crmeb\services\SysService;
  14. use crmeb\services\UtilService;
  15. use think\facade\Cache;
  16. use think\exception\ValidateException;
  17. use think\facade\Config;
  18. use think\facade\Queue;
  19. use think\facade\Session;
  20. /**微信小程序授权类
  21. * Class AuthController
  22. * @package app\api\controller
  23. */
  24. class AuthController
  25. {
  26. /**
  27. * H5账号登陆
  28. * @param Request $request
  29. * @return mixed
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. * @throws \think\exception\DbException
  33. */
  34. public function login(Request $request)
  35. {
  36. $user = User::where('account', $request->param('account'))->find();
  37. if ($user) {
  38. if ($user->pwd !== md5($request->param('password')))
  39. return app('json')->fail('账号或密码错误');
  40. if ($user->pwd === md5(123456))
  41. return app('json')->fail('请修改您的初始密码,再尝试登陆!');
  42. } else {
  43. return app('json')->fail('账号或密码错误');
  44. }
  45. if (!$user['status'])
  46. return app('json')->fail('已被禁止,请联系管理员');
  47. // 设置推广关系
  48. User::setSpread(intval($request->param('spread')), $user->uid);
  49. $token = UserToken::createToken($user, 'user');
  50. if ($token) {
  51. event('UserLogin', [$user, $token]);
  52. return app('json')->success('登录成功', ['token' => $token->token, 'expires_time' => $token->expires_time]);
  53. } else
  54. return app('json')->fail('登录失败');
  55. }
  56. /**
  57. * 退出登录
  58. * @param Request $request
  59. */
  60. public function logout(Request $request)
  61. {
  62. $request->tokenData()->delete();
  63. return app('json')->success('成功');
  64. }
  65. public function verifyCode()
  66. {
  67. $unique = password_hash(uniqid(true), PASSWORD_BCRYPT);
  68. Cache::set('sms.key.' . $unique, 0, 300);
  69. return app('json')->success(['key' => $unique]);
  70. }
  71. public function captcha(Request $request)
  72. {
  73. ob_clean();
  74. $rep = captcha();
  75. $key = app('session')->get('captcha.key');
  76. $uni = $request->get('key');
  77. if ($uni)
  78. Cache::set('sms.key.cap.' . $uni, $key, 300);
  79. return $rep;
  80. }
  81. /**
  82. * 验证验证码是否正确
  83. *
  84. * @param $uni
  85. * @param string $code
  86. * @return bool
  87. * @throws \Psr\SimpleCache\InvalidArgumentException
  88. */
  89. protected function checkCaptcha($uni, string $code): bool
  90. {
  91. $cacheName = 'sms.key.cap.' . $uni;
  92. if (!Cache::has($cacheName)) {
  93. return false;
  94. }
  95. $key = Cache::get($cacheName);
  96. $code = mb_strtolower($code, 'UTF-8');
  97. $res = password_verify($code, $key);
  98. if ($res) {
  99. Cache::delete($cacheName);
  100. }
  101. return $res;
  102. }
  103. /**
  104. * 验证码发送
  105. * @param Request $request
  106. * @return mixed
  107. */
  108. public function verify(Request $request)
  109. {
  110. list($phone, $type) = UtilService::postMore([['phone',0],['type','']],$request, true);
  111. try {
  112. validate(RegisterValidates::class)->scene('code')->check(['phone'=>$phone]);
  113. } catch (ValidateException $e) {
  114. return app('json')->fail($e->getError());
  115. }
  116. if(User::checkPhone($phone) && $type == 'register') return app('json')->fail('手机号已注册');
  117. if(!User::checkPhone($phone) && $type == 'login') return app('json')->fail('账号不存在!');
  118. $time = 300;
  119. if(CacheService::get('code_'.$phone) && false)
  120. return app('json')->fail($time.'秒内有效');
  121. $code = rand(100000,999999);
  122. $data['code'] = $code;
  123. $res = SMSService::send($phone,$data);
  124. if($res['status'] == 400) return app('json')->fail('短信平台验证码发送失败'.$res['msg']);
  125. CacheService::set('code_'.$phone, $code, $time);
  126. return app('json')->success($res['msg'] ?? '发送失败');
  127. }
  128. /**
  129. * H5注册新用户
  130. * @param Request $request
  131. * @return mixed
  132. */
  133. public function register(Request $request)
  134. {
  135. list($account, $captcha, $password, $spread) = UtilService::postMore([['account', ''], ['captcha', ''], ['password', ''], ['spread', 0]], $request, true);
  136. try {
  137. validate(RegisterValidates::class)->scene('register')->check(['account' => $account, 'captcha' => $captcha, 'password' => $password]);
  138. } catch (ValidateException $e) {
  139. return app('json')->fail($e->getError());
  140. }
  141. $verifyCode = CacheService::get('code_' . $account);
  142. /*
  143. if (!$verifyCode)
  144. return app('json')->fail('请先获取验证码');
  145. $verifyCode = substr($verifyCode, 0, 6);
  146. if ($verifyCode!='888888' && $verifyCode != $captcha)
  147. return app('json')->fail('验证码错误');*/
  148. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 16)
  149. return app('json')->fail('密码必须是在6到16位之间');
  150. if ($password == '123456') return app('json')->fail('密码太过简单,请输入较为复杂的密码');
  151. $registerStatus = User::register($account, $password, $spread);
  152. if ($registerStatus) return app('json')->success('注册成功');
  153. return app('json')->fail(User::getErrorInfo('注册失败'));
  154. }
  155. /**
  156. * 密码修改
  157. * @param Request $request
  158. * @return mixed
  159. */
  160. public function reset(Request $request)
  161. {
  162. list($account, $captcha, $password) = UtilService::postMore([['account', ''], ['captcha', ''], ['password', '']], $request, true);
  163. try {
  164. validate(RegisterValidates::class)->scene('register')->check(['account' => $account, 'captcha' => $captcha, 'password' => $password]);
  165. } catch (ValidateException $e) {
  166. return app('json')->fail($e->getError());
  167. }
  168. $verifyCode = CacheService::get('code_' . $account);
  169. if (!$verifyCode)
  170. return app('json')->fail('请先获取验证码');
  171. $verifyCode = substr($verifyCode, 0, 6);
  172. if ($verifyCode != $captcha)
  173. return app('json')->fail('验证码错误');
  174. if (strlen(trim($password)) < 6 || strlen(trim($password)) > 16)
  175. return app('json')->fail('密码必须是在6到16位之间');
  176. if ($password == '123456') return app('json')->fail('密码太过简单,请输入较为复杂的密码');
  177. $resetStatus = User::reset($account, $password);
  178. if ($resetStatus) return app('json')->success('修改成功');
  179. return app('json')->fail(User::getErrorInfo('修改失败'));
  180. }
  181. /**
  182. * 手机号登录
  183. * @param Request $request
  184. * @return mixed
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\ModelNotFoundException
  187. * @throws \think\exception\DbException
  188. */
  189. public function mobile(Request $request)
  190. {
  191. list($phone, $captcha, $spread) = UtilService::postMore([['phone', ''], ['captcha', ''], ['spread', 0]], $request, true);
  192. //验证手机号
  193. try {
  194. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  195. } catch (ValidateException $e) {
  196. return app('json')->fail($e->getError());
  197. }
  198. //验证验证码
  199. $verifyCode = CacheService::get('code_' . $phone);
  200. if (!$verifyCode)
  201. return app('json')->fail('请先获取验证码');
  202. $verifyCode = substr($verifyCode, 0, 6);
  203. if ($verifyCode != $captcha)
  204. return app('json')->fail('验证码错误');
  205. //数据库查询
  206. $user = User::where('account', $phone)->find();
  207. if (!$user)
  208. return app('json')->fail('用户不存在');
  209. if (!$user->status)
  210. return app('json')->fail('已被禁止,请联系管理员');
  211. // 设置推广关系
  212. User::setSpread($spread, $user->uid);
  213. $token = UserToken::createToken($user, 'user');
  214. if ($token) {
  215. event('UserLogin', [$user, $token]);
  216. return app('json')->success('登录成功', ['token' => $token->token, 'expires_time' => $token->expires_time]);
  217. } else
  218. return app('json')->fail('登录失败');
  219. }
  220. /**
  221. * H5切换登陆
  222. * @param Request $request
  223. * @return mixed
  224. * @throws \think\db\exception\DataNotFoundException
  225. * @throws \think\db\exception\ModelNotFoundException
  226. * @throws \think\exception\DbException
  227. */
  228. public function switch_h5(Request $request)
  229. {
  230. $from = $request->post('from', 'wechat');
  231. $user = $request->user();
  232. if ($from === 'h5') {
  233. $user = User::where('phone', $user['phone'])->where('user_type', '<>', 'h5')->find();
  234. $user->login_type = 'wechat';
  235. $user->save();
  236. } else {
  237. //数据库查询
  238. $user = User::where('account|phone', $user['phone'])->where('user_type', 'h5')->find();
  239. if (!$user)
  240. return app('json')->fail('H5用户不存在,无法切换');
  241. if (!$user->status) return app('json')->fail('已被禁止,请联系管理员');
  242. $wechatUserInfo = WechatUser::where('uid', $request->uid())->find();//当前登陆用户信息
  243. $wechatH5UserInfo = WechatUser::where('uid', $user->uid)->find();//H5登陆切换用户信息
  244. if ($wechatH5UserInfo->unionid && $wechatUserInfo->unionid != $wechatH5UserInfo->unionid)
  245. return app('json')->fail('您的账号已绑定特定用户无法切换到此用户上');
  246. if ($wechatH5UserInfo->openid && $wechatUserInfo->openid != $wechatH5UserInfo->openid)
  247. return app('json')->fail('您的账号已绑定特定用户无法切换到此用户上');
  248. if ($wechatH5UserInfo->routine_openid && $wechatUserInfo->routine_openid != $wechatH5UserInfo->routine_openid)
  249. return app('json')->fail('您的账号已绑定特定用户无法切换到此用户上');
  250. switch ($from) {
  251. case 'wechat':
  252. if (!$wechatH5UserInfo->openid)
  253. $wechatH5UserInfo->openid = $wechatUserInfo->openid;
  254. if (!$wechatH5UserInfo->unionid && $wechatUserInfo->unionid)
  255. $wechatH5UserInfo->unionid = $wechatUserInfo->unionid;
  256. break;
  257. case 'routine':
  258. if (!$wechatH5UserInfo->routine_openid)
  259. $wechatH5UserInfo->routine_openid = $wechatUserInfo->routine_openid;
  260. if (!$wechatH5UserInfo->unionid && $wechatUserInfo->unionid)
  261. $wechatH5UserInfo->unionid = $wechatUserInfo->unionid;
  262. break;
  263. }
  264. $wechatH5UserInfo->save();
  265. User::where('uid', $request->uid())->update(['login_type' => 'h5']);
  266. }
  267. $token = UserToken::createToken($user, 'user');
  268. if ($token) {
  269. event('UserLogin', [$user, $token]);
  270. //退出上一个账号
  271. $request->tokenData()->delete();
  272. return app('json')->success('登录成功', ['userInfo' => $user, 'token' => $token->token, 'expires_time' => $token->expires_time, 'time' => strtotime($token->expires_time)]);
  273. } else
  274. return app('json')->fail('登录失败');
  275. }
  276. /**
  277. * 绑定手机号
  278. * @param Request $request
  279. * @return mixed
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\ModelNotFoundException
  282. * @throws \think\exception\DbException
  283. */
  284. public function binding_phone(Request $request)
  285. {
  286. list($phone, $captcha, $step) = UtilService::postMore([
  287. ['phone', ''],
  288. ['captcha', ''],
  289. ['step', 0]
  290. ], $request, true);
  291. //验证手机号
  292. try {
  293. validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
  294. } catch (ValidateException $e) {
  295. return app('json')->fail($e->getError());
  296. }
  297. //验证验证码
  298. $verifyCode = CacheService::get('code_' . $phone);
  299. if (!$verifyCode)
  300. return app('json')->fail('请先获取验证码');
  301. $verifyCode = substr($verifyCode, 0, 6);
  302. if ($verifyCode != $captcha)
  303. return app('json')->fail('验证码错误');
  304. $userInfo = User::where('uid', $request->uid())->find();
  305. $userPhone = $userInfo->phone;
  306. if (!$userInfo) return app('json')->fail('用户不存在');
  307. if ($userInfo->phone) return app('json')->fail('您的账号已经绑定过手机号码!');
  308. if (User::where('phone', $phone)->where('user_type', '<>', 'h5')->count())
  309. return app('json')->fail('此手机已经绑定,无法多次绑定!');
  310. if (User::where('account', $phone)->where('phone', $phone)->where('user_type', 'h5')->find()) {
  311. if (!$step) return app('json')->success('H5已有账号是否绑定此账号上', ['is_bind' => 1]);
  312. $userInfo->phone = $phone;
  313. } else {
  314. $userInfo->account = $phone;
  315. $userInfo->phone = $phone;
  316. }
  317. if ($userInfo->save() || $userPhone == $phone) {
  318. SysService::phone_bangding($request->uid());
  319. return app('json')->success('绑定成功');
  320. }
  321. else
  322. return app('json')->fail('绑定失败');
  323. }
  324. }