Skip to content

Commit 24c4cf4

Browse files
committedDec 24, 2022
更新:重写评论
·
v2.7.7v2.1.7
1 parent 33b4565 commit 24c4cf4

18 files changed

+473
-48
lines changed
 

‎app/Plugins/Comment/Comment.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
<?php
22

3-
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
411
namespace App\Plugins\Comment;
512

613
/**
7-
* Class Comment
8-
* @author Inkedus
9-
* @link https://github.com/zhuchunshu/sf-comment
10-
* @package 评论组件
14+
* Class Comment.
15+
* @see https://github.com/zhuchunshu/sf-comment
1116
* @name Comment
1217
* @version 1.0.0
1318
*/
1419
class Comment
1520
{
16-
public function handler(){
17-
$this->helpers();
21+
public function handler()
22+
{
23+
$this->helpers();
1824
$this->setting();
25+
$this->bootstrap();
26+
}
27+
28+
private function setting()
29+
{
30+
require_once __DIR__ . '/setting.php';
31+
}
32+
33+
private function helpers()
34+
{
35+
require_once __DIR__ . '/helpers.php';
1936
}
2037

21-
public function setting(){
22-
require_once __DIR__."/setting.php";
38+
private function bootstrap()
39+
{
40+
require_once __DIR__ . '/bootstrap.php';
2341
}
24-
25-
public function helpers(){
26-
require_once __DIR__."/helpers.php";
27-
}
28-
}
42+
}

‎app/Plugins/Comment/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
Itf()->add('create-topic-comment-handle-middleware-end', 0, \App\Plugins\Comment\src\Handler\Middleware\Create\Topic\EndMiddleware::class);

‎app/Plugins/Comment/resources/views/Widget/topic.blade.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,23 @@
4949
<a class="btn-close btn-close-white" data-bs-dismiss="alert" aria-label="close"></a>
5050
</div>
5151

52-
<div class="mb-1" id="topic-comment-model">
53-
<form action="" method="post" @@submit.prevent="submit">
52+
<div class="mb-1">
53+
<form action="/topic/create/comment/{{$data->id}}" method="post">
5454
<div class="row">
55-
<div class="col-md-12">
56-
<div id="topic-comment"></div>
55+
<div class="col-md-12 mb-3">
56+
<x-csrf/>
57+
<input type="hidden" name="topic_id" value="{{$data->id}}">
58+
<textarea name="content" placeholder="说点什么..." class="form-control" data-bs-toggle="autosize" required></textarea>
59+
</div>
60+
<div class="col-12">
61+
<div class="row">
62+
<div class="col">
63+
<button class="btn btn-primary">评论</button>
64+
</div>
65+
<div class="col-auto"><a class="text-muted" href="/topic/create/comment/{{$data->id}}">[高级回复]</a></div>
66+
</div>
5767
</div>
5868
</div>
59-
<button class="btn btn-azure">评论</button>
6069
</form>
6170
</div>
6271
@endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Plugins\Comment\src\Annotation\Topic;
4+
5+
use Attribute;
6+
use Hyperf\Di\Annotation\AbstractAnnotation;
7+
8+
/**
9+
* @Annotation
10+
* @Target({"CLASS"})
11+
*/
12+
#[Attribute(Attribute::TARGET_CLASS)]
13+
class CreateFirstMiddleware extends AbstractAnnotation
14+
{
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Plugins\Comment\src\Annotation\Topic;
4+
5+
use Attribute;
6+
use Hyperf\Di\Annotation\AbstractAnnotation;
7+
8+
/**
9+
* @Annotation
10+
* @Target({"CLASS"})
11+
*/
12+
#[Attribute(Attribute::TARGET_CLASS)]
13+
class CreateLastMiddleware extends AbstractAnnotation
14+
{
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Plugins\Comment\src\Annotation\Topic;
4+
5+
use Attribute;
6+
use Hyperf\Di\Annotation\AbstractAnnotation;
7+
8+
/**
9+
* @Annotation
10+
* @Target({"CLASS"})
11+
*/
12+
#[Attribute(Attribute::TARGET_CLASS)]
13+
class CreateMiddleware extends AbstractAnnotation
14+
{
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Plugins\Comment\src\Annotation\Topic;
4+
5+
use Attribute;
6+
use Hyperf\Di\Annotation\AbstractAnnotation;
7+
8+
/**
9+
* @Annotation
10+
* @Target({"CLASS"})
11+
*/
12+
#[Attribute(Attribute::TARGET_CLASS)]
13+
class UpdateFirstMiddleware extends AbstractAnnotation
14+
{
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Plugins\Comment\src\Annotation\Topic;
4+
5+
use Attribute;
6+
use Hyperf\Di\Annotation\AbstractAnnotation;
7+
8+
/**
9+
* @Annotation
10+
* @Target({"CLASS"})
11+
*/
12+
#[Attribute(Attribute::TARGET_CLASS)]
13+
class UpdateLastMiddleware extends AbstractAnnotation
14+
{
15+
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Plugins\Comment\src\Annotation\Topic;
4+
5+
use Attribute;
6+
use Hyperf\Di\Annotation\AbstractAnnotation;
7+
8+
/**
9+
* @Annotation
10+
* @Target({"CLASS"})
11+
*/
12+
#[Attribute(Attribute::TARGET_CLASS)]
13+
class UpdateMiddleware extends AbstractAnnotation
14+
{
15+
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
11+
namespace App\Plugins\Comment\src\Controller;
12+
13+
use App\Plugins\Comment\src\Handler\CreateTopicComment;
14+
use Hyperf\HttpServer\Annotation\Controller;
15+
use Hyperf\HttpServer\Annotation\GetMapping;
16+
use Hyperf\HttpServer\Annotation\PostMapping;
17+
18+
#[Controller(prefix: '/topic/create/comment')]
19+
class CreateTopicCommentController
20+
{
21+
#[PostMapping(path: '{topic_id}')]
22+
public function store($topic_id)
23+
{
24+
return (new CreateTopicComment())->handler($topic_id);
25+
}
26+
}
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
<?php
22

3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
311
namespace App\Plugins\Comment\src\Controller;
412

513
use App\Plugins\Comment\src\Model\TopicComment;
6-
use App\Plugins\Core\src\Models\Report;
714
use Hyperf\HttpServer\Annotation\Controller;
815
use Hyperf\HttpServer\Annotation\GetMapping;
916

10-
#[Controller(prefix: "/comment")]
17+
#[Controller(prefix: '/comment')]
1118
class IndexController
1219
{
13-
#[GetMapping(path: "topic/{id}/edit")]
20+
#[GetMapping(path: 'topic/{id}/edit')]
1421
public function edit_topic_comment($id)
1522
{
16-
if (!TopicComment::query()->where("id", $id)->exists()) {
17-
return admin_abort("id为:" . $id . "的评论不存在", 404);
23+
if (! TopicComment::query()->where('id', $id)->exists()) {
24+
return admin_abort('id为:' . $id . '的评论不存在', 404);
1825
}
19-
$data = TopicComment::query()->where("id", $id)->first();
26+
$data = TopicComment::query()->where('id', $id)->first();
2027
$quanxian = false;
21-
if (Authority()->check("admin_topic_edit") && curd()->GetUserClass(auth()->data()->class_id)['permission-value'] > curd()->GetUserClass($data->user->class_id)['permission-value']) {
28+
if (Authority()->check('admin_topic_edit') && curd()->GetUserClass(auth()->data()->class_id)['permission-value'] > curd()->GetUserClass($data->user->class_id)['permission-value']) {
2229
$quanxian = true;
2330
}
24-
if (Authority()->check("topic_edit") && auth()->id() === $data->user->id) {
31+
if (Authority()->check('topic_edit') && auth()->id() === $data->user->id) {
2532
$quanxian = true;
2633
}
2734
if ($quanxian === false) {
28-
return admin_abort("无权操作!", 419);
35+
return admin_abort('无权操作!', 419);
2936
}
30-
return view("Comment::topic.edit", ['data' => $data]);
37+
return view('Comment::topic.edit', ['data' => $data]);
3138
}
32-
}
39+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
11+
namespace App\Plugins\Comment\src\Handler;
12+
13+
use App\Plugins\Comment\src\Annotation\Topic\CreateFirstMiddleware;
14+
use App\Plugins\Comment\src\Annotation\Topic\CreateLastMiddleware;
15+
use App\Plugins\Comment\src\Annotation\Topic\CreateMiddleware;
16+
use Hyperf\Di\Annotation\AnnotationCollector;
17+
18+
class CreateTopicComment
19+
{
20+
public function handler($topic_id)
21+
{
22+
$request = request()->all();
23+
$request[$topic_id] = $topic_id;
24+
$handler = function ($request) {
25+
return $request;
26+
};
27+
28+
// 通过中间件
29+
$run = $this->throughMiddleware($handler, $this->middlewares());
30+
return $run($request);
31+
}
32+
33+
/**
34+
* 通过中间件 through the middleware.
35+
* @param $handler
36+
* @param $stack
37+
* @return \Closure|mixed
38+
*/
39+
protected function throughMiddleware($handler, $stack)
40+
{
41+
// 闭包实现中间件功能 closures implement middleware functions
42+
foreach ($stack as $middleware) {
43+
$handler = function ($request) use ($handler, $middleware) {
44+
if ($middleware instanceof \Closure) {
45+
return call_user_func($middleware, $request, $handler);
46+
}
47+
48+
return call_user_func([new $middleware(), 'handler'], $request, $handler);
49+
};
50+
}
51+
return $handler;
52+
}
53+
54+
private function middlewares(): array
55+
{
56+
$middlewares = [];
57+
foreach (AnnotationCollector::getClassesByAnnotation(CreateFirstMiddleware::class) as $key => $value) {
58+
$middlewares[] = $key;
59+
}
60+
foreach (AnnotationCollector::getClassesByAnnotation(CreateMiddleware::class) as $key => $value) {
61+
$middlewares[] = $key;
62+
}
63+
foreach (AnnotationCollector::getClassesByAnnotation(CreateLastMiddleware::class) as $key => $value) {
64+
$middlewares[] = $key;
65+
}
66+
$endMiddlewares = Itf()->get('create-topic-comment-handle-middleware-end');
67+
krsort($endMiddlewares);
68+
foreach ($endMiddlewares as $value) {
69+
$middlewares[] = $value;
70+
}
71+
return array_reverse($middlewares);
72+
}
73+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
11+
namespace App\Plugins\Comment\src\Handler\Middleware\Create\Topic;
12+
13+
use App\Plugins\Comment\src\Model\TopicComment;
14+
use App\Plugins\Core\src\Models\Post;
15+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
16+
use App\Plugins\Topic\src\Models\Topic;
17+
use App\Plugins\User\src\Models\User;
18+
use Hyperf\Di\Annotation\Inject;
19+
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
20+
21+
#[\App\Plugins\Comment\src\Annotation\Topic\CreateMiddleware]
22+
class CreateMiddleware implements MiddlewareInterface
23+
{
24+
/**
25+
* @Inject
26+
*/
27+
protected ValidatorFactoryInterface $validationFactory;
28+
29+
public function handler($data, \Closure $next)
30+
{
31+
$validator = $this->validationFactory->make(
32+
$data,
33+
[
34+
'topic_id' => 'required|exists:topic,id',
35+
'content' => 'required|string|min:' . get_options('comment_create_min', 1) . '|max:' . get_options('comment_create_max', 200),
36+
],
37+
[],
38+
[
39+
'topic_id' => '帖子ID',
40+
'content' => '评论内容',
41+
]
42+
);
43+
44+
if ($validator->fails()) {
45+
// Handle exception
46+
cache()->delete('comment_create_time_' . auth()->id());
47+
return redirect()->url(request()->getHeader('referer')[0] . '?' . http_build_query($data))->with('danger', $validator->errors()->first())->go();
48+
}
49+
$data = $this->create($data);
50+
return $next($data);
51+
}
52+
53+
public function create($data)
54+
{
55+
// 原内容
56+
$_content = $data['content'];
57+
58+
// 过滤xss
59+
$content = xss()->clean($data['content']);
60+
61+
// 解析艾特
62+
$content = replace_all_at($content);
63+
64+
$post = Post::query()->create([
65+
'content' => $content,
66+
'user_agent' => get_user_agent(),
67+
'user_ip' => get_client_ip(),
68+
'user_id' => auth()->id(),
69+
]);
70+
$comment = TopicComment::query()->create([
71+
'topic_id' => $data['topic_id'],
72+
'post_id' => $post->id,
73+
'user_id' => auth()->id(),
74+
]);
75+
// 给posts表设置comment_id字段的值
76+
$posts = Post::query()->where('id', $post->id)->update(['comment_id' => $comment->id]);
77+
78+
// 艾特被回复的人
79+
$this->at_user($comment, $_content);
80+
81+
//发布成功
82+
// 发送通知
83+
$topic_data = Topic::query()->find($data['topic_id']);
84+
if ($topic_data->user_id != auth()->id()) {
85+
$title = auth()->data()->username . '评论了你发布的帖子!';
86+
$content = view('Comment::Notice.comment', ['comment' => $content, 'user_data' => auth()->data(), 'data' => $comment]);
87+
$action = '/' . $topic_data->id . '.html';
88+
user_notice()->send($topic_data->user_id, $title, $content, $action);
89+
}
90+
91+
$data['comment'] = $comment;
92+
$data['posts'] = $posts;
93+
return $data;
94+
}
95+
96+
private function at_user(\Hyperf\Database\Model\Model | \Hyperf\Database\Model\Builder $data, string $html): void
97+
{
98+
$at_user = get_all_at($html);
99+
foreach ($at_user as $username) {
100+
go(function () use ($username, $data) {
101+
if (User::query()->where('username', $username)->exists()) {
102+
$user = User::query()->where('username', $username)->first();
103+
if ((int) $user->id !== (int) $data->user_id) {
104+
user_notice()->send(
105+
$user->id,
106+
'有人在评论中提到了你',
107+
'有人在评论中提到了你',
108+
'/' . $data->topic_id . '.html/' . $data->id . '?page=' . get_topic_comment_page($data->id)
109+
);
110+
}
111+
}
112+
});
113+
}
114+
}
115+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
11+
namespace App\Plugins\Comment\src\Handler\Middleware\Create\Topic;
12+
13+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
14+
15+
class EndMiddleware implements MiddlewareInterface
16+
{
17+
public function handler($data, \Closure $next)
18+
{
19+
return redirect()->back()->with('success', '发表成功!')->go();
20+
}
21+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
11+
namespace App\Plugins\Comment\src\Handler\Middleware\Create\Topic;
12+
13+
use App\Plugins\Comment\src\Annotation\Topic\CreateFirstMiddleware;
14+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
15+
16+
#[CreateFirstMiddleware]
17+
class FirstMiddleware implements MiddlewareInterface
18+
{
19+
public function handler($data, \Closure $next)
20+
{
21+
if (! auth()->check()) {
22+
return redirect()->back()->with('danger', '未登陆')->go();
23+
}
24+
if (! Authority()->check('comment_create')) {
25+
return redirect()->back()->with('danger', '无评论权限')->go();
26+
}
27+
if (cache()->has('comment_create_time_' . auth()->id())) {
28+
$time = cache()->get('comment_create_time_' . auth()->id()) - time();
29+
return redirect()->back()->with('danger', '发表评论过于频繁,请 ' . $time . ' 秒后再试')->go();
30+
}
31+
cache()->set('comment_create_time_' . auth()->id(), time() + get_options('comment_create_time', 60), get_options('comment_create_time', 60));
32+
return $next($data);
33+
}
34+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
11+
namespace App\Plugins\Comment\src\Handler\Middleware\Create\Topic;
12+
13+
use App\Plugins\Comment\src\Annotation\Topic\CreateLastMiddleware;
14+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
15+
16+
#[CreateLastMiddleware]
17+
class LastMiddleware implements MiddlewareInterface
18+
{
19+
public function handler($data, \Closure $next)
20+
{
21+
return $next($data);
22+
}
23+
}

‎app/Plugins/Core/src/Lib/Redirect.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
<?php
22

3-
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
411
namespace App\Plugins\Core\src\Lib;
512

6-
713
class Redirect
814
{
915
private string $url;
1016

1117
public function url($url): Redirect
1218
{
13-
$this->url = $url;
14-
return $this;
19+
$this->url = $url;
20+
return $this;
1521
}
1622

1723
public function back(): Redirect
1824
{
19-
$this->url = session()->get("_previous")['url'];
25+
$this->url = request()->getHeader('referer')[0];
2026
return $this;
2127
}
2228

@@ -30,4 +36,4 @@ public function go(): \Psr\Http\Message\ResponseInterface
3036
{
3137
return response()->redirect($this->url);
3238
}
33-
}
39+
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
<?php
22

3-
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of zhuchunshu.
6+
* @link https://github.com/zhuchunshu
7+
* @document https://github.com/zhuchunshu/super-forum
8+
* @contact laravel@88.com
9+
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
10+
*/
411
namespace App\Plugins\Topic\src\Handler\Topic;
512

6-
713
class CreateTopicView
814
{
9-
1015
public function handler(): \Psr\Http\Message\ResponseInterface
1116
{
12-
return view("Topic::create",['right' => $this->right()]);
13-
}
14-
15-
// 右侧侧栏
16-
public function right(): array
17-
{
18-
//Itf()->add("Topic_create_right",2,"Topic::create.right-summary");
19-
return Itf()->get("Topic_create_right");
17+
return view('Topic::create');
2018
}
21-
}
19+
}

0 commit comments

Comments
 (0)
Please sign in to comment.