mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-04 00:24:29 +08:00
增加访问记录功能
This commit is contained in:
@@ -5,93 +5,110 @@ namespace app\index\controller;
|
||||
use app\model\Category;
|
||||
use app\model\Nav;
|
||||
use app\model\Post;
|
||||
use app\model\PostVisit;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Session;
|
||||
use think\facade\View;
|
||||
use think\helper\Str;
|
||||
use UserHub\Client;
|
||||
|
||||
class Common extends BaseController
|
||||
{
|
||||
public function initialize()
|
||||
{
|
||||
parent::initialize();
|
||||
protected $userinfo = [];
|
||||
|
||||
$site_logo = get_source_link(get_system_config('site_logo'));
|
||||
public function initialize()
|
||||
{
|
||||
parent::initialize();
|
||||
|
||||
View::assign('site_logo', $site_logo);
|
||||
$site_logo_box = get_source_link(get_system_config('site_logo_box'));
|
||||
$site_logo = get_source_link(get_system_config('site_logo'));
|
||||
|
||||
View::assign('site_logo_box', $site_logo_box);
|
||||
View::assign('site_logo', $site_logo);
|
||||
$site_logo_box = get_source_link(get_system_config('site_logo_box'));
|
||||
|
||||
$list_nav_slide = Nav::where('type', 3)->cacheAlways('type_list_3')->order('sort asc')->where('status', 1)->select();
|
||||
View::assign('site_logo_box', $site_logo_box);
|
||||
|
||||
View::assign('list_nav_slide', $list_nav_slide);
|
||||
$list_nav_slide = Nav::where('type', 3)->cacheAlways('type_list_3')->order('sort asc')->where('status', 1)->select();
|
||||
|
||||
$list_nav_friend_url = Nav::where('type', 2)->cacheAlways('type_list_2')->order('sort asc')->where('status', 1)->select();
|
||||
View::assign('list_nav_friend_url', $list_nav_friend_url);
|
||||
View::assign('list_nav_slide', $list_nav_slide);
|
||||
|
||||
$list_header_nav = Nav::where('type', 11)->cacheAlways('type_list_11')->order('sort asc')->where('status', 1)->select();
|
||||
View::assign('list_header_nav', $list_header_nav);
|
||||
$list_nav_friend_url = Nav::where('type', 2)->cacheAlways('type_list_2')->order('sort asc')->where('status', 1)->select();
|
||||
View::assign('list_nav_friend_url', $list_nav_friend_url);
|
||||
|
||||
$list_category_first_level = Category::where('level', 1)->where('status', 1)->where('type', 3)->cacheAlways('category_type_list_3')->order('sort asc')->select();
|
||||
View::assign('list_category_first_level', $list_category_first_level);
|
||||
$list_nav_more = Nav::where('type', 8)->cacheAlways('type_list_8')->order('sort asc')->where('status', 1)->select();
|
||||
View::assign('list_nav_more', $list_nav_more);
|
||||
$list_header_nav = Nav::where('type', 11)->cacheAlways('type_list_11')->order('sort asc')->where('status', 1)->select();
|
||||
View::assign('list_header_nav', $list_header_nav);
|
||||
|
||||
$top_posts = Post::where('is_top', 1)->limit(8)->where('type', 3)->cacheAlways('top_post')->select();
|
||||
View::assign('top_posts', $top_posts);
|
||||
$list_category_first_level = Category::where('level', 1)->where('status', 1)->where('type', 3)->cacheAlways('category_type_list_3')->order('sort asc')->select();
|
||||
View::assign('list_category_first_level', $list_category_first_level);
|
||||
$list_nav_more = Nav::where('type', 8)->cacheAlways('type_list_8')->order('sort asc')->where('status', 1)->select();
|
||||
View::assign('list_nav_more', $list_nav_more);
|
||||
|
||||
$this->userHubLogin();
|
||||
}
|
||||
$top_posts = Post::where('is_top', 1)->limit(8)->where('type', 3)->cacheAlways('top_post')->select();
|
||||
View::assign('top_posts', $top_posts);
|
||||
|
||||
public function userHubLogin()
|
||||
{
|
||||
$user_uid = Session::get('user_uid');
|
||||
$user_info = [];
|
||||
if (empty($user_uid)) {
|
||||
$code = $this->request->param('code');
|
||||
$list_site_last_visit = PostVisit::order('id desc')
|
||||
->group('ip,uid')
|
||||
->limit(6)
|
||||
->cache(60)
|
||||
->select();
|
||||
View::assign('list_site_last_visit', $list_site_last_visit);
|
||||
|
||||
// 实例化客户端,传入相关参数
|
||||
$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);
|
||||
}
|
||||
$total_hits = Post::cache(60)->sum('hits');
|
||||
View::assign('total_hits', $total_hits);
|
||||
|
||||
public static function getUserInfo($uid)
|
||||
{
|
||||
$cache_key = 'user_uid_'.$uid;
|
||||
$user_info = Cache::get($cache_key);
|
||||
$total_week_hits = PostVisit::cache(60)->where('create_time', '>', time() - 86400 * 7)->count();
|
||||
$total_day_hits = PostVisit::cache(60)->where('create_time', '>', time() - 86400)->count();
|
||||
View::assign('total_week_hits', $total_week_hits);
|
||||
View::assign('total_day_hits', $total_day_hits);
|
||||
|
||||
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);
|
||||
$this->userHubLogin();
|
||||
}
|
||||
|
||||
return $user_info;
|
||||
}
|
||||
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);
|
||||
}
|
||||
$this->userinfo = $user_info;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,18 +8,16 @@ use app\model\PostCategory;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Session;
|
||||
use think\facade\View;
|
||||
use think\Request;
|
||||
|
||||
class Index extends Common
|
||||
{
|
||||
/**
|
||||
* 显示资源列表
|
||||
* 显示资源列表.
|
||||
*
|
||||
* @return \think\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
|
||||
$page_cache_key = md5($this->request->url());
|
||||
|
||||
$content = Cache::get($page_cache_key);
|
||||
@@ -47,20 +45,18 @@ class Index extends Common
|
||||
if (empty($sub_category_id)) {
|
||||
$categorys = [$category_id];
|
||||
|
||||
$categorys = array_merge($categorys, array_column((array)Category::getListLevel($category_id), 3));
|
||||
$categorys = array_merge($categorys, array_column((array) Category::getListLevel($category_id), 3));
|
||||
|
||||
$categorys_where = PostCategory::whereIn('category_id', $categorys);
|
||||
|
||||
$model_post = Post::hasWhere('categorys', $categorys_where)->where('status', 1)->order('id desc');
|
||||
} else {
|
||||
|
||||
$model_sub_category = Category::find($sub_category_id);
|
||||
|
||||
$page_title .= "-{$model_sub_category->title}";
|
||||
$model_post = Post::hasWhere('categorys', ['category_id' => $sub_category_id])->where('status', 1)->order('id desc');
|
||||
}
|
||||
} else {
|
||||
|
||||
$model_post = Post::where('status', 1)->order('id desc');
|
||||
}
|
||||
|
||||
@@ -84,9 +80,9 @@ class Index extends Common
|
||||
'category_id' => $this->request->param('category_id'),
|
||||
'sub_category_id' => $this->request->param('sub_category_id'),
|
||||
'page' => $page,
|
||||
'keywords' => $keywords
|
||||
'keywords' => $keywords,
|
||||
],
|
||||
'list_rows' => 10
|
||||
'list_rows' => 10,
|
||||
]);
|
||||
|
||||
View::assign('current_category', $current_category);
|
||||
@@ -102,13 +98,12 @@ class Index extends Common
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
public function logout()
|
||||
{
|
||||
Session::clear();
|
||||
|
||||
$back_url = $this->request->param('back_url', '/');
|
||||
|
||||
return $this->success('退出成功', $back_url);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,17 +5,16 @@ declare(strict_types=1);
|
||||
namespace app\index\controller;
|
||||
|
||||
use app\model\Post as ModelPost;
|
||||
use app\model\PostCategory;
|
||||
use app\model\PostVisit;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Db;
|
||||
use think\facade\View;
|
||||
use think\model\Relation;
|
||||
use think\Request;
|
||||
|
||||
class Post extends Common
|
||||
{
|
||||
|
||||
/**
|
||||
* 显示指定的资源
|
||||
* 显示指定的资源.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\Response
|
||||
@@ -29,31 +28,45 @@ class Post extends Common
|
||||
$model_post = Cache::get($cache_key);
|
||||
|
||||
if (empty($model_post)) {
|
||||
|
||||
$model_post = ModelPost::with([
|
||||
'comments' => function (Relation $query) {
|
||||
$query->order('id asc');
|
||||
}
|
||||
},
|
||||
])->where('uid', $uid)->find();
|
||||
|
||||
if (empty($model_post)) {
|
||||
return $this->error('链接已失效', '/');
|
||||
}
|
||||
|
||||
Cache::set($cache_key, $model_post,600);
|
||||
Cache::set($cache_key, $model_post, 600);
|
||||
}
|
||||
|
||||
|
||||
$model_post->hits = $model_post->hits + 1;
|
||||
$model_post->hits = Db::raw('hits + 1');
|
||||
|
||||
$model_post->save();
|
||||
|
||||
|
||||
$this->recordVisit($model_post->id);
|
||||
|
||||
$list_last_visit = PostVisit::where('post_id', $model_post->id)
|
||||
->order('id desc')
|
||||
->group('ip,uid')
|
||||
->limit(12)
|
||||
->cache(60)
|
||||
->select();
|
||||
View::assign('post', $model_post);
|
||||
|
||||
View::assign('list_last_visit', $list_last_visit);
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
public function recordVisit($post_id)
|
||||
{
|
||||
$model_visit = new PostVisit();
|
||||
$model_visit->uid = $this->userinfo['uid'] ?? '';
|
||||
$model_visit->post_id = $post_id;
|
||||
$model_visit->avatar = $this->userinfo['avatar'] ?? '';
|
||||
$model_visit->nickname = $this->userinfo['nickname'] ?? '';
|
||||
$model_visit->ip = $this->request->ip();
|
||||
$model_visit->save();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user