mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
增加访问分析缓存
This commit is contained in:
@@ -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,7 +46,6 @@ 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')
|
||||
@@ -56,6 +56,9 @@ class Post extends Common
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use app\common\ColumnFormat;
|
||||
use think\migration\db\Column;
|
||||
use think\migration\Migrator;
|
||||
|
||||
class CreateTablePostVisit extends Migrator
|
||||
@@ -37,6 +38,12 @@ class CreateTablePostVisit extends Migrator
|
||||
$table->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');
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
<div class="ul-avatar-sidebar-list-item-title">{$vo.nickname_title}</div>
|
||||
<div class="ul-avatar-sidebar-list-item-intro" style="color: #000;">
|
||||
<span>{$vo.create_time_title}</span>
|
||||
<span>{$vo.client_name}</span>
|
||||
<span>{$vo.client_os_name}</span>
|
||||
<a href="{$vo.post.read_url}" title="{$vo.post.title}">《{$vo.post.title}》</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -156,7 +156,11 @@
|
||||
<div class="ul-avatar-sidebar-list-item-img" style="background-image: url({$vo.avatar_src});"></div>
|
||||
<div class="ul-avatar-sidebar-list-item-info">
|
||||
<div class="ul-avatar-sidebar-list-item-title">{$vo.nickname_title}</div>
|
||||
<div class="ul-avatar-sidebar-list-item-intro">{$vo.create_time_title}</div>
|
||||
<div class="ul-avatar-sidebar-list-item-intro">
|
||||
{$vo.create_time_title}
|
||||
{$vo.client_name}
|
||||
{$vo.client_os_name}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/volist}
|
||||
@@ -377,6 +381,10 @@
|
||||
});
|
||||
</script>
|
||||
|
||||
<script>
|
||||
$.get('{:url("parseVisit",["visit_id"=>$model_visit->id])}')
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user