完成评论

This commit is contained in:
augushong
2021-01-03 21:52:54 +08:00
parent bcc07c301e
commit 3b5a56aef5
18 changed files with 815 additions and 383 deletions

View File

@@ -8,7 +8,7 @@
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
declare(strict_types=1);
namespace app;
@@ -24,147 +24,151 @@ use think\exception\HttpResponseException;
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* Request实例
* @var \think\Request
*/
protected $request;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 是否批量验证
* @var bool
*/
protected $batchValidate = false;
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 控制器中间件
* @var array
*/
protected $middleware = [];
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
// 控制器初始化
$this->initialize();
// 控制器初始化
$this->initialize();
}
// 初始化
protected function initialize()
{
}
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
// 初始化
protected function initialize()
{}
$v->message($message);
/**
* 验证数据
* @access protected
* @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息
* @param bool $batch 是否批量验证
* @return array|string|true
* @throws ValidateException
*/
protected function validate(array $data, $validate, array $message = [], bool $batch = false)
{
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
if (strpos($validate, '.')) {
// 支持场景
list($validate, $scene) = explode('.', $validate);
}
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
return $v->failException(true)->check($data);
// 是否批量验证
if ($batch || $this->batchValidate) {
$v->batch(true);
}
public function success($msg = '操作成功',$jump_to_url = null,$code = 200,$params = [])
{
return $v->failException(true)->check($data);
}
if(is_null($jump_to_url)){
$jump_to_url = \request()->server('HTTP_REFERER');
}else{
if($jump_to_url instanceof Url){
public function success($msg = '操作成功', $jump_to_url = null, $code = 200, $params = [])
{
$jump_to_url = $this->parseJumpUrl($jump_to_url);
$data = [
'msg' => $msg,
'jump_to_url' => $jump_to_url,
'params' => $params
];
$jump_to_url = $jump_to_url;
}else{
$jump_to_url = url($jump_to_url);
}
}
$data = [
'msg'=>$msg,
'jump_to_url'=>$jump_to_url,
'params'=>$params
];
if(\request()->isAjax()){
$data['jump_to_url'] = (string)$jump_to_url;
if($code == 200){
$code = 0;
}
throw new HttpResponseException(json_message($data,$code,$msg));
}
View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/success'),$code));
}
public function error($msg = '操作失败',$jump_to_url = null,$code = 200,$params = [])
{
if(is_null($jump_to_url)){
$jump_to_url = \request()->server('HTTP_REFERER');
}else{
if($jump_to_url instanceof Url){
$jump_to_url = $jump_to_url;
}else{
$jump_to_url = url($jump_to_url);
}
}
$data = [
'msg'=>$msg,
'jump_to_url'=>$jump_to_url,
'params'=>$params
];
if(\request()->isAjax()){
$data['jump_to_url'] = (string)$jump_to_url;
if($code == 200){
$code = 500;
}
throw new HttpResponseException(json_message($data,$code,$msg));
}
View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/error'),$code));
if (\request()->isAjax()) {
$data['jump_to_url'] = $jump_to_url;
if ($code == 200) {
$code = 0;
}
throw new HttpResponseException(json_message($data, $code, $msg));
}
View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/success'), $code));
}
public function error($msg = '操作失败', $jump_to_url = null, $code = 200, $params = [])
{
$jump_to_url = $this->parseJumpUrl($jump_to_url);
$data = [
'msg' => $msg,
'jump_to_url' => $jump_to_url,
'params' => $params
];
if (\request()->isAjax()) {
$data['jump_to_url'] = $jump_to_url;
if ($code == 200) {
$code = 500;
}
throw new HttpResponseException(json_message($data, $code, $msg));
}
View::assign($data);
throw new HttpResponseException(response(View::fetch('common@tpl/error'), $code));
}
public function redirect($jump_to_url, $code = 302)
{
$jump_to_url = $this->parseJumpUrl($jump_to_url);
throw new HttpResponseException(redirect($jump_to_url), $code);
}
public function parseJumpUrl($jump_to_url)
{
if (is_null($jump_to_url)) {
$jump_to_url = \request()->server('HTTP_REFERER');
} else {
if ($jump_to_url instanceof Url) {
$jump_to_url = (string)$jump_to_url;
} else {
if (strpos($jump_to_url, 'http') !== 0) {
$jump_to_url = url($jump_to_url);
}
}
}
return (string)$jump_to_url;
}
}

View File

@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\model\PostComment as ModelPostComment;
use think\facade\View;
use think\Request;
class PostComment extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
$type = $this->request->param('type', 1);
$list = ModelPostComment::order('id desc')
->where('type', $type)
->paginate();
View::assign('list',$list);
return View::fetch();
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
ModelPostComment::destroy($id);
return json_message();
}
}

View File

