181 lines
5.3 KiB
PHP
181 lines
5.3 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;
|
|
|
|
use addon\blog\model\BlogArticle as ArticleModel;
|
|
use addon\blog\model\BlogComment as CommentModel;
|
|
use think\facade\Request;
|
|
use think\facade\View;
|
|
use think\facade\Session;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* Comment 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class Comment extends BlogFrontend
|
|
{
|
|
/**
|
|
* 公开访问(登录校验由方法内部通过 $this->auth 处理,会员统一以 uid 标识)
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = ['*'];
|
|
|
|
/**
|
|
* 发表评论
|
|
*/
|
|
public function add()
|
|
{
|
|
if (! $this->auth->isLogin) {
|
|
return json(['code' => 0, 'msg' => '请先登录']);
|
|
}
|
|
$uid = $this->auth->model->uid;
|
|
|
|
$data = Request::post();
|
|
|
|
$validate = new \think\Validate([
|
|
'content|评论内容' => 'require|min:5|max:500',
|
|
'article_id|文章ID' => 'require|number',
|
|
'parent_id|父评论ID' => 'number'
|
|
]);
|
|
|
|
if (!$validate->check($data)) {
|
|
return json(['code' => 0, 'msg' => $validate->getError()]);
|
|
}
|
|
|
|
$article = ArticleModel::find($data['article_id']);
|
|
if (!$article) {
|
|
return json(['code' => 0, 'msg' => '文章不存在']);
|
|
}
|
|
|
|
if ($data['parent_id'] > 0) {
|
|
$parentComment = CommentModel::find($data['parent_id']);
|
|
if (!$parentComment || $parentComment->aid != $data['article_id']) {
|
|
return json(['code' => 0, 'msg' => '回复的评论不存在']);
|
|
}
|
|
}
|
|
|
|
$sensitiveWords = ['垃圾', '广告', '违法', '政治'];
|
|
$content = str_replace($sensitiveWords, '***', $data['content']);
|
|
|
|
$comment = new CommentModel();
|
|
$comment->uid = $uid;
|
|
$comment->aid = $data['article_id'];
|
|
$comment->pid = $data['parent_id'] ?? 0;
|
|
$comment->content = $content;
|
|
$comment->status = 1;
|
|
$comment->ip = Request::ip();
|
|
$comment->user_agent = Request::server('HTTP_USER_AGENT');
|
|
$comment->save();
|
|
|
|
$article->comment_count = Db::raw('comment_count + 1');
|
|
$article->save();
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => '评论发表成功',
|
|
'data' => [
|
|
'id' => $comment->id,
|
|
'content' => $comment->content,
|
|
'create_at' => $comment->create_text,
|
|
'member' => [
|
|
'uid' => $uid,
|
|
'nickname' => $this->auth->model->nickname,
|
|
'avatar' => $this->auth->model->avatar ?? '/static/images/avatar.png'
|
|
],
|
|
'parent_id' => $comment->pid
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 删除评论
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
if (! $this->auth->isLogin) {
|
|
return json(['code' => 0, 'msg' => '请先登录']);
|
|
}
|
|
$uid = $this->auth->model->uid;
|
|
|
|
$comment = CommentModel::find($id);
|
|
if (!$comment) {
|
|
return json(['code' => 0, 'msg' => '评论不存在']);
|
|
}
|
|
|
|
if ($comment->uid != $uid) {
|
|
return json(['code' => 0, 'msg' => '无权限操作']);
|
|
}
|
|
|
|
$this->deleteCommentWithChildren($id);
|
|
|
|
$article = ArticleModel::find($comment->aid);
|
|
if ($article) {
|
|
$article->comment_count = Db::raw('comment_count - 1');
|
|
$article->save();
|
|
}
|
|
|
|
return json(['code' => 1, 'msg' => '评论已删除']);
|
|
}
|
|
|
|
/**
|
|
* 递归删除评论及其子评论
|
|
*/
|
|
private function deleteCommentWithChildren($commentId)
|
|
{
|
|
$comment = CommentModel::find($commentId);
|
|
if ($comment) {
|
|
$children = CommentModel::where('pid', $commentId)->select();
|
|
foreach ($children as $child) {
|
|
$this->deleteCommentWithChildren($child->id);
|
|
}
|
|
$comment->delete();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 评论点赞
|
|
*/
|
|
public function like($id)
|
|
{
|
|
if (! $this->auth->isLogin) {
|
|
return json(['code' => 0, 'msg' => '请先登录']);
|
|
}
|
|
$uid = $this->auth->model->uid;
|
|
|
|
$comment = CommentModel::find($id);
|
|
if (!$comment) {
|
|
return json(['code' => 0, 'msg' => '评论不存在']);
|
|
}
|
|
|
|
$likeKey = "comment_like_{$id}_{$uid}";
|
|
if (Session::get($likeKey)) {
|
|
Session::delete($likeKey);
|
|
$comment->like_count = max(0, $comment->like_count - 1);
|
|
$action = 'cancel';
|
|
} else {
|
|
Session::set($likeKey, true);
|
|
$comment->like_count += 1;
|
|
$action = 'add';
|
|
}
|
|
|
|
$comment->save();
|
|
|
|
return json([
|
|
'code' => 1,
|
|
'msg' => $action == 'add' ? '点赞成功' : '已取消点赞',
|
|
'count' => $comment->like_count,
|
|
'action' => $action
|
|
]);
|
|
}
|
|
}
|