1
0
admin 7 жил өмнө
parent
commit
b49d4cef8d

+ 70 - 3
app/Http/Controllers/LoginController.php

@@ -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;
+    }
 }

+ 22 - 0
app/Http/Models/UserLoginLog.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+/**
+ * 用户登录日志
+ * Class UserLoginLog
+ *
+ * @package App\Http\Models
+ */
+class UserLoginLog extends Model
+{
+    protected $table = 'user_login_log';
+    protected $primaryKey = 'id';
+
+    function user()
+    {
+        return $this->hasOne(User::class, 'id', 'user_id');
+    }
+}

+ 24 - 0
sql/db.sql

@@ -1023,6 +1023,30 @@ CREATE TABLE `marketing` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='营销';
 
 
+-- ----------------------------
+-- Table structure for `user_login_log`
+-- ----------------------------
+CREATE TABLE `user_login_log` (
+	`id` INT(11) NOT NULL AUTO_INCREMENT,
+	`user_id` INT(11) NOT NULL DEFAULT '0',
+	`ip` CHAR(20) NOT NULL,
+	`country` CHAR(20) NOT NULL,
+	`country_id` CHAR(20) NOT NULL,
+	`region` CHAR(20) NOT NULL,
+	`region_id` CHAR(20) NOT NULL,
+	`city` CHAR(20) NOT NULL,
+	`city_id` CHAR(20) NOT NULL,
+	`county` CHAR(20) NOT NULL,
+	`county_id` CHAR(20) NOT NULL,
+	`isp` CHAR(20) NOT NULL,
+	`isp_id` CHAR(20) NOT NULL,
+	`created_at` DATETIME NOT NULL,
+	`updated_at` DATETIME NOT NULL,
+	PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户登录日志';
+
+
+
 /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
 /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
 /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;

+ 19 - 0
sql/update/20180823.sql

@@ -0,0 +1,19 @@
+-- 加入登录日志表
+CREATE TABLE `user_login_log` (
+	`id` INT(11) NOT NULL AUTO_INCREMENT,
+	`user_id` INT(11) NOT NULL DEFAULT '0',
+	`ip` CHAR(20) NOT NULL,
+	`country` CHAR(20) NOT NULL,
+	`country_id` CHAR(20) NOT NULL,
+	`region` CHAR(20) NOT NULL,
+	`region_id` CHAR(20) NOT NULL,
+	`city` CHAR(20) NOT NULL,
+	`city_id` CHAR(20) NOT NULL,
+	`county` CHAR(20) NOT NULL,
+	`county_id` CHAR(20) NOT NULL,
+	`isp` CHAR(20) NOT NULL,
+	`isp_id` CHAR(20) NOT NULL,
+	`created_at` DATETIME NOT NULL,
+	`updated_at` DATETIME NOT NULL,
+	PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户登录日志';