Skip to content

Commit

Permalink
更新:优化发帖,完成高级评论
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuchunshu committed Dec 25, 2022
1 parent 24c4cf4 commit 1fc866a
Show file tree
Hide file tree
Showing 15 changed files with 634 additions and 26 deletions.
136 changes: 136 additions & 0 deletions app/Plugins/Comment/bootstrap.php
@@ -1,3 +1,139 @@
<?php

Itf()->add('create-topic-comment-handle-middleware-end', 0, \App\Plugins\Comment\src\Handler\Middleware\Create\Topic\EndMiddleware::class);

Itf()->add('comment-topic-create-editor-external_plugins', 0, [
'sfPreview' => file_hash('plugins/Topic/js/editor/plugins/sfPreview.js'),
]);
Itf()->add('comment-topic-edit-editor-external_plugins', 0, [
'sfPreview' => file_hash('plugins/Topic/js/editor/plugins/sfPreview.js'),
]);
Itf()->add('comment-topic-create-editor-plugins', 0, ['importcss', 'searchreplace', 'autolink', 'autosave', 'directionality', 'code', 'visualblocks', 'visualchars', 'image', 'link', 'media', 'codesample', 'table', 'charmap', 'pagebreak', 'nonbreaking', 'advlist', 'lists', 'wordcount', 'charmap', 'quickbars']);
Itf()->add('comment-topic-edit-editor-plugins', 0, ['importcss', 'searchreplace', 'autolink', 'autosave', 'directionality', 'code', 'visualblocks', 'visualchars', 'image', 'link', 'media', 'codesample', 'table', 'charmap', 'pagebreak', 'nonbreaking', 'advlist', 'lists', 'wordcount', 'charmap', 'quickbars']);

Itf()->add('comment-topic-create-editor-toolbar', 0, ['undo', 'redo', '|', 'blocks', '|', 'bold', 'italic', 'underline', 'strikethrough', '|', 'alignleft', 'aligncenter', 'alignright', 'alignjustify', 'outdent', 'indent', 'numlist', 'bullist', '|', 'forecolor', 'backcolor', 'removeformat', 'insertfile', 'image', 'media', 'link', 'sfPreview', 'restoredraft', 'codesample', '|', 'ltr', 'rtl']);
Itf()->add('comment-topic-edit-editor-toolbar', 0, ['undo', 'redo', '|', 'blocks', '|', 'bold', 'italic', 'underline', 'strikethrough', '|', 'alignleft', 'aligncenter', 'alignright', 'alignjustify', 'outdent', 'indent', 'numlist', 'bullist', '|', 'forecolor', 'backcolor', 'removeformat', 'insertfile', 'image', 'media', 'link', 'sfPreview', 'restoredraft', 'codesample', '|', 'ltr', 'rtl']);


