mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
增加访问记录功能
This commit is contained in:
@@ -5,17 +5,16 @@ declare(strict_types=1);
|
||||
namespace app\index\controller;
|
||||
|
||||
use app\model\Post as ModelPost;
|
||||
use app\model\PostCategory;
|
||||
use app\model\PostVisit;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Db;
|
||||
use think\facade\View;
|
||||
use think\model\Relation;
|
||||
use think\Request;
|
||||
|
||||
class Post extends Common
|
||||
{
|
||||
|
||||
/**
|
||||
* 显示指定的资源
|
||||
* 显示指定的资源.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
@@ -29,31 +28,45 @@ class Post extends Common
|
||||
$model_post = Cache::get($cache_key);
|
||||
|
||||
if (empty($model_post)) {
|
||||
|
||||
$model_post = ModelPost::with([
|
||||
'comments' => function (Relation $query) {
|
||||
$query->order('id asc');
|
||||
}
|
||||
},
|
||||
])->where('uid', $uid)->find();
|
||||
|
||||
if (empty($model_post)) {
|
||||
return $this->error('链接已失效', '/');
|
||||
}
|
||||
|
||||
Cache::set($cache_key, $model_post,600);
|
||||
Cache::set($cache_key, $model_post, 600);
|
||||
}
|
||||
|
||||
|
||||
$model_post->hits = $model_post->hits + 1;
|
||||
$model_post->hits = Db::raw('hits + 1');
|
||||
|
||||
$model_post->save();
|
||||
|
||||
|
||||
$this->recordVisit($model_post->id);
|
||||
|
||||
$list_last_visit = PostVisit::where('post_id', $model_post->id)
|
||||
->order('id desc')
|
||||
->group('ip,uid')
|
||||
->limit(12)
|
||||
->cache(60)
|
||||
->select();
|
||||
View::assign('post', $model_post);
|
||||
|
||||
View::assign('list_last_visit', $list_last_visit);
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
public function recordVisit($post_id)
|
||||
{
|
||||
$model_visit = new PostVisit();
|
||||
$model_visit->uid = $this->userinfo['uid'] ?? '';
|
||||
$model_visit->post_id = $post_id;
|
||||
$model_visit->avatar = $this->userinfo['avatar'] ?? '';
|
||||
$model_visit->nickname = $this->userinfo['nickname'] ?? '';
|
||||
$model_visit->ip = $this->request->ip();
|
||||
$model_visit->save();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user