LoginController.php 7.0 KB

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