123 lines
2.9 KiB
PHP
123 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Modules\Cms\Models;
|
|
|
|
use App\Models\Model;
|
|
use App\Traits\HasClicks;
|
|
use App\Traits\HasCovers;
|
|
use App\Traits\HasStatus;
|
|
use App\Traits\OrderByIdDesc;
|
|
use Encore\Admin\Traits\Resizable;
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Cms\Traits\HasTags;
|
|
use Modules\Task\Facades\TaskFacade;
|
|
use Modules\Task\Models\Task;
|
|
use Overtrue\LaravelFavorite\Traits\Favoriteable;
|
|
use Overtrue\LaravelSubscribe\Traits\Subscribable;
|
|
use Overtrue\LaravelVersionable\Versionable;
|
|
|
|
class Article extends Model
|
|
{
|
|
|
|
use Cachable,
|
|
HasClicks,
|
|
HasCovers,
|
|
HasTags,
|
|
Subscribable,
|
|
Favoriteable,
|
|
HasStatus,
|
|
OrderByIdDesc,
|
|
Resizable,
|
|
Versionable,
|
|
SoftDeletes;
|
|
|
|
protected $table = 'cms_articles';
|
|
|
|
protected $casts = [
|
|
'pictures' => 'json',
|
|
'attachments' => 'json',
|
|
];
|
|
|
|
/**
|
|
* 不参与版本记录的字段
|
|
*
|
|
* @var array|string[]
|
|
*/
|
|
protected array $dontVersionable = ['clicks', 'updated_at'];
|
|
|
|
/**
|
|
* Notes: 阅读记录
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date: 2022/10/20 9:57
|
|
* @return HasMany
|
|
*/
|
|
public function logs(): HasMany
|
|
{
|
|
return $this->hasMany(Log::class);
|
|
}
|
|
|
|
/**
|
|
* Notes: 阅读
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date: 2022/10/20 9:57
|
|
* @param int $user_id
|
|
*/
|
|
public function read($user_id = '')
|
|
{
|
|
$this->incrementClicks();
|
|
if ($user_id) {
|
|
$this->logs()->create([
|
|
'user_id' => $user_id
|
|
]);
|
|
|
|
if (Task::query()->shown()->where('key', 'tour_article')->first()) {
|
|
# todo 阅读得水滴
|
|
TaskFacade::do('tour_article', $user_id);
|
|
}
|
|
|
|
if (Task::query()->shown()->where('key',
|
|
'read_article')->first() && $this->categories()->Health()->first()) {
|
|
# todo 阅读100次得水滴
|
|
TaskFacade::do('read_article', $user_id);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes : 文章所属分类
|
|
*
|
|
* @Date : 2021/4/15 12:44 下午
|
|
* @Author : < Jason.C >
|
|
* @return BelongsToMany
|
|
*/
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Category::class, 'cms_article_category')
|
|
->using(ArticleCategory::class)
|
|
->withTimestamps();
|
|
}
|
|
|
|
/**
|
|
* Notes : 获取附件的下载地址
|
|
*
|
|
* @Date : 2021/4/16 12:01 下午
|
|
* @Author : < Jason.C >
|
|
* @return Collection
|
|
*/
|
|
public function getAttachmentsUrlAttribute(): Collection
|
|
{
|
|
return collect($this->attachments)->map(function ($item) {
|
|
return Storage::url($item);
|
|
});
|
|
}
|
|
|
|
} |