140 lines
3.6 KiB
PHP
140 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\App\Vote;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Overtrue\LaravelFollow\Traits\CanBeLiked;
|
|
use Actuallymab\LaravelComment\Contracts\Commentable;
|
|
use Actuallymab\LaravelComment\HasComments;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use App\Http\Response\ResponseJson;
|
|
class Moment extends Base implements Commentable
|
|
{
|
|
// use SoftDeletes;
|
|
use CanBeLiked;
|
|
use HasComments;
|
|
use ResponseJson;
|
|
protected $fillable = [];
|
|
protected $guarded = [];
|
|
public const RDMOMENTKEY = 'rd_moment_';
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id', 'id');
|
|
}
|
|
|
|
public function topic()
|
|
{
|
|
return $this->HasOne(MomentTopic::class, 'id', 'topic_id');
|
|
}
|
|
|
|
public function unlinkLogs()
|
|
{
|
|
return $this->hasMany(UnlikeMomentLog::class)->where('user_id', auth()->id());
|
|
}
|
|
|
|
public function unlikeMoments()
|
|
{
|
|
return $this->hasMany(UnlikeMomentLog::class);
|
|
}
|
|
|
|
public function vote()
|
|
{
|
|
return $this->belongsTo(Vote::class);
|
|
}
|
|
|
|
public static function cacheMoemntKey($moment_id)
|
|
{
|
|
return self::RDMOMENTKEY . $moment_id;
|
|
}
|
|
|
|
//缓存动态信息
|
|
public function cacheMoment()
|
|
{
|
|
try {
|
|
$rd_moment_key = self::cacheMoemntKey($this->id);
|
|
$result = Cache::put($rd_moment_key, $this, now()->addHours(24));
|
|
return $result;
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function getCacheMoment()
|
|
{
|
|
try {
|
|
$rd_moment_key = self::cacheMoemntKey($this->id);
|
|
$moment = Cache::get($rd_moment_key);
|
|
return $moment;
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//修改缓存点赞信息
|
|
public function updateCacheMomentLikeNum($type = 'ADD')
|
|
{
|
|
try {
|
|
$moment = $this->getCacheMoment();
|
|
if ($moment) {
|
|
if ($type == 'ADD') {
|
|
$moment->momentLikerCount = $moment->momentLikerCount + 1;
|
|
} else {
|
|
$moment->momentLikerCount = $moment->momentLikerCount - 1;
|
|
}
|
|
}
|
|
$this->cacheMoment();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//修改缓存点赞信息
|
|
public function updateCacheMomentLikeNumV2($momentLikerCount)
|
|
{
|
|
try {
|
|
$moment = $this->getCacheMoment();
|
|
if ($moment && $moment->momentLikerCount != $momentLikerCount) {
|
|
$moment->momentLikerCount = $momentLikerCount;
|
|
$moment->cacheMoment();
|
|
}
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//修改缓存评论信息
|
|
public function updateCacheMomentCommentNum($type = 'ADD')
|
|
{
|
|
try {
|
|
$moment = $this->getCacheMoment();
|
|
if (!$moment) {
|
|
$moment = $this;
|
|
}
|
|
if ($type == 'ADD') {
|
|
$moment->momentCommentCount = $moment->momentCommentCount + 1;
|
|
} else {
|
|
$moment->momentCommentCount = $moment->momentCommentCount - 1;
|
|
}
|
|
$moment->cacheMoment();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return false;
|
|
}
|
|
}
|
|
/**
|
|
public function getPhotosAttribute(){
|
|
|
|
}
|
|
* **/
|
|
}
|