完成评论

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

@@ -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();
}
}