Skip to content

Commit c8ea0ed

Browse files
committedDec 23, 2022
更新:重构编辑帖子逻辑
·
v2.7.7v2.1.7
1 parent 7b3f388 commit c8ea0ed

File tree

10 files changed

+304
-1
lines changed

10 files changed

+304
-1
lines changed
 

‎app/Plugins/Topic/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241
]);
242242

243243
Itf()->add('topic-create-handle-middleware-end', 0, \App\Plugins\Topic\src\Handler\Topic\Middleware\Create\CreateEndMiddleware::class);
244-
Itf()->add('topic-update-handle-middleware-end', 0, \App\Plugins\Topic\src\Handler\Topic\Middleware\Update\UpdateEndMiddleware::class);
244+
Itf()->add('topic-edit-handle-middleware-end', 0, \App\Plugins\Topic\src\Handler\Topic\Middleware\Update\UpdateEndMiddleware::class);
245245

246246
Itf()->add('topic-create-editor-external_plugins', 0, [
247247
'sfPreview' => file_hash('plugins/Topic/js/editor/plugins/sfPreview.js'),

‎app/Plugins/Topic/resources/views/edit/basis.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<input type="text" class="form-control" value="{{$data->title}}" name="basis[title]" placeholder="title" required>
44
</div>
55

6+
<input type="hidden" name="basis[topic_id]" value="{{$data->id}}">
67
<div class="mb-3">
78
<label class="form-label">选择标签</label>
89
<select type="text" name="basis[tag]" class="form-select" id="select-topic-tags" required>
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\Topic\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\Topic\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\Topic\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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Topic\src\Handler\Topic\Middleware\Update;
12+
13+
use App\Plugins\Core\src\Models\PostsOption;
14+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
15+
16+
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateLastMiddleware]
17+
class PostsOptionsMiddleware implements MiddlewareInterface
18+
{
19+
public function handler($data, \Closure $next)
20+
{
21+
$post_id = $data['post_id'];
22+
if (! PostsOption::query()->where('post_id', $post_id)->exists()) {
23+
$post_options = PostsOption::create([
24+
'post_id' => $post_id,
25+
]);
26+
27+
} else {
28+
$post_options = PostsOption::query()->where('post_id', $post_id)->first();
29+
}
30+
$data['posts_options'] = $post_options;
31+
return $next($data);
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Topic\src\Handler\Topic\Middleware\Update;
12+
13+
use App\Plugins\Core\src\Models\PostsOption;
14+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
15+
16+
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateLastMiddleware]
17+
class SetDisableCommentMiddleware implements MiddlewareInterface
18+
{
19+
public function handler($data, \Closure $next)
20+
{
21+
$disable_comment = (bool) request()->input('options.disable_comment', false);
22+
$posts_options_id = $data['posts_options']['id'];
23+
PostsOption::query()->where('id', $posts_options_id)->update([
24+
'disable_comment' => $disable_comment,
25+
]);
26+
return $next($data);
27+
}
28+
}
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\Topic\src\Handler\Topic\Middleware\Update;
12+
13+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
14+
15+
class UpdateEndMiddleware implements MiddlewareInterface
16+
{
17+
public function handler($data, \Closure $next)
18+
{
19+
return redirect()->url('/' . $data['topic_id'] . '.html')->with('success', '更新成功!')->go();
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Topic\src\Handler\Topic\Middleware\Update;
12+
13+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
14+
use App\Plugins\Topic\src\Models\TopicTag;
15+
use App\Plugins\User\src\Models\UserClass;
16+
17+
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateFirstMiddleware]
18+
class UpdateFirstMiddleware implements MiddlewareInterface
19+
{
20+
public function handler($data, \Closure $next)
21+
{
22+
$class_name = UserClass::query()->where('id', auth()->data()->class_id)->first()->name;
23+
$tag_value = TopicTag::query()->where('id', $data['basis']['tag'])->first();
24+
if (! user_TopicTagQuanxianCheck($tag_value, $class_name)) {
25+
return redirect()->with('danger', '无权使用此标签')->url('topic/create')->go();
26+
}
27+
return $next($data);
28+
}
29+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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\Topic\src\Handler\Topic\Middleware\Update;
12+
13+
use App\Plugins\Core\src\Models\Post;
14+
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
15+
use App\Plugins\Topic\src\Models\Topic;
16+
use App\Plugins\Topic\src\Models\TopicKeyword;
17+
use App\Plugins\Topic\src\Models\TopicKeywordsWith;
18+
use App\Plugins\Topic\src\Models\TopicUpdated;
19+
use App\Plugins\User\src\Models\User;
20+
use Hyperf\Di\Annotation\Inject;
21+
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
22+
23+
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateMiddleware]
24+
class UpdateMiddleware implements MiddlewareInterface
25+
{
26+
/**
27+
* @Inject
28+
*/
29+
protected ValidatorFactoryInterface $validationFactory;
30+
31+
public function handler($data, \Closure $next)
32+
{
33+
$validator = $this->validationFactory->make(
34+
$data['basis'],
35+
[
36+
'topic_id' => 'required|exists:topic,id',
37+
'content' => 'required|string|min:' . get_options('topic_create_content_min', 10),
38+
'title' => 'required|string|min:' . get_options('topic_create_title_min', 1) . '|max:' . get_options('topic_create_title_max', 200),
39+
'tag' => 'required|exists:topic_tag,id',
40+
],
41+
[
42+
'topic_id' => '帖子id',
43+
'content' => '正文内容',
44+
'title' => '标题',
45+
'tag' => '标签id',
46+
]
47+
);
48+
49+
if ($validator->fails()) {
50+
return redirect()->with('danger', $validator->errors()->first())->url('topic/' . $data['basis']['topic_id'] . '/edit')->go();
51+
}
52+
$data['topic_id'] = $data['basis']['topic_id'];
53+
$data = $this->update($data);
54+
return $next($data);
55+
}
56+
57+
public function update($data)
58+
{
59+
// 帖子id
60+
$topic_id = $data['topic_id'];
61+
// 帖子标题
62+
$title = $data['basis']['title'];
63+
// 帖子标签
64+
$tag = $data['basis']['tag'];
65+
// 帖子html内容
66+
$content = $data['basis']['content'];
67+
$content = xss()->clean($content);
68+
69+
// 解析标签
70+
$_content = $content;
71+
$content = $this->tag($content);
72+
// 解析艾特
73+
$content = $this->at($content);
74+
75+
$post_id = Topic::query()->find($topic_id)->post_id;
76+
Post::query()->where('id', $post_id)->update([
77+
'content' => $content,
78+
]);
79+
Topic::query()->where('id', $topic_id)->update([
80+
'title' => $title,
81+
'tag_id' => $tag,
82+
]);
83+
$topic = Topic::query()->where('id', $topic_id)->first();
84+
TopicUpdated::create([
85+
'topic_id' => $topic_id,
86+
'user_id' => auth()->id(),
87+
'user_ip' => get_client_ip(),
88+
'user_agent' => get_user_agent(),
89+
]);
90+
$this->topic_keywords($topic, $_content);
91+
$topic_data = Topic::query()->find($topic_id);
92+
$this->at_user($topic_data, $_content);
93+
cache()->delete('topic.data.' . $topic_id);
94+
$data['topic_id'] = $topic_id;
95+
$data['post_id'] = $post_id;
96+
return $data;
97+
}
98+
99+
public function tag(string $html)
100+
{
101+
return replace_all_keywords($html);
102+
}
103+
104+
public function at(string $html): string
105+
{
106+
return replace_all_at($html);
107+
}
108+
109+
public function topic_keywords($data, string $html): void
110+
{
111+
foreach (get_all_keywords($html) as $tag) {
112+
if (! TopicKeyword::query()->where('name', $tag)->exists()) {
113+
TopicKeyword::query()->create([
114+
'name' => $tag,
115+
'user_id' => auth()->id(),
116+
]);
117+
}
118+
$tk = TopicKeyword::query()->where('name', $tag)->first();
119+
if (! TopicKeywordsWith::query()->where(['topic_id' => $data->id, 'with_id' => $tk->id])->exists()) {
120+
TopicKeywordsWith::query()->create([
121+
'topic_id' => $data->id,
122+
'with_id' => $tk->id,
123+
'user_id' => auth()->id(),
124+
]);
125+
}
126+
}
127+
}
128+
129+
private function at_user(\Hyperf\Database\Model\Model | \Hyperf\Database\Model\Builder $data, string $html): void
130+
{
131+
$at_user = get_all_at($html);
132+
foreach ($at_user as $value) {
133+
go(function () use ($value, $data) {
134+
if (User::query()->where('username', $value)->exists()) {
135+
$user = User::query()->where('username', $value)->first();
136+
if ($user->id != $data->user_id) {
137+
user_notice()->send($user->id, '有人在帖子中提到了你', $user->username . '在帖子<b>' . $data->title . '</b>中提到了你', '/' . $data->id . '.html');
138+
}
139+
}
140+
});
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)
Please sign in to comment.