增加访问记录功能

This commit is contained in:
2023-06-28 11:12:30 +08:00
parent a12cca37e4
commit 65e1800c6f
18 changed files with 447 additions and 98 deletions

View File

@@ -15,7 +15,7 @@ class Admin extends Model
{
if(empty($value)){
return '/static/images/avatar.jpeg';
return '/static/images/avatar.png';
}
return \get_source_link($value);

View File

@@ -23,6 +23,9 @@ class Post extends Base
use SoftDelete;
//
public const CACHE_KEY_HITS = 'cache_hits_';
public static $autoClearCache = [
[
'name' => 'top_post'
@@ -184,7 +187,7 @@ class Post extends Base
public function getPosterAttr($value)
{
if (empty($value)) {
$value = '/static/images/avatar.jpeg';
$value = '/static/images/avatar.png';
}
return get_source_link($value);
@@ -233,4 +236,31 @@ class Post extends Base
}
return $end_content;
}
public function getHitsTitleAttr()
{
$cache_key = static::CACHE_KEY_HITS.$this->getAttr('id');
$value = Cache::get($cache_key);
if(!is_null($value)) {
return $value;
}
$value = $this->getData('hits');
Cache::set($cache_key, $value, 600);
return $value;
}
public function setHitsAttr($value)
{
$cache_key = static::CACHE_KEY_HITS.$this->getAttr('id');
$this->getAttr('hits_title');
Cache::inc($cache_key);
return $value;
}
}

44
app/model/PostVisit.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
declare (strict_types=1);
namespace app\model;
use think\Model;
/**
* @mixin \think\Model
*/
class PostVisit extends Model
{
//
public function getAvatarSrcAttr()
{
$value = $this->getAttr('avatar');
if(empty($value)) {
$value = '/static/images/avatar.png';
}
return $value;
}
public function getNicknameTitleAttr()
{
$value = $this->getAttr('nickname');
if(empty($value)) {
$value = 'IP用户:'.$this->getAttr('ip');
}
return $value;
}
public function getCreateTimeTitleAttr()
{
$value = $this->getData('create_time');
return show_time_ago($value);
}
}

View File

@@ -19,7 +19,7 @@ class User extends Model
public function getAvatarAttr($value)
{
if(empty($value)){
return '/static/images/avatar.jpeg';
return '/static/images/avatar.png';
}
return \get_source_link($value);