TaoLer/app/index/controller/Index.php

163 lines
5.2 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
namespace app\index\controller;
use app\common\controller\BaseController;
use think\facade\View;
use think\facade\Request;
use think\facade\Db;
2020-03-05 20:15:01 +08:00
use think\facade\Cache;
2020-01-01 13:17:19 +08:00
use app\common\model\Article;
use app\common\model\User;
use app\common\model\Cate;
use app\common\model\Comment;
class Index extends BaseController
{
public function index()
{
2020-01-08 17:37:41 +08:00
$types = input('type');
2020-01-01 13:17:19 +08:00
//幻灯
2020-03-05 20:15:01 +08:00
$sliders = Cache::get('slider');
if(!$sliders){
$sliders = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',1)->whereTime('slid_over','>=',time())->select();
Cache::set('slider',$sliders,3600);
}
2020-01-01 13:17:19 +08:00
//置顶文章
2020-03-05 20:15:01 +08:00
$artTop = Cache::get('arttop');
if(!$artTop){
2020-04-25 17:36:24 +08:00
$artTop = Article::field('id,title,title_color,cate_id,user_id,create_time,is_top,jie')->where(['is_top'=>1,'status'=>1,'delete_time'=>0])->with([
2020-01-01 13:17:19 +08:00
'cate' => function($query){
2020-01-08 17:37:41 +08:00
$query->where('delete_time',0)->field('id,catename');
},
'user' => function($query){
2020-03-30 21:11:12 +08:00
$query->field('id,name,nickname,user_img,area_id,vip');
2020-01-08 17:37:41 +08:00
}
2020-03-05 20:15:01 +08:00
])->withCount(['comments'])->order('create_time','desc')->limit(5)->select();
2020-04-19 14:29:34 +08:00
Cache::tag('tagArtDetail')->set('arttop',$artTop,60);
2020-03-05 20:15:01 +08:00
}
//首页文章显示20条
$artList = Cache::get('artlist');
if(!$artList){
2020-04-25 17:36:24 +08:00
$artList = Article::field('id,title,title_color,cate_id,user_id,create_time,is_hot,jie')->with([
2020-01-08 17:37:41 +08:00
'cate' => function($query){
$query->where('delete_time',0)->field('id,catename');
},
'user' => function($query){
2020-03-30 21:11:12 +08:00
$query->field('id,name,nickname,user_img,area_id,vip');
2020-01-08 17:37:41 +08:00
}
2020-03-05 20:15:01 +08:00
])->withCount(['comments'])->where(['status'=>1,'delete_time'=>0])->order('create_time','desc')->limit(20)->select();
2020-04-19 14:29:34 +08:00
Cache::tag('tagArt')->set('artlist',$artList,60);
2020-03-05 20:15:01 +08:00
}
2020-01-01 13:17:19 +08:00
//热议文章
2020-03-28 21:41:07 +08:00
$artHot = Article::field('id,title')->withCount('comments')->where(['status'=>1,'delete_time'=>0])->whereTime('create_time', 'year')->order('comments_count','desc')->limit(10)->withCache(60)->select();
2020-03-05 20:15:01 +08:00
2020-01-01 13:17:19 +08:00
//首页赞助
2020-03-05 20:15:01 +08:00
$ad_index = Cache::get('adindex');
if(!$ad_index){
$ad_index = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>3])->whereTime('slid_over','>=',time())->select();
Cache::set('adindex',$ad_index,3600);
}
2020-01-01 13:17:19 +08:00
//首页右栏
2020-03-05 20:15:01 +08:00
$ad_comm = Cache::get('adcomm');
if(!$ad_comm){
$ad_comm = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>2])->whereTime('slid_over','>=',time())->select();
Cache::set('adcomm',$ad_comm,3600);
}
2020-01-01 13:17:19 +08:00
//友情链接
2020-03-05 20:15:01 +08:00
$friend_links = Cache::get('flinks');
if(!$friend_links){
$friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select();
Cache::set('flinks',$friend_links,3600);
}
2020-01-01 13:17:19 +08:00
$vs = [
'slider' => $sliders,
'artTop' => $artTop,
'artList' => $artList,
'artHot' => $artHot,
'type' => $types,
'ad_index' => $ad_index,
'ad_comm' => $ad_comm,
'flinks' => $friend_links,
];
View::assign($vs);
return View::fetch();
}
//回帖榜
public function reply()
{
2020-03-28 21:41:07 +08:00
$user = User::withCount('comments')->order(['comments_count'=>'desc','last_login_time'=>'desc'])->limit(20)->select();
2020-01-01 13:17:19 +08:00
if($user)
{
$res['status'] = 0;
$res['data'] = array();
foreach ($user as $key=>$v) {
$u['uid'] = $v['id'];
$u['count(*)'] = $v['comments_count'];
if($v['nickname'])
{
$u['user'] = ['username'=>$v['nickname'],'avatar'=>$v['user_img']];
} else {
$u['user'] = ['username'=>$v['name'],'avatar'=>$v['user_img']];
}
$res['data'][] = $u;
}
}
return json($res);
}
//搜索功能
public function search()
{
//$this->showUser();
//全局查询条件
$map = []; //所有的查询条件封装到数组中
//条件1
$map[] = ['status','=',1]; //这里等号不能省略
//实现搜索功能
2020-02-27 20:27:55 +08:00
$keywords = Request::only(['keywords']);
//var_dump($keywords['keywords']);
if(!empty($keywords['keywords'])){
2020-01-01 13:17:19 +08:00
//条件2
2020-02-27 20:27:55 +08:00
$map[] = ['title','like','%'.$keywords['keywords'].'%'];
2020-01-01 13:17:19 +08:00
$artList = Article::where($map)->withCount('comments')->order('create_time','desc')->paginate(10);
$counts = $artList->count();
$searchs = [
'artList' => $artList,
2020-02-27 20:27:55 +08:00
'keywords' => $keywords['keywords'],
2020-01-01 13:17:19 +08:00
'counts' => $counts
2020-02-27 20:27:55 +08:00
];
} else {
return response('输入非法');
}
2020-01-01 13:17:19 +08:00
View::assign($searchs);
2020-02-27 20:27:55 +08:00
//友情链接
$friend_links = Db::name('slider')->where(['slid_status'=>1,'delete_time'=>0,'slid_type'=>6])->whereTime('slid_over','>=',time())->field('slid_name,slid_href')->select();
2020-01-01 13:17:19 +08:00
// 查询热议
$artHot = Article::withCount('comments')->field('title,comments_count')->where('status',1)->whereTime('create_time', 'year')->order('comments_count','desc')->limit(10)->select();
2020-02-27 20:27:55 +08:00
View::assign(['flinks'=>$friend_links,'artHot'=>$artHot]);
2020-01-01 13:17:19 +08:00
return View::fetch('search');
}
public function jump()
{
$username = Request::param('username');
$u = Db::name('user')->whereOr('nickname', $username)->whereOr('name', $username)->find();
2020-04-30 18:35:46 +08:00
return redirect((string) url('user/home',['id'=>$u['id']]));
2020-01-01 13:17:19 +08:00
}
}