chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | 文章模型
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\articles\model;
|
||||
|
||||
use think\model\concern\SoftDelete;
|
||||
use ywxapp\model\BaseModel;
|
||||
|
||||
class Article extends BaseModel
|
||||
{
|
||||
use SoftDelete;
|
||||
|
||||
protected $name = 'articles_article';
|
||||
protected $deleteTime = 'delete_at';
|
||||
protected $defaultSoftDelete = 0;
|
||||
|
||||
// 时间字段为 int 时间戳(与 install.sql 一致),开启自动维护
|
||||
protected $autoWriteTimestamp = true;
|
||||
protected $createTime = 'create_at';
|
||||
protected $updateTime = 'update_at';
|
||||
|
||||
protected $type = [
|
||||
'status' => 'integer',
|
||||
'is_top' => 'integer',
|
||||
'is_recommend' => 'integer',
|
||||
'view_count' => 'integer',
|
||||
'comment_count'=> 'integer',
|
||||
'like_count' => 'integer',
|
||||
];
|
||||
|
||||
// 发布中(未删除、已发布)
|
||||
public function scopeNormal($query)
|
||||
{
|
||||
$query->where('delete_at', 0)->where('status', 1);
|
||||
}
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(ArticleCategory::class, 'cid', 'id')
|
||||
->bind(['category_name' => 'name', 'category_alias' => 'alias']);
|
||||
}
|
||||
|
||||
public function author()
|
||||
{
|
||||
return $this->belongsTo(\ywxapp\model\MemberUser::class, 'uid', 'uid')
|
||||
->bind(['author_name' => 'nickname']);
|
||||
}
|
||||
|
||||
public function tags()
|
||||
{
|
||||
return $this->belongsToMany(ArticleTag::class, ArticleArticleTag::class, 'tid', 'aid');
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表查询(支持分类/标签/关键词)
|
||||
*/
|
||||
public static function listArticles(array $where = [], int $page = 1, int $size = 10, string $order = 'is_top desc, create_at desc')
|
||||
{
|
||||
$query = self::with(['category'])->scope('normal');
|
||||
|
||||
if (!empty($where['cid'])) {
|
||||
$query->where('cid', $where['cid']);
|
||||
}
|
||||
if (!empty($where['tid'])) {
|
||||
$ids = ArticleArticleTag::where('tid', $where['tid'])->column('aid');
|
||||
$query->whereIn('id', $ids ?: [0]);
|
||||
}
|
||||
if (!empty($where['keyword'])) {
|
||||
$kw = $where['keyword'];
|
||||
$query->where(function ($q) use ($kw) {
|
||||
$q->whereLike('title', "%{$kw}%")->whereOr('summary', 'like', "%{$kw}%");
|
||||
});
|
||||
}
|
||||
if (!empty($where['is_recommend'])) {
|
||||
$query->where('is_recommend', 1);
|
||||
}
|
||||
|
||||
return $query->orderRaw($order)->paginate([
|
||||
'list_rows' => $size,
|
||||
'page' => $page,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getDetail(int $id)
|
||||
{
|
||||
return self::with(['category', 'tags', 'author'])
|
||||
->scope('normal')
|
||||
->find($id);
|
||||
}
|
||||
|
||||
public static function prev(int $id, int $cid = 0)
|
||||
{
|
||||
$query = self::scope('normal')->where('id', '<', $id);
|
||||
if ($cid) {
|
||||
$query->where('cid', $cid);
|
||||
}
|
||||
return $query->order('id', 'desc')->field('id,title')->find();
|
||||
}
|
||||
|
||||
public static function next(int $id, int $cid = 0)
|
||||
{
|
||||
$query = self::scope('normal')->where('id', '>', $id);
|
||||
if ($cid) {
|
||||
$query->where('cid', $cid);
|
||||
}
|
||||
return $query->order('id', 'asc')->field('id,title')->find();
|
||||
}
|
||||
|
||||
public static function hot(int $limit = 10)
|
||||
{
|
||||
return self::scope('normal')
|
||||
->order('view_count', 'desc')
|
||||
->limit($limit)
|
||||
->field('id,title,cid,view_count')
|
||||
->select();
|
||||
}
|
||||
|
||||
public static function related(int $id, int $cid = 0, int $limit = 5)
|
||||
{
|
||||
$query = self::scope('normal')->where('id', '<>', $id);
|
||||
if ($cid) {
|
||||
$query->where('cid', $cid);
|
||||
}
|
||||
return $query->order('create_at', 'desc')
|
||||
->limit($limit)
|
||||
->field('id,title,cid')
|
||||
->select();
|
||||
}
|
||||
|
||||
public static function ensureSchema(): void
|
||||
{
|
||||
$prefix = self::currentPrefix();
|
||||
self::ensureTableFromInstall($prefix, 'articles_article');
|
||||
self::ensureTableFromInstall($prefix, 'articles_article_tag');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user