Files
ulthon_information/app/index/controller/Post.php
2023-06-27 00:51:30 +08:00

60 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace app\index\controller;
use app\model\Post as ModelPost;
use app\model\PostCategory;
use think\facade\Cache;
use think\facade\View;
use think\model\Relation;
use think\Request;
class Post extends Common
{
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($uid)
{
//
$cache_key = 'post_' . $uid;
$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);
}
$model_post->hits = $model_post->hits + 1;
$model_post->save();
View::assign('post', $model_post);
return View::fetch();
}
}