Files
YwxAppThink/addon/blog/controller/backend/Comment.php
T

182 lines
5.5 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace addon\blog\controller\backend;
use addon\blog\model\BlogArticle as ArticleModel;
use addon\blog\model\BlogComment as CommentModel;
use think\facade\Request;
use think\facade\View;
use ywxapp\controller\BackendBase;
/**
* Comment 类
*
* @author ywxapp <admin@ywxapp.cn>
*/
class Comment extends BackendBase
{
protected $noNeedVerify = ['*'];
protected function initialize()
{}
/**
* 评论列表(ajax 请求返回 layui table 所需 JSON,普通请求渲染视图)
*/
public function index()
{
$status = Request::param('status', 'all');
$keyword = Request::param('keyword', '');
$page = Request::param('page', 1);
$limit = Request::param('limit', 15);
if (Request::isAjax()) {
$query = CommentModel::with(['user', 'article']);
if ($status !== 'all') {
$query->where('status', $status);
}
if ($keyword) {
$query->where('content', 'like', "%{$keyword}%");
}
$paginator = $query->order('create_at', 'desc')
->paginate(['list_rows' => $limit, 'page' => $page]);
$rows = [];
foreach ($paginator->items() as $c) {
$createText = (function ($t) {
if ($t instanceof \DateTime) return $t->format('Y-m-d H:i');
if (is_numeric($t)) return date('Y-m-d H:i', (int) $t);
$ts = strtotime((string) $t);
return $ts !== false ? date('Y-m-d H:i', $ts) : (string) $t;
})($c->create_at);
$rows[] = [
'id' => $c->id,
'nickname' => $c->user->nickname ?? '-',
'avatar' => $c->user->avatar ?? '',
'content' => $c->content,
'article_id' => $c->aid,
'article_title' => $c->article->title ?? '-',
'article_url' => $c->article ? (string) url('blog/backend.article/edit', ['id' => $c->aid]) : '',
'status' => $c->status,
'create_text' => $createText,
];
}
return json([
'code' => 0,
'msg' => '',
'count' => $paginator->total(),
'data' => $rows,
]);
}
return View::fetch('comment/index', [
'status' => $status,
'keyword' => $keyword,
]);
}
/**
* 审核通过
*/
public function approve($id)
{
$comment = CommentModel::find($id);
if ($comment) {
$comment->status = 1;
$comment->save();
ArticleModel::where('id', $comment->aid)->inc('comment_count')->update();
return json(['code' => 1, 'msg' => '评论已通过']);
}
return json(['code' => 0, 'msg' => '评论不存在']);
}
/**
* 拒绝评论
*/
public function reject($id)
{
$comment = CommentModel::find($id);
if ($comment) {
$comment->status = 0;
$comment->save();
ArticleModel::where('id', $comment->aid)->dec('comment_count')->update();
return json(['code' => 1, 'msg' => '评论已拒绝']);
}
return json(['code' => 0, 'msg' => '评论不存在']);
}
/**
* 删除评论
*/
public function delete($id)
{
$comment = CommentModel::find($id);
if ($comment) {
$this->deleteChildren($id);
ArticleModel::where('id', $comment->aid)->dec('comment_count')->update();
return json(['code' => 1, 'msg' => '评论已删除']);
}
return json(['code' => 0, 'msg' => '评论不存在']);
}
/**
* 递归删除子评论
*/
private function deleteChildren($parentId)
{
$children = CommentModel::where('pid', $parentId)->select();
foreach ($children as $child) {
$this->deleteChildren($child->id);
$child->delete();
}
}
/**
* 批量操作
*/
public function batch()
{
$action = Request::post('action');
$ids = Request::post('ids/a');
if (empty($ids)) {
return json(['code' => 0, 'msg' => '请选择评论']);
}
switch ($action) {
case 'approve':
CommentModel::whereIn('id', $ids)->update(['status' => 1]);
return json(['code' => 1, 'msg' => '批量通过成功']);
case 'reject':
CommentModel::whereIn('id', $ids)->update(['status' => 0]);
return json(['code' => 1, 'msg' => '批量拒绝成功']);
case 'delete':
CommentModel::destroy($ids);
return json(['code' => 1, 'msg' => '批量删除成功']);
default:
return json(['code' => 0, 'msg' => '无效操作']);
}
}
}