$editor_menu =[
'file' => [
'title' => 'File',
'items' => [
'newdocument',
'restoredraft',
'|',
'sfPreview',
'export',
'|',
'deleteallconversations',
],
],
'edit' => [
'title' => 'Edit',
'items' => [
'undo',
'redo',
'|',
'cut',
'copy',
'paste',
'pastetext',
'|',
'selectall',
'|',
'searchreplace',
],
],
'view' => [
'title' => 'View',
'items' => [
'code',
'|',
'visualaid',
'visualchars',
'visualblocks',
'|',
'spellchecker',
'|',
'preview',
'|',
'showcomments',
],
],
'insert' => [
'title' => 'Insert',
'items' => [
'image',
'link',
'media',
'addcomment',
'pageembed',
'template',
'codesample',
'inserttable',
'|',
'charmap',
'emoticons',
'hr',
'|',
'pagebreak',
'nonbreaking',
'anchor',
'tableofcontents',
],
],
'format' => [
'title' => 'Format',
'items' => [
'bold',
'italic',
'underline',
'strikethrough',
'superscript',
'subscript',
'codeformat',
'|',
'styles',
'blocks',
'fontfamily',
'fontsize',
'align',
'lineheight',
'|',
'forecolor',
'backcolor',
'|',
'language',
'|',
'removeformat',
],
],
'tools' => [
'title' => 'Tools',
'items' => [
'spellchecker',
'spellcheckerlanguage',
'|',
'a11ycheck',
'code',
'wordcount',
],
],
'table' => [
'title' => 'Table',
'items' => [
'inserttable',
'|',
'cell',
'row',
'column',
'|',
'advtablesort',
'|',
'tableprops',
'deletetable',
],
],
];
Itf()->add('comment-topic-create-editor-menu', 0, $editor_menu);
Itf()->add('comment-topic-edit-editor-menu', 0, $editor_menu);
2 changes: 1 addition & 1 deletion app/Plugins/Comment/resources/views/Widget/topic.blade.php
Expand Up @@ -55,7 +55,7 @@
<div class="col-md-12 mb-3">
<x-csrf/>
<input type="hidden" name="topic_id" value="{{$data->id}}">
<textarea name="content" placeholder="说点什么..." class="form-control" data-bs-toggle="autosize" required></textarea>
<textarea name="content" placeholder="说点什么..." class="form-control" data-bs-toggle="autosize" required>{{request()->input('content')}}</textarea>
</div>
<div class="col-12">
<div class="row">
Expand Down
125 changes: 125 additions & 0 deletions app/Plugins/Comment/resources/views/topic/create.blade.php
@@ -0,0 +1,125 @@
@extends('App::app')
@section('title','评论帖子【'.\Hyperf\Utils\Str::limit($topic->title,25).'】')
@section('content')
<div class="row row-cards justify-content-center">
<div class="col-lg-12">
<ol class="breadcrumb breadcrumb-arrows" aria-label="breadcrumbs">
<li class="breadcrumb-item"><a href="/">首页</a></li>
<li class="breadcrumb-item"><a href="/tags/{{$topic->tag->id}}.html">
{!! $topic->tag->icon !!}
{{$topic->tag->name}}
</a>
</li>
<li class="breadcrumb-item"><a href="/{{$topic->id}}.html">
{{\Hyperf\Utils\Str::limit($topic->title,25)}}
</a>
</li>
<li class="breadcrumb-item active" aria-current="page"><a href="#">回帖</a></li>
</ol>
</div>
<div class="col-lg-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">回帖</h3>
</div>
<div class="card-body">
<form action="/topic/create/comment/{{$topic->id}}" method="post">
<x-csrf/>
<input type="hidden" name="no_content" value="1">
<input type="hidden" name="topic_id" value="{{$topic->id}}">
<div class="mb-3">
<label for="" class="form-label"></label>
<textarea name="content" id="content" rows="3"></textarea>
</div>
<div class="mb-3 d-flex flex-row-reverse">
<button class="btn btn-primary" type="submit">回帖</button>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection


@section('scripts')
<script src="{{file_hash("js/axios.min.js")}}"></script>
<script src="{{file_hash('tabler/libs/tinymce/tinymce.min.js')}}"></script>
<script defer>
const target = document.getElementsByTagName("html")[0]
const body_className = document.getElementsByTagName("html")[0].getAttribute("data-theme");
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (body_className !== document.getElementsByTagName("html")[0].getAttribute("data-theme")) {
location.reload()
}
});
});

observer.observe(target, {attributes: true});

const image_upload_handler = (blobInfo, progress) => new Promise((resolve, reject) => {
const formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
formData.append('_token', csrf_token);
axios.post("/user/upload/image",formData,{
'Content-type' : 'multipart/form-data'
}).then(function(r){
console.log(r)
const data = r.data;
if(data.success){
resolve(data.result.url);
return ;
}
reject({message:'HTTP Error: ' + data.result.msg + ', Error Code: '+data.code,remove: true});
}).catch(function(e){
console.log(e)
})

});

document.addEventListener("DOMContentLoaded", function () {
let options = {
selector: '#content',
height: 450,
menu:{!! \App\Plugins\Comment\src\Lib\Edit\Editor::menu() !!},
menubar:"{!! \App\Plugins\Comment\src\Lib\Edit\Editor::menubar() !!}",
statusbar: true,
elementpath: true,
promotion: false,
plugins: {!! \App\Plugins\Comment\src\Lib\Edit\Editor::plugins() !!},
language: "zh-Hans",
toolbar: "{!! \App\Plugins\Comment\src\Lib\Edit\Editor::toolbar() !!}",
link_default_target: '_blank',
toolbar_mode: 'sliding',
image_advtab: true,
automatic_uploads: true,
convert_urls:false,
external_plugins:{!! \App\Plugins\Comment\src\Lib\Edit\Editor::externalPlugins() !!},
images_upload_handler: image_upload_handler,
init_instance_callback: (editor) => {
@if(request()->has('restoredraft'))
editor.plugins.autosave.restoreDraft()
@endif
},
mobile:{
menu:{!! \App\Plugins\Comment\src\Lib\Edit\Editor::menu() !!},
menubar:"{!! \App\Plugins\Comment\src\Lib\Edit\Editor::menubar() !!}",
toolbar_mode: 'scrolling'
},
autosave_ask_before_unload: true,
autosave_interval: '1s',
autosave_prefix: '{{config('codefec.app.name')}}-comment-{path}{query}-{id}-',
autosave_restore_when_empty: false,
autosave_retention: '1400m',
branding:false,
content_style: 'body { font-family: -apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif; font-size: 14px; -webkit-font-smoothing: antialiased; }'
}
if (document.body.className === 'theme-dark') {
options.skin = 'oxide-dark';
options.content_css = 'dark';
}
tinyMCE.init(options);
});
</script>
@endsection
Expand Up @@ -11,13 +11,33 @@
namespace App\Plugins\Comment\src\Controller;

