feat: 新增文章评论分页列表接口

This commit is contained in:
2025-01-15 17:56:39 +08:00
parent 4a2132336a
commit 25a0b32ad5
4 changed files with 125 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;
use app\admin\model\v1\ArticleLeaveMessageModel;
class ArticleLeaveMessage
{
// 文章留言分页列表
public function index()
{
// 获取参数
$param = request()->param([
'title',
'is_audited',
'created_at',
'page/a' => 1,
'size/d' => 20
]);
// 获取文章留言列表
$data = ArticleLeaveMessageModel::withoutField([
'ip',
'user_agent'
])
->withJoin(['article' => function($query) use($param) {
if (!empty($param['title'])) {
$query->where('article.title', 'like', '%' . $param['title'] . '%');
}
}])
->isAudited($param['is_audited']??null)
->withSearch(['created_at'], [
'created_at' => $param['created_at']??'',
])
->order(['id' => 'desc'])
->hidden(['article_id', 'article'])
->paginate([
'page' => $param['page'],
'list_rows' => $param['size'],
])
->bindAttr('article', ['article_title' => 'title']);
return success('获取成功', $data);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare (strict_types = 1);
namespace app\admin\model\v1;
use app\common\model\ArticleLeaveMessageBaseModel;
/**
* @mixin \think\Model
*/
class ArticleLeaveMessageModel extends ArticleLeaveMessageBaseModel
{
// 关联文章
public function article()
{
return $this->belongsTo(ArticleModel::class, 'article_id', 'id');
}
// 审核状态查询
public function scopeIsAudited($query, $is_audited)
{
if (is_null($is_audited)) {
return;
}
$query->where('is_audited', '=', $is_audited);
}
// 搜索留言内容
public function searchCreatedAtAttr($query, $value, $data)
{
if (empty($value)) {
return;
}
$val = explode(',', $value);
if (count($val) > 1) {
$query->whereBetweenTime('created_at', $val[0], $val[1]);
} else {
$query->whereTime('created_at', '>=', $val[0]);
}
}
}

View File

@@ -72,6 +72,12 @@ Route::group('v1', function () {
// 分类删除
Route::delete('delete/:id', 'ArticleCategory/delete');
});
// 文章留言评论
Route::group('message', function () {
// 留言列表
Route::get('index', 'ArticleLeaveMessage/index');
});
});
})->prefix('v1.');

View File

@@ -0,0 +1,31 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
use think\Model;
/**
* @mixin \think\Model
*/
class ArticleLeaveMessageBaseModel extends Model
{
// 表名
protected $name = 'article_leave_message';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'article_id' => 'int',
'name' => 'string',
'email' => 'string',
'content' => 'string',
'ip' => 'string',
'user_agent' => 'string',
'is_audited' => 'int',
'created_at' => 'datetime',
];
}