diff --git a/app/BaseController.php b/app/BaseController.php index ddcaf9b..8451177 100644 --- a/app/BaseController.php +++ b/app/BaseController.php @@ -8,7 +8,7 @@ // +---------------------------------------------------------------------- // | Author: liu21st // +---------------------------------------------------------------------- -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; + } } diff --git a/app/admin/controller/PostComment.php b/app/admin/controller/PostComment.php new file mode 100644 index 0000000..6db4901 --- /dev/null +++ b/app/admin/controller/PostComment.php @@ -0,0 +1,101 @@ +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(); + } +} diff --git a/app/common/ColumnFormat.php b/app/common/ColumnFormat.php index cf3d21e..d556bb9 100644 --- a/app/common/ColumnFormat.php +++ b/app/common/ColumnFormat.php @@ -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); diff --git a/app/index/controller/Common.php b/app/index/controller/Common.php index 7ac59e5..9c89503 100644 --- a/app/index/controller/Common.php +++ b/app/index/controller/Common.php @@ -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; + } } diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index 8546f71..7669af6 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -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); + } + /** * 显示创建资源表单页. * diff --git a/app/index/controller/Post.php b/app/index/controller/Post.php index 950c744..aeca4b0 100644 --- a/app/index/controller/Post.php +++ b/app/index/controller/Post.php @@ -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; diff --git a/app/index/controller/PostComment.php b/app/index/controller/PostComment.php new file mode 100644 index 0000000..94a9138 --- /dev/null +++ b/app/index/controller/PostComment.php @@ -0,0 +1,118 @@ +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(); + } +} diff --git a/app/model/Post.php b/app/model/Post.php index 3b3048d..efb3e5f 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -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; } diff --git a/app/model/PostComment.php b/app/model/PostComment.php new file mode 100644 index 0000000..75ab487 --- /dev/null +++ b/app/model/PostComment.php @@ -0,0 +1,35 @@ +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; + } +} diff --git a/composer.json b/composer.json index 1d4fadc..39de9aa 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "topthink/think-migration": "^3.0", "topthink/think-helper": "^3.1", "topthink/think-captcha": "^3.0", - "topthink/think-multi-app": "^1.0" + "topthink/think-multi-app": "^1.0", + "ulthon/user_hub_client": "^1.0" }, "require-dev": { "symfony/var-dumper": "^4.2" diff --git a/config/app.php b/config/app.php index bd93738..06d77d3 100644 --- a/config/app.php +++ b/config/app.php @@ -23,10 +23,10 @@ return [ 'app_map' => [], // 域名绑定(自动多应用模式有效) 'domain_bind' => [ - '*'=>'index', 'www'=>'index', 'admin'=>'admin', 'api'=>'api', + '*'=>'index', ], // 应用默认域名(自动多应用模式有效,为空时采用domain_bind的设置键值对换,适合跨应用生成url) 'app_default_doamin' => [], diff --git a/config/session.php b/config/session.php index c1ef6e1..a769bad 100644 --- a/config/session.php +++ b/config/session.php @@ -13,7 +13,7 @@ return [ // 存储连接标识 当type使用cache的时候有效 'store' => null, // 过期时间 - 'expire' => 1440, + 'expire' => 3600*24*7, // 前缀 'prefix' => '', ]; diff --git a/database/migrations/20200418120809_create_table_post.php b/database/migrations/20200418120809_create_table_post.php index 07f4e38..efb1809 100644 --- a/database/migrations/20200418120809_create_table_post.php +++ b/database/migrations/20200418120809_create_table_post.php @@ -36,8 +36,8 @@ class CreateTablePost extends Migrator $table->addColumn(ColumnFormat::timestamp('create_time')); $table->addColumn(ColumnFormat::timestamp('update_time')); $table->addColumn(ColumnFormat::timestamp('delete_time')); - $table->addColumn(Column::make('content','text')); - $table->addColumn(Column::make('content_html','text')); + $table->addColumn(Column::longText('content')); + $table->addColumn(Column::longText('content_html')); $table->addColumn(ColumnFormat::stringLong('desc')->setDefault('描述')); $table->addColumn(ColumnFormat::integerTypeStatus('is_top')->setComment('是否置顶')); $table->addColumn(ColumnFormat::integerTypeStatus('status')->setComment('1:显示,0:不显示')); diff --git a/database/migrations/20210103102340_create_table_post_comment.php b/database/migrations/20210103102340_create_table_post_comment.php new file mode 100644 index 0000000..31ceed8 --- /dev/null +++ b/database/migrations/20210103102340_create_table_post_comment.php @@ -0,0 +1,45 @@ +table('post_comment'); + + $table->addColumn(ColumnFormat::integer('post_id')) + ->addColumn(ColumnFormat::stringNormal('user_uid')) + ->addColumn(ColumnFormat::stringLong('content')) + ->addColumn(ColumnFormat::timestamp('create_time')) + ->addColumn(ColumnFormat::timestamp('update_time')) + ->addColumn(ColumnFormat::timestamp('delete_time')) + ->addColumn(ColumnFormat::stringShort('type')->setComment('类型,1:文章,有分类有标签,2:页面,无分类无标签,N:其他形式用,用于区分不同的用途')) + ->addIndex('post_id'); + + $table->create(); + } +} diff --git a/view/admin/common/left_post.html b/view/admin/common/left_post.html index 004b73a..e3f5638 100644 --- a/view/admin/common/left_post.html +++ b/view/admin/common/left_post.html @@ -11,6 +11,9 @@
  • 标签管理
  • +
  • + 评论管理 +
  • \ No newline at end of file diff --git a/view/admin/post_comment/index.html b/view/admin/post_comment/index.html new file mode 100644 index 0000000..1ebae1c --- /dev/null +++ b/view/admin/post_comment/index.html @@ -0,0 +1,102 @@ + + + + + + + + 应用管理 + {include file="common/_require"} + + + + + + +
    + {include file="common/_header"} + + {include file="common/left_post"} + +
    + +
    +
    + + 首页 + 系统信息 + +
    +
    + + + + + + + + + + + + + + {volist name='list' id='vo'} + + + + + + + + + + + {/volist} + {if condition="count($list) == 0" } + + + + {/if} + +
    ID文章评论用户评论时间操作
    {$vo.id}{$vo.post.title}{$vo.content} +

    {$vo.user.nickname}

    +

    {$vo.user.account}

    +
    {$vo.create_time} +
    + 查看 +
    删除
    +
    +
    暂无数据
    +
    + {$list|raw} +
    +
    +
    +
    + + + {include file="common/_footer"} +
    + + + + + + \ No newline at end of file diff --git a/view/admin/system/others.html b/view/admin/system/others.html index a9a9252..926c9a9 100644 --- a/view/admin/system/others.html +++ b/view/admin/system/others.html @@ -2,256 +2,93 @@ - - - - 系统管理 - {include file="common/_require"} + + + + 系统管理 + {include file="common/_require"} - - + + -
    - {include file="common/_header"} +
    + {include file="common/_header"} - {include file="common/left_system"} + {include file="common/left_system"} -
    +
    -
    - -
    -
    - -
    -
    - 阿里OSS存储设置 -
    -
    -
    -
    AccessKey
    -
    - -
    -
    -
    -
    SecretKey
    -
    - -
    -
    -
    -
    空间名称
    -
    - -
    -
    -
    -
    空间域名
    -
    - -
    -
    - -
    - -
    -
    -
    -
    - -
    -
    -
    - -
    -
    - 阿里短信配置 -
    -
    -
    -
    AccessKey
    -
    - -
    -
    -
    -
    AccessSecret
    -
    - -
    -
    -
    -
    模板ID
    -
    - -
    -
    - -
    - -
    -
    -
    -
    - -
    -
    -
    - -
    -
    - 微信第三方平台配置 -
    -
    -
    -
    AppID
    -
    - -
    -
    -
    -
    AppSecret
    -
    - -
    -
    -
    -
    消息校验Token
    -
    - -
    -
    -
    -
    消息加解密Key
    -
    - -
    -
    -
    - -
    -
    -
    -
    - -
    -
    -
    -
    -
    - 微信公众号配置 -
    -
    -
    -
    公众号名称
    -
    - -
    -
    -
    -
    AppID
    -
    - -
    -
    -
    -
    AppSecret
    -
    - -
    -
    -
    -
    Token
    -
    - -
    -
    -
    -
    - -
    保存
    - -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    - 微信支付配置 -
    -
    -
    -
    商户ID
    -
    - -
    -
    -
    -
    商户秘钥
    -
    - - -
    -
    -
    -
    商户key
    -
    - -
    上传
    -
    -
    -
    -
    商户cert
    -
    - -
    上传
    -
    -
    -
    - -
    -
    -
    -
    -
    -
    -
    -
    +
    + +
    +
    +
    +
    + UserHub +
    +
    +
    +
    key
    +
    + +
    +
    +
    +
    secret
    +
    + +
    +
    +
    +
    域名
    +
    + +
    +
    +
    + +
    +
    +
    +
    - {include file="common/_footer"} +
    +
    + +
    +
    - + + {include file="common/_footer"} +
    + \ No newline at end of file diff --git a/view/index/post/read.html b/view/index/post/read.html index 16c2df1..8c227f1 100644 --- a/view/index/post/read.html +++ b/view/index/post/read.html @@ -40,6 +40,28 @@ border-radius: 2px; cursor: pointer; } + + .post-container { + margin-top: 15px; + } + + .post-item { + display: flex; + justify-content: flex-start; + align-items: flex-start; + } + + .post-item .left img { + width: 60px; + } + + .post-item .right { + margin-left: 15px; + } + + .post-item .content { + margin-top: 5px; + } @@ -64,8 +86,7 @@
    {if !empty($post.jump_to_url) && $post.jump_to_url_status != 0} {if $post.jump_to_url_status == 2 }