use App\Plugins\Comment\src\Handler\CreateTopicComment;
use App\Plugins\Topic\src\Models\Topic;
use App\Plugins\User\src\Middleware\LoginMiddleware;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;

#[Controller(prefix: '/topic/create/comment')]
#[Middleware(LoginMiddleware::class)]
class CreateTopicCommentController
{
#[GetMapping(path: '{topic_id}')]
public function index($topic_id)
{
if (! Authority()->check('comment_create')) {
return redirect()->back()->with('danger', '无评论权限')->go();
}
if (! Topic::query()->where(['id' => $topic_id, 'status' => 'publish'])->exists()) {
return redirect()->back()->with('danger', '帖子不存在')->go();
}
$topic = Topic::query()->find($topic_id);
if (@$topic->post->options->disable_comment) {
return redirect()->back()->with('danger', '此帖子关闭了评论功能')->go();
}
return view('Comment::topic.create', ['topic' => $topic]);
}

#[PostMapping(path: '{topic_id}')]
public function store($topic_id)
{
Expand Down
Expand Up @@ -44,7 +44,17 @@ public function handler($data, \Closure $next)
if ($validator->fails()) {
// Handle exception
cache()->delete('comment_create_time_' . auth()->id());
return redirect()->url(request()->getHeader('referer')[0] . '?' . http_build_query($data))->with('danger', $validator->errors()->first())->go();
$backUrl = pathinfo(request()->getHeader('referer')[0])['dirname'] . '/' . $data['topic_id'];
$result = [
'content' => $data['no_content'] ?: $data['content'],
'restoredraft' => true,
];
return redirect()->url($backUrl . '?' . http_build_query($result))->with('danger', $validator->errors()->first())->go();
}
$topic = Topic::query()->find($data['topic_id']);
if (@$topic->post->options->disable_comment) {
cache()->delete('comment_create_time_' . auth()->id());
return redirect()->back()->with('danger', '此帖子关闭了评论功能')->go();
}
$data = $this->create($data);
return $next($data);
Expand Down
Expand Up @@ -16,6 +16,6 @@ class EndMiddleware implements MiddlewareInterface
{
public function handler($data, \Closure $next)
{
return redirect()->back()->with('success', '发表成功!')->go();
return redirect()->url('/' . $data['topic_id'] . '.html/' . $data['comment']['id'] . '?page=' . get_topic_comment_page($data['comment']['id']))->with('success', '发表成功!')->go();
}
}
Expand Up @@ -24,9 +24,14 @@ public function handler($data, \Closure $next)
if (! Authority()->check('comment_create')) {
return redirect()->back()->with('danger', '无评论权限')->go();
}
$backUrl = pathinfo(request()->getHeader('referer')[0])['dirname'] . '/' . $data['topic_id'];
$result = [
'content' => @$data['no_content']?:$data['content'],
'restoredraft' => true,
];
if (cache()->has('comment_create_time_' . auth()->id())) {
$time = cache()->get('comment_create_time_' . auth()->id()) - time();
return redirect()->back()->with('danger', '发表评论过于频繁,请 ' . $time . ' 秒后再试')->go();
return redirect()->url($backUrl . '?' . http_build_query($result))->with('danger', '发表评论过于频繁,请 ' . $time . ' 秒后再试')->go();
}
cache()->set('comment_create_time_' . auth()->id(), time() + get_options('comment_create_time', 60), get_options('comment_create_time', 60));
return $next($data);
Expand Down

0 comments on commit 1fc866a

Please sign in to comment.