Login.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use app\wap\model\user\SmsCode;
  13. use app\wap\model\user\PhoneUser;
  14. use app\wap\model\user\User;
  15. use basic\WapBasic;
  16. use service\SystemConfigService;
  17. use service\UtilService;
  18. use service\JsonService;
  19. use think\Cookie;
  20. use think\Request;
  21. use think\Session;
  22. use think\Url;
  23. /**移动端登录控制器
  24. * Class Login
  25. * @package app\wap\controller
  26. */
  27. class Login extends WapBasic
  28. {
  29. public function index($ref = '', $spread_uid = 0)
  30. {
  31. Cookie::set('is_bg', 1);
  32. $ref && $ref = htmlspecialchars_decode(base64_decode($ref));
  33. if (UtilService::isWechatBrowser()) {
  34. $this->_logout();
  35. $this->oauth($spread_uid);
  36. Cookie::delete('_oen');
  37. exit($this->redirect(empty($ref) ? Url::build('wap/index/index') : $ref));
  38. }
  39. $this->assign(['ref' => $ref,
  40. 'Auth_site_name' => SystemConfigService::get('site_name')
  41. ]);
  42. return $this->fetch();
  43. }
  44. /**
  45. * 短信登陆
  46. * @param Request $request
  47. */
  48. public function phone_check(Request $request)
  49. {
  50. list($phone, $code) = UtilService::postMore([
  51. ['phone', ''],
  52. ['code', ''],
  53. ], $request, true);
  54. if (!$phone || !$code) return JsonService::fail('请输入登录账号');
  55. if (!$code) return JsonService::fail('请输入验证码');
  56. $code = md5('is_phone_code' . $code);
  57. if (!SmsCode::CheckCode($phone, $code)) return JsonService::fail('验证码验证失败');
  58. SmsCode::setCodeInvalid($phone, $code);
  59. if (($info = PhoneUser::UserLogIn($phone, $request)) !== false)
  60. return JsonService::successful('登录成功', $info);
  61. else
  62. return JsonService::fail(PhoneUser::getErrorInfo('登录失败'));
  63. }
  64. /**账号密码登录
  65. * @param Request $request
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public function check(Request $request)
  71. {
  72. list($account, $pwd) = UtilService::postMore(['account', 'pwd'], $request, true);
  73. if (!$account || !$pwd) return JsonService::fail('请输入登录账号');
  74. if (!PhoneUser::be(['phone' => $account])) return JsonService::fail('登陆账号不存在!');
  75. $phoneInfo = PhoneUser::where('phone', $account)->find();
  76. $errorInfo = Session::get('login_error_info', 'wap') ?: ['num' => 0];
  77. $now = time();
  78. if ($errorInfo['num'] > 5 && $errorInfo['time'] < ($now - 900))
  79. return JsonService::fail('错误次数过多,请稍候再试!');
  80. if ($phoneInfo['pwd'] != $pwd) {
  81. Session::set('login_error_info', ['num' => $errorInfo['num'] + 1, 'time' => $now], 'wap');
  82. return JsonService::fail('账号或密码输入错误!');
  83. }
  84. $userinfo = User::where('uid', $phoneInfo['uid'])->find();
  85. if (!$userinfo) return JsonService::fail('账号异常!');
  86. if (!$userinfo['status']) return JsonService::fail('账号已被锁定,无法登陆!');
  87. $this->_logout();
  88. $name = '__login_phone_number';
  89. Session::set('loginUid', $userinfo['uid'], 'wap');
  90. $phoneInfo['last_time'] = time();
  91. $phoneInfo['last_ip'] = $request->ip();
  92. $phoneInfo->save();
  93. unset($userinfo['pwd']);
  94. Session::delete('login_error_info', 'wap');
  95. Cookie::set('is_login', 1);
  96. Cookie::set('__login_phone', 1);
  97. Session::set($name, $userinfo['phone'], 'wap');
  98. Session::set('__login_phone_num' . $userinfo['uid'], $userinfo['phone'], 'wap');
  99. $qrcode_url = SystemConfigService::get('wechat_qrcode');
  100. $info = ['userinfo' => $userinfo, 'url' => $qrcode_url, 'qcode_id' => 0, 'isfollow' => false];
  101. return JsonService::successful('登录成功', $info);
  102. }
  103. /**账号密码注册/找回密码
  104. * @param Request $request
  105. * @param $account 账号
  106. * @param $pwd 密码
  107. * @param $code 验证码
  108. * @param $type 1=注册 2=找回密码
  109. */
  110. public function register(Request $request)
  111. {
  112. list($account, $pwd, $code, $type) = UtilService::postMore([
  113. ['account', ''],
  114. ['pwd', ''],
  115. ['code', ''],
  116. ['type', 1]
  117. ], $request, true);
  118. if (!$account || !$pwd || !$code) return JsonService::fail('参数有误!');
  119. if (!$code) return JsonService::fail('请输入验证码');
  120. $code = md5('is_phone_code' . $code);
  121. if (!SmsCode::CheckCode($account, $code)) return JsonService::fail('验证码验证失败');
  122. SmsCode::setCodeInvalid($account, $code);
  123. $msg = $type == 1 ? '注册' : '找回密码';
  124. if (($info = PhoneUser::userRegister($account, $pwd, $type, $request)) !== false)
  125. return JsonService::successful($msg . '成功');
  126. else
  127. return JsonService::fail(PhoneUser::getErrorInfo(PhoneUser::getErrorInfo($msg . '失败')));
  128. }
  129. /**
  130. * 退出登陆
  131. */
  132. public function logout()
  133. {
  134. $this->_logout();
  135. $this->successful('退出登陆成功', Url::build('Index/index'));
  136. }
  137. /**
  138. * 清除缓存
  139. */
  140. private function _logout()
  141. {
  142. Session::clear('wap');
  143. Cookie::delete('is_login');
  144. Cookie::delete('__login_phone');
  145. }
  146. }