@@ -56,7 +56,7 @@ class ColumnFormat
public static function integer($name)
{
return Column::make($name,'integer')
return Column::bigInteger($name)
->setDefault(0)
->setLimit(20)
->setSigned(false);

View File

@@ -5,8 +5,11 @@ namespace app\index\controller;
use app\model\Category;
use app\model\Nav;
use app\model\Post;
use think\facade\Cache;
use think\facade\Session;
use think\facade\View;
use think\helper\Str;
use UserHub\Client;
class Common extends BaseController
{
@@ -14,7 +17,6 @@ class Common extends BaseController
{
parent::initialize();
$list_nav_slide = Nav::where('type', 3)->order('sort asc')->where('status', 1)->select();
View::assign('list_nav_slide', $list_nav_slide);
$list_nav_friend_url = Nav::where('type', 2)->order('sort asc')->where('status', 1)->select();
@@ -23,14 +25,64 @@ class Common extends BaseController
$list_header_nav = Nav::where('type', 11)->order('sort asc')->where('status', 1)->select();
View::assign('list_header_nav', $list_header_nav);
$list_category_first_level = Category::where('level', 1)->where('status', 1)->where('type',3)->order('sort asc')->select();
$list_category_first_level = Category::where('level', 1)->where('status', 1)->where('type', 3)->order('sort asc')->select();
View::assign('list_category_first_level', $list_category_first_level);
$list_nav_more = Nav::where('type', 8)->order('sort asc')->where('status', 1)->select();
View::assign('list_nav_more', $list_nav_more);
$top_posts = Post::where('is_top',1)->limit(8)->where('type',3)->select();
View::assign('top_posts',$top_posts);
$top_posts = Post::where('is_top', 1)->limit(8)->where('type', 3)->select();
View::assign('top_posts', $top_posts);
$this->userHubLogin();
}
public function userHubLogin()
{
$user_uid = Session::get('user_uid');
$user_info = [];
if (empty($user_uid)) {
$code = $this->request->param('code');
// 实例化客户端,传入相关参数
$user_hub_client = new Client([
'key' => get_system_config('user_hub_key'),
'secret' => get_system_config('user_hub_secret'),
'host' => get_system_config('user_hub_host'),
]);
if (empty($code)) {
// 跳转登录
$url = $user_hub_client->getBowserRedirectUrl($this->request->url(true));
View::assign('login_url',$url);
} else {
// 获取用户信息
$user_info = $user_hub_client->getUserinfoByCode($code);
Session::set('user_uid', $user_info['uid']);
}
}else{
$user_info = self::getUserInfo($user_uid);
}
View::assign('user_info',$user_info);
}
public static function getUserInfo($uid)
{
$cache_key = 'user_uid_'.$uid;
$user_info = Cache::get($cache_key);
if(is_null($user_info)){
// 实例化客户端,传入相关参数
$user_hub_client = new Client([
'key' => get_system_config('user_hub_key'),
'secret' => get_system_config('user_hub_secret'),
'host' => get_system_config('user_hub_host'),
]);
$user_info = $user_hub_client->getUserinfoByUid($uid);
Cache::set($cache_key,$user_info);
}
return $user_info;
}
}

View File

@@ -5,6 +5,7 @@ namespace app\index\controller;
use app\model\Category;
use app\model\Post;
use app\model\PostCategory;
use think\facade\Session;
use think\facade\View;
use think\Request;
@@ -63,6 +64,14 @@ class Index extends Common
}
public function logout()
{
Session::clear();
$back_url = $this->request->param('back_url','/');
return $this->success('退出成功',$back_url);
}
/**
* 显示创建资源表单页.
*

View File

@@ -6,6 +6,7 @@ namespace app\index\controller;
use app\model\Post as ModelPost;
use think\facade\View;
use think\model\Relation;
use think\Request;
class Post extends Common
@@ -51,7 +52,11 @@ class Post extends Common
{
//
$model_post = ModelPost::where('uid',$uid)->find();
$model_post = ModelPost::with([
'comments' => function (Relation $query) {
$query->order('id asc');
}
])->where('uid', $uid)->find();
$model_post->hits = $model_post->hits + 1;

View File

@@ -0,0 +1,118 @@
<?php
declare(strict_types=1);
namespace app\index\controller;
use app\model\PostComment as ModelPostComment;
use think\facade\Session;
use think\facade\Validate;
use think\Request;
use think\validate\ValidateRule;
class PostComment extends Common
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
//
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
$user_uid = Session::get('user_uid');
if (empty($user_uid)) {
return json_message('请先登录');
}
$post_data = $request->post();
$validate = Validate::rule('post_id', ValidateRule::isRequire())
->rule('content|评论内容', ValidateRule::isRequire()->length('5,200', '评论字符长度需要在5-200之间'));
if (!$validate->check($post_data)) {
return json_message($validate->getError());
}
$post_data['type'] = 3;
$post_data['user_uid'] = $user_uid;
ModelPostComment::create($post_data);
return json_message();
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
ModelPostComment::destroy($id);
return json_message();
}
}

View File

@@ -34,6 +34,11 @@ class Post extends Model
return $this->hasMany(PostTag::class, 'post_id');
}
public function comments()
{
return $this->hasMany(PostComment::class,'post_id');
}
public function setPublishTimeAttr($value)
{
return strtotime($value);
@@ -149,9 +154,9 @@ class Post extends Model
{
$share_text = get_system_config('post_share_text_tpl');
$share_text = str_replace('%post_title%',$this->getAttr('title'),$share_text);
$share_text = str_replace('%post_desc%',$this->getAttr('desc'),$share_text);
$share_text = str_replace('%post_url%',$this->getAttr('read_url'),$share_text);
$share_text = str_replace('%post_title%', $this->getAttr('title'), $share_text);
$share_text = str_replace('%post_desc%', $this->getAttr('desc'), $share_text);
$share_text = str_replace('%post_url%', $this->getAttr('read_url'), $share_text);
return $share_text;
}

35
app/model/PostComment.php Normal file
View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace app\model;
use app\index\controller\Common;
use think\Model;
/**
* @mixin \think\Model
*/
class PostComment extends Model
{
//
public function post()
{
return $this->belongsTo(Post::class, 'post_id');
}
public function getUserAttr()
{
return Common::getUserInfo($this->getData('user_uid'));
}
public function getReadUrlAttr()
{
$model_post = $this->getAttr('post');
$read_url = $model_post->read_url . '#comment-' . $this->getData('id');
return $read_url;
}
}