LoginController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\QQWry;
  4. use App\Http\Models\User;
  5. use App\Http\Models\UserLoginLog;
  6. use Illuminate\Http\Request;
  7. use Response;
  8. use Redirect;
  9. use Captcha;
  10. use Session;
  11. use Cache;
  12. use Log;
  13. /**
  14. * 登录控制器
  15. * Class LoginController
  16. *
  17. * @package App\Http\Controllers
  18. */
  19. class LoginController extends Controller
  20. {
  21. // 登录页
  22. public function index(Request $request)
  23. {
  24. if ($request->method() == 'POST') {
  25. $username = trim($request->get('username'));
  26. $password = trim($request->get('password'));
  27. $captcha = trim($request->get('captcha'));
  28. if (empty($username) || empty($password)) {
  29. Session::flash('errorMsg', '请输入用户名和密码');
  30. return Redirect::back();
  31. }
  32. // 是否校验验证码
  33. if ($this->systemConfig['is_captcha']) {
  34. if (!Captcha::check($captcha)) {
  35. Session::flash('errorMsg', '验证码错误,请重新输入');
  36. return Redirect::back()->withInput();
  37. }
  38. }
  39. $user = User::query()->where('username', $username)->where('password', md5($password))->first();
  40. if (!$user) {
  41. Session::flash('errorMsg', '用户名或密码错误');
  42. return Redirect::back()->withInput();
  43. } elseif (!$user->is_admin && $user->status < 0) {
  44. Session::flash('errorMsg', '账号已禁用');
  45. return Redirect::back();
  46. } elseif ($user->status == 0 && $this->systemConfig['is_active_register'] && $user->is_admin == 0) {
  47. Session::flash('errorMsg', '账号未激活,请点击<a href="/activeUser?username=' . $user->username . '" target="_blank"><span style="color:#000">【激活账号】</span></a>');
  48. return Redirect::back()->withInput();
  49. }
  50. // 更新登录信息
  51. $remember_token = "";
  52. if ($request->get('remember')) {
  53. $remember_token = makeRandStr(20);
  54. User::query()->where('id', $user->id)->update(['last_login' => time(), 'remember_token' => $remember_token]);
  55. } else {
  56. User::query()->where('id', $user->id)->update(['last_login' => time(), 'remember_token' => '']);
  57. }
  58. // 登录送积分
  59. if ($this->systemConfig['login_add_score']) {
  60. if (!Cache::has('loginAddScore_' . md5($username))) {
  61. $score = mt_rand($this->systemConfig['min_rand_score'], $this->systemConfig['max_rand_score']);
  62. $ret = User::query()->where('id', $user->id)->increment('score', $score);
  63. if ($ret) {
  64. $this->addUserScoreLog($user->id, $user->score, $user->score + $score, $score, '登录送积分');
  65. // 登录多久后再登录可以获取积分
  66. $ttl = $this->systemConfig['login_add_score_range'] ? $this->systemConfig['login_add_score_range'] : 1440;
  67. Cache::put('loginAddScore_' . md5($username), '1', $ttl);
  68. Session::flash('successMsg', '欢迎回来,系统自动赠送您 ' . $score . ' 积分,您可以用它兑换流量包');
  69. }
  70. }
  71. }
  72. // 写入登录日志
  73. $this->addUserLoginLog($user->id, getClientIp());
  74. // 重新取出用户信息
  75. $userInfo = User::query()->where('id', $user->id)->first();
  76. Session::put('user', $userInfo->toArray());
  77. // 根据权限跳转
  78. if ($user->is_admin) {
  79. return Redirect::to('admin')->cookie('remember', $remember_token, 36000);
  80. }
  81. return Redirect::to('/')->cookie('remember', $remember_token, 36000);
  82. } else {
  83. if ($request->cookie("remember")) {
  84. $u = User::query()->where('status', '>=', 0)->where("remember_token", $request->cookie("remember"))->first();
  85. if ($u) {
  86. Session::put('user', $u->toArray());
  87. if ($u->is_admin) {
  88. return Redirect::to('admin');
  89. }
  90. return Redirect::to('/');
  91. }
  92. }
  93. $view['is_captcha'] = $this->systemConfig['is_captcha'];
  94. $view['is_register'] = $this->systemConfig['is_register'];
  95. $view['website_home_logo'] = $this->systemConfig['website_home_logo'];
  96. $view['website_analytics'] = $this->systemConfig['website_analytics'];
  97. $view['website_customer_service'] = $this->systemConfig['website_customer_service'];
  98. return Response::view('login', $view);
  99. }
  100. }
  101. // 退出
  102. public function logout(Request $request)
  103. {
  104. Session::flush();
  105. return Redirect::to('login')->cookie('remember', "", 36000);
  106. }
  107. // 添加用户登录日志
  108. private function addUserLoginLog($userId, $ip)
  109. {
  110. // 解析IP信息
  111. $qqwry = new QQWry();
  112. $ipInfo = $qqwry->ip($ip);
  113. if (!$ipInfo || !is_array($ipInfo)) {
  114. \Log::warning("获取IP地址信息异常:" . $ip);
  115. }
  116. $log = new UserLoginLog();
  117. $log->user_id = $userId;
  118. $log->ip = $ip;
  119. $log->country = $ipInfo['country'];
  120. $log->province = $ipInfo['province'];
  121. $log->city = $ipInfo['city'];
  122. $log->county = $ipInfo['county'];
  123. $log->isp = $ipInfo['isp'];
  124. $log->area = $ipInfo['area'];
  125. $log->save();
  126. }
  127. // 获取IP信息
  128. private function getIPInfo($ip)
  129. {
  130. $url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
  131. $result = $this->curlRequest($url);
  132. Log::info("登录获取IP信息:" . $result);
  133. $result = json_decode($result);
  134. if (!$result) {
  135. return false;
  136. }
  137. if ($result->code) {
  138. return false;
  139. }
  140. return $result->data;
  141. }
  142. // 发起一个CURL请求
  143. private function curlRequest($url, $data = [])
  144. {
  145. $ch = curl_init();
  146. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  147. curl_setopt($ch, CURLOPT_TIMEOUT, 500);
  148. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  149. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  150. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  151. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  152. curl_setopt($ch, CURLOPT_URL, $url);
  153. // 如果data有数据,则用POST请求
  154. if ($data) {
  155. curl_setopt($ch, CURLOPT_POST, 1);
  156. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  157. }
  158. $res = curl_exec($ch);
  159. curl_close($ch);
  160. return $res;
  161. }
  162. }