Skip to content

Commit

Permalink
更新:重构编辑帖子逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Dec 23, 2022
1 parent 7b3f388 commit c8ea0ed
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/Plugins/Topic/bootstrap.php
Expand Up @@ -241,7 +241,7 @@
]);

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

Itf()->add('topic-create-editor-external_plugins', 0, [
'sfPreview' => file_hash('plugins/Topic/js/editor/plugins/sfPreview.js'),
Expand Down
1 change: 1 addition & 0 deletions app/Plugins/Topic/resources/views/edit/basis.blade.php
Expand Up @@ -3,6 +3,7 @@
<input type="text" class="form-control" value="{{$data->title}}" name="basis[title]" placeholder="title" required>
</div>

<input type="hidden" name="basis[topic_id]" value="{{$data->id}}">
<div class="mb-3">
<label class="form-label">选择标签</label>
<select type="text" name="basis[tag]" class="form-select" id="select-topic-tags" required>
Expand Down
16 changes: 16 additions & 0 deletions app/Plugins/Topic/src/Annotation/Topic/UpdateFirstMiddleware.php
@@ -0,0 +1,16 @@
<?php

namespace App\Plugins\Topic\src\Annotation\Topic;

use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation;

/**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
class UpdateFirstMiddleware extends AbstractAnnotation
{

}
16 changes: 16 additions & 0 deletions app/Plugins/Topic/src/Annotation/Topic/UpdateLastMiddleware.php
@@ -0,0 +1,16 @@
<?php

namespace App\Plugins\Topic\src\Annotation\Topic;

use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation;

/**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
class UpdateLastMiddleware extends AbstractAnnotation
{

}
16 changes: 16 additions & 0 deletions app/Plugins/Topic/src/Annotation/Topic/UpdateMiddleware.php
@@ -0,0 +1,16 @@
<?php

namespace App\Plugins\Topic\src\Annotation\Topic;

use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation;

/**
* @Annotation
* @Target({"CLASS"})
*/
#[Attribute(Attribute::TARGET_CLASS)]
class UpdateMiddleware extends AbstractAnnotation
{

}
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/super-forum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update;

use App\Plugins\Core\src\Models\PostsOption;
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;

#[\App\Plugins\Topic\src\Annotation\Topic\UpdateLastMiddleware]
class PostsOptionsMiddleware implements MiddlewareInterface
{
public function handler($data, \Closure $next)
{
$post_id = $data['post_id'];
if (! PostsOption::query()->where('post_id', $post_id)->exists()) {
$post_options = PostsOption::create([
'post_id' => $post_id,
]);

} else {
$post_options = PostsOption::query()->where('post_id', $post_id)->first();
}
$data['posts_options'] = $post_options;
return $next($data);
}
}
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/super-forum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update;

use App\Plugins\Core\src\Models\PostsOption;
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;

#[\App\Plugins\Topic\src\Annotation\Topic\UpdateLastMiddleware]
class SetDisableCommentMiddleware implements MiddlewareInterface
{
public function handler($data, \Closure $next)
{
$disable_comment = (bool) request()->input('options.disable_comment', false);
$posts_options_id = $data['posts_options']['id'];
PostsOption::query()->where('id', $posts_options_id)->update([
'disable_comment' => $disable_comment,
]);
return $next($data);
}
}
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/super-forum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update;

use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;

class UpdateEndMiddleware implements MiddlewareInterface
{
public function handler($data, \Closure $next)
{
return redirect()->url('/' . $data['topic_id'] . '.html')->with('success', '更新成功!')->go();
}
}
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/super-forum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update;

use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
use App\Plugins\Topic\src\Models\TopicTag;
use App\Plugins\User\src\Models\UserClass;

#[\App\Plugins\Topic\src\Annotation\Topic\UpdateFirstMiddleware]
class UpdateFirstMiddleware implements MiddlewareInterface
{
public function handler($data, \Closure $next)
{
$class_name = UserClass::query()->where('id', auth()->data()->class_id)->first()->name;
$tag_value = TopicTag::query()->where('id', $data['basis']['tag'])->first();
if (! user_TopicTagQuanxianCheck($tag_value, $class_name)) {
return redirect()->with('danger', '无权使用此标签')->url('topic/create')->go();
}
return $next($data);
}
}
@@ -0,0 +1,143 @@
<?php

declare(strict_types=1);
/**
* This file is part of zhuchunshu.
* @link https://github.com/zhuchunshu
* @document https://github.com/zhuchunshu/super-forum
* @contact laravel@88.com
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE
*/
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update;

use App\Plugins\Core\src\Models\Post;
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface;
use App\Plugins\Topic\src\Models\Topic;
use App\Plugins\Topic\src\Models\TopicKeyword;
use App\Plugins\Topic\src\Models\TopicKeywordsWith;
use App\Plugins\Topic\src\Models\TopicUpdated;
use App\Plugins\User\src\Models\User;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

#[\App\Plugins\Topic\src\Annotation\Topic\UpdateMiddleware]
class UpdateMiddleware implements MiddlewareInterface
{
/**
* @Inject
*/
protected ValidatorFactoryInterface $validationFactory;

public function handler($data, \Closure $next)
{
$validator = $this->validationFactory->make(
$data['basis'],
[
'topic_id' => 'required|exists:topic,id',
'content' => 'required|string|min:' . get_options('topic_create_content_min', 10),
'title' => 'required|string|min:' . get_options('topic_create_title_min', 1) . '|max:' . get_options('topic_create_title_max', 200),
'tag' => 'required|exists:topic_tag,id',
],
[
'topic_id' => '帖子id',
'content' => '正文内容',
'title' => '标题',
'tag' => '标签id',
]
);

if ($validator->fails()) {
return redirect()->with('danger', $validator->errors()->first())->url('topic/' . $data['basis']['topic_id'] . '/edit')->go();
}
$data['topic_id'] = $data['basis']['topic_id'];
$data = $this->update($data);
return $next($data);
}

public function update($data)
{
// 帖子id
$topic_id = $data['topic_id'];
// 帖子标题
$title = $data['basis']['title'];
// 帖子标签
$tag = $data['basis']['tag'];
// 帖子html内容
$content = $data['basis']['content'];
$content = xss()->clean($content);

// 解析标签
$_content = $content;
$content = $this->tag($content);
// 解析艾特
$content = $this->at($content);

$post_id = Topic::query()->find($topic_id)->post_id;
Post::query()->where('id', $post_id)->update([
'content' => $content,
]);
Topic::query()->where('id', $topic_id)->update([
'title' => $title,
'tag_id' => $tag,
]);
$topic = Topic::query()->where('id', $topic_id)->first();
TopicUpdated::create([
'topic_id' => $topic_id,
'user_id' => auth()->id(),
'user_ip' => get_client_ip(),
'user_agent' => get_user_agent(),
]);
$this->topic_keywords($topic, $_content);
$topic_data = Topic::query()->find($topic_id);
$this->at_user($topic_data, $_content);
cache()->delete('topic.data.' . $topic_id);
$data['topic_id'] = $topic_id;
$data['post_id'] = $post_id;
return $data;
}

public function tag(string $html)
{
return replace_all_keywords($html);
}

public function at(string $html): string
{
return replace_all_at($html);
}

public function topic_keywords($data, string $html): void
{
foreach (get_all_keywords($html) as $tag) {
if (! TopicKeyword::query()->where('name', $tag)->exists()) {
TopicKeyword::query()->create([
'name' => $tag,
'user_id' => auth()->id(),
]);
}
$tk = TopicKeyword::query()->where('name', $tag)->first();
if (! TopicKeywordsWith::query()->where(['topic_id' => $data->id, 'with_id' => $tk->id])->exists()) {
TopicKeywordsWith::query()->create([
'topic_id' => $data->id,
'with_id' => $tk->id,
'user_id' => auth()->id(),
]);
}
}
}

private function at_user(\Hyperf\Database\Model\Model | \Hyperf\Database\Model\Builder $data, string $html): void
{
$at_user = get_all_at($html);
foreach ($at_user as $value) {
go(function () use ($value, $data) {
if (User::query()->where('username', $value)->exists()) {
$user = User::query()->where('username', $value)->first();
if ($user->id != $data->user_id) {
user_notice()->send($user->id, '有人在帖子中提到了你', $user->username . '在帖子<b>' . $data->title . '</b>中提到了你', '/' . $data->id . '.html');
}
}
});
}
}
}

0 comments on commit c8ea0ed

Please sign in to comment.