diff --git a/app/index/controller/Post.php b/app/index/controller/Post.php index c40b11e..e394721 100644 --- a/app/index/controller/Post.php +++ b/app/index/controller/Post.php @@ -6,6 +6,7 @@ namespace app\index\controller; use app\model\Post as ModelPost; use app\model\PostVisit; +use DeviceDetector\DeviceDetector; use think\facade\Cache; use think\facade\Db; use think\facade\View; @@ -45,8 +46,7 @@ class Post extends Common $model_post->save(); - $this->recordVisit($model_post->id); - + $list_last_visit = PostVisit::where('post_id', $model_post->id) ->order('id desc') ->group('ip,uid') @@ -55,6 +55,9 @@ class Post extends Common ->select(); View::assign('post', $model_post); View::assign('list_last_visit', $list_last_visit); + + $model_visit = $this->recordVisit($model_post->id); + View::assign('model_visit', $model_visit); return View::fetch(); } @@ -67,6 +70,37 @@ class Post extends Common $model_visit->avatar = $this->userinfo['avatar'] ?? ''; $model_visit->nickname = $this->userinfo['nickname'] ?? ''; $model_visit->ip = $this->request->ip(); + $model_visit->save(); + + return $model_visit; + } + + public function parseVisit($visit_id) + { + $model_visit = PostVisit::find($visit_id); + + $user_agent = $this->request->header('user-agent'); + + $dd = new DeviceDetector($user_agent); + + $cache = Cache::instance(); + + $dd->setCache( + new \DeviceDetector\Cache\PSR16Bridge($cache) + ); + + $dd->parse(); + + $model_visit->client_bot = $dd->getBot() ?: ''; + $model_visit->client = $dd->getClient(); + $model_visit->client_os = $dd->getOs(); + $model_visit->client_device_name = $dd->getDeviceName(); + $model_visit->client_brand_name = $dd->getBrandName(); + $model_visit->client_model = $dd->getModel(); + + $model_visit->save(); + + return json_message(); } } diff --git a/app/model/PostVisit.php b/app/model/PostVisit.php index 8adaeb3..25416e3 100644 --- a/app/model/PostVisit.php +++ b/app/model/PostVisit.php @@ -14,6 +14,11 @@ class PostVisit extends Model { // + protected $json = [ + 'client_os', + 'client', + ]; + public function post() { return $this->belongsTo(Post::class, 'post_id'); @@ -55,4 +60,21 @@ class PostVisit extends Model return show_time_ago($value); } + + public function getClientNameAttr() + { + $client = $this->getAttr('client'); + + return $client->name ?? ''; + } + + public function getClientOsNameAttr() + { + $client_os = $this->getAttr('client_os'); + + $name = $client_os->name ?? ''; + $version = $client_os->version ?? ''; + + return "$name $version"; + } } diff --git a/composer.json b/composer.json index 58f6bb4..c33b80f 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "jaeger/phpquery-single": "^1.1", "league/html-to-markdown": "^5.1", "topthink/think-filesystem": "^2.0", - "mibe/feedwriter": "^1.1" + "mibe/feedwriter": "^1.1", + "matomo/device-detector": "^6.1" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/database/migrations/20230628025857_create_table_post_visit.php b/database/migrations/20230628025857_create_table_post_visit.php index 5ac3b2b..096f861 100644 --- a/database/migrations/20230628025857_create_table_post_visit.php +++ b/database/migrations/20230628025857_create_table_post_visit.php @@ -1,6 +1,7 @@ addColumn(ColumnFormat::stringNormal('uid')); $table->addColumn(ColumnFormat::stringNormal('nickname')); $table->addColumn(ColumnFormat::stringUrl('avatar')); + $table->addColumn(ColumnFormat::stringNormal('client_bot')); + $table->addColumn(Column::text('client')); + $table->addColumn(Column::text('client_os')); + $table->addColumn(ColumnFormat::stringNormal('client_device_name')); + $table->addColumn(ColumnFormat::stringNormal('client_brand_name')); + $table->addColumn(ColumnFormat::stringNormal('client_model')); $table->addIndex('delete_time'); $table->addIndex('post_id'); $table->addIndex('ip'); diff --git a/view/index/index/visit.html b/view/index/index/visit.html index 34d6267..c8da372 100644 --- a/view/index/index/visit.html +++ b/view/index/index/visit.html @@ -22,6 +22,8 @@
{$vo.nickname_title}
{$vo.create_time_title} + {$vo.client_name} + {$vo.client_os_name} 《{$vo.post.title}》
diff --git a/view/index/post/read.html b/view/index/post/read.html index 23928af..da74004 100644 --- a/view/index/post/read.html +++ b/view/index/post/read.html @@ -156,7 +156,11 @@
{$vo.nickname_title}
-
{$vo.create_time_title}
+
+ {$vo.create_time_title} + {$vo.client_name} + {$vo.client_os_name} +
{/volist} @@ -377,6 +381,10 @@ }); + + \ No newline at end of file