|
|
@@ -3,12 +3,14 @@
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
use App\Http\Models\User;
|
|
|
+use App\Http\Models\UserLoginLog;
|
|
|
use Illuminate\Http\Request;
|
|
|
use Response;
|
|
|
use Redirect;
|
|
|
use Captcha;
|
|
|
use Session;
|
|
|
use Cache;
|
|
|
+use Log;
|
|
|
|
|
|
/**
|
|
|
* 登录控制器
|
|
|
@@ -46,11 +48,11 @@ class LoginController extends Controller
|
|
|
Session::flash('errorMsg', '用户名或密码错误');
|
|
|
|
|
|
return Redirect::back()->withInput();
|
|
|
- } else if (!$user->is_admin && $user->status < 0) {
|
|
|
+ } elseif (!$user->is_admin && $user->status < 0) {
|
|
|
Session::flash('errorMsg', '账号已禁用');
|
|
|
|
|
|
return Redirect::back();
|
|
|
- } else if ($user->status == 0 && $this->systemConfig['is_active_register'] && $user->is_admin == 0) {
|
|
|
+ } elseif ($user->status == 0 && $this->systemConfig['is_active_register'] && $user->is_admin == 0) {
|
|
|
Session::flash('errorMsg', '账号未激活,请先<a href="/activeUser?username=' . $user->username . '" target="_blank"><span style="color:#000">【激活账号】</span></a>');
|
|
|
|
|
|
return Redirect::back()->withInput();
|
|
|
@@ -58,7 +60,6 @@ class LoginController extends Controller
|
|
|
|
|
|
// 更新登录信息
|
|
|
$remember_token = "";
|
|
|
- User::query()->where('id', $user->id)->update(['last_login' => time()]);
|
|
|
if ($request->get('remember')) {
|
|
|
$remember_token = makeRandStr(20);
|
|
|
|
|
|
@@ -84,6 +85,9 @@ class LoginController extends Controller
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 写入登录日志
|
|
|
+ $this->addUserLoginLog($user->id, $request->getClientIp());
|
|
|
+
|
|
|
// 重新取出用户信息
|
|
|
$userInfo = User::query()->where('id', $user->id)->first();
|
|
|
|
|
|
@@ -127,4 +131,67 @@ class LoginController extends Controller
|
|
|
return Redirect::to('login')->cookie('remember', "", 36000);
|
|
|
}
|
|
|
|
|
|
+ // 添加用户登录日志
|
|
|
+ private function addUserLoginLog($userId, $ip)
|
|
|
+ {
|
|
|
+ // 调用淘宝IP接口查询IP信息
|
|
|
+ $ipInfo = $this->getIPInfo($ip);
|
|
|
+
|
|
|
+ $log = new UserLoginLog();
|
|
|
+ $log->user_id = $userId;
|
|
|
+ $log->ip = $ip;
|
|
|
+ $log->country = $ipInfo->country;
|
|
|
+ $log->country_id = $ipInfo->country_id;
|
|
|
+ $log->region = $ipInfo->region;
|
|
|
+ $log->region_id = $ipInfo->region_id;
|
|
|
+ $log->city = $ipInfo->city;
|
|
|
+ $log->city_id = $ipInfo->city_id;
|
|
|
+ $log->county = $ipInfo->county;
|
|
|
+ $log->county_id = $ipInfo->county_id;
|
|
|
+ $log->isp = $ipInfo->isp;
|
|
|
+ $log->isp_id = $ipInfo->isp_id;
|
|
|
+ $log->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取IP信息
|
|
|
+ private function getIPInfo($ip)
|
|
|
+ {
|
|
|
+ $url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' . $ip;
|
|
|
+ $result = $this->curlRequest($url);
|
|
|
+ Log::info("登录获取IP信息:" . $result);
|
|
|
+ $result = json_decode($result);
|
|
|
+ if (!$result) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($result->code) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发起一个CURL请求
|
|
|
+ private function curlRequest($url, $data = [])
|
|
|
+ {
|
|
|
+ $ch = curl_init();
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, 500);
|
|
|
+ // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
|
|
|
+ // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
+
|
|
|
+ // 如果data有数据,则用POST请求
|
|
|
+ if ($data) {
|
|
|
+ curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
+ }
|
|
|
+
|
|
|
+ $res = curl_exec($ch);
|
|
|
+ curl_close($ch);
|
|
|
+
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
}
|