refactor: 文章详情留言

This commit is contained in:
2025-04-12 17:03:45 +08:00
parent 7ada83016d
commit 752f2a476f
3 changed files with 70 additions and 3 deletions

View File

@@ -3,12 +3,16 @@ declare (strict_types = 1);
namespace app\index\controller; namespace app\index\controller;
use app\admin\controller\v1\BannerItem;
use app\index\model\ArticleCategoryModel; use app\index\model\ArticleCategoryModel;
use app\index\model\ArticleLeaveMessageModel;
use app\index\model\ArticleModel; use app\index\model\ArticleModel;
use app\index\model\SysBannerItemModel; use app\index\model\SysBannerItemModel;
use think\facade\Validate;
use think\facade\View; use think\facade\View;
/**
* 文章控制器
*/
class Article extends Common class Article extends Common
{ {
/** /**
@@ -100,4 +104,43 @@ class Article extends Common
return View::fetch('detail'); return View::fetch('detail');
} }
/**
* 留言
*/
public function comment()
{
$id = request()->param('id');
$post = request()->post([
'name',
'email',
'content'
]);
// 验证字段
$validate = Validate::rule([
'name' => 'max:64',
'email' => 'email'
])
->message([
'name.max' => '姓名不能超过64个字符',
'email' => '请输入正确的邮箱'
]);
if (!$validate->check($post)) {
return error($validate->getError());
}
// 保存留言
$data = array_merge($post, [
'article_id' => $id,
'ip' => request()->ip(),
'user_agent' => request()->header('user-agent')
]);
$ret = ArticleLeaveMessageModel::create($data);
if ($ret->isEmpty()) {
return error('留言提交失败');
}
return success('留言提交成功');
}
} }

View File

@@ -30,6 +30,8 @@ Route::group('article', function() {
Route::get('detail/:id', 'Article/detail'); Route::get('detail/:id', 'Article/detail');
// 文章搜索页 // 文章搜索页
Route::get('search', 'Article/search'); Route::get('search', 'Article/search');
// 文章留言
Route::post('comment', 'Article/comment');
}); });
// 数据迁移 // 数据迁移

View File

@@ -45,14 +45,14 @@
</div> </div>
<div class="repply"> <div class="repply">
<h3>{:lang('article.detail_leave_reply')}</h3> <h3>{:lang('article.detail_leave_reply')}</h3>
<form> <form action="{:url('article/comment', ['id' => $detail.id])}" method="POST" autocomplete="off">
<span>{:lang('article.detail_leave_reply_name')}</span> <span>{:lang('article.detail_leave_reply_name')}</span>
<input class="form-control itinp new_name" type="text" name="name" style="text-indent: 10px;"> <input class="form-control itinp new_name" type="text" name="name" style="text-indent: 10px;">
<span>{:lang('article.detail_leave_reply_email')}</span> <span>{:lang('article.detail_leave_reply_email')}</span>
<input class="form-control itinp new_email" type="email" name="email" style="text-indent: 10px; margin-bottom:0;"> <input class="form-control itinp new_email" type="email" name="email" style="text-indent: 10px; margin-bottom:0;">
<p style="color: #C6C7C9; font-size: 0.75rem; margin-bottom: 0.625rem;">{:lang('article.detail_leave_reply_email_tip')}</p> <p style="color: #C6C7C9; font-size: 0.75rem; margin-bottom: 0.625rem;">{:lang('article.detail_leave_reply_email_tip')}</p>
<span>{:lang('article.detail_leave_reply_comment')}</span> <span>{:lang('article.detail_leave_reply_comment')}</span>
<textarea class="form-control itinp new_comment" rows="3" style="text-indent: 10px;width: 98%; margin-top: 0.625rem;margin-bottom: 0.625rem;border: 1px solid #DBDBDB;"name="comment"></textarea> <textarea class="form-control itinp new_comment" name="content" rows="3" style="text-indent: 10px;width: 98%; margin-top: 0.625rem;margin-bottom: 0.625rem;border: 1px solid #DBDBDB;"></textarea>
<div class="comment_btn" style="color:#ffffff;">{:lang('article.detail_leave_reply_submit')}</div> <div class="comment_btn" style="color:#ffffff;">{:lang('article.detail_leave_reply_submit')}</div>
</form> </form>
</div> </div>
@@ -82,3 +82,25 @@
{/notempty} {/notempty}
</div> </div>
{/block} {/block}
{block name="script"}
<script type="text/javascript">
$(document).ready(function() {
$('.comment_btn').click(function() {
var form = $(this).parents('form');
var form_data = form.serialize();
$.ajax({
url: form.attr('action'),
type: 'POST',
data: form_data,
dataType: 'json',
success: function(data) {
alert(data.msg);
if (data.code == 0) {
window.location.reload();
}
}
})
})
})
</script>
{/block}