增加访问记录功能

This commit is contained in:
2023-06-28 11:12:30 +08:00
parent a12cca37e4
commit 65e1800c6f
18 changed files with 447 additions and 98 deletions

View File

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