You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
7.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Home;
  3. use App\Http\Controllers\Home\CommonController;
  4. use App\Http\Model\Article;
  5. use App\Http\Model\Arctype;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Http\Request;
  8. class IndexController extends CommonController
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. //首页
  15. public function index()
  16. {
  17. return view('home.index.index');
  18. }
  19. //列表页
  20. public function category($cat, $page)
  21. {
  22. $pagenow=page;
  23. if(empty($cat) || !preg_match('/[0-9]+/',$cat)){error_jump('您访问的页面不存在或已被删除!', route('page404'));}
  24. if(cache("catid$cat")){$post=cache("catid$cat");}else{$post = db('arctype')->where("id=$cat")->find();if(empty($post)){error_jump('您访问的页面不存在或已被删除!', route('page404'));}cache("catid$cat",$post,2592000);}
  25. $this->assign('post',$post);
  26. $subcat="";$sql="";
  27. $post2=db('arctype')->field('id')->where("reid=$cat")->select();
  28. if(!empty($post2)){foreach($post2 as $row){$subcat=$subcat."typeid=".$row["id"]." or ";}}
  29. $subcat=$subcat."typeid=".$cat;
  30. $sql=$subcat." or typeid2 in (".$cat.")";//echo $subcat2;exit;
  31. $this->assign('sql',$sql);
  32. $counts=db("article")->where($sql)->count('id');
  33. if($counts>CMS_MAXARC){$counts=CMS_MAXARC;}
  34. $pagesize=CMS_PAGESIZE;$page=0;
  35. if($counts % $pagesize){//取总数据量除以每页数的余数
  36. $pages = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  37. }else{$pages = $counts/$pagesize;}
  38. if(!empty($pagenow)){if($pagenow==1 || $pagenow>$pages){header("HTTP/1.0 404 Not Found");error_jump('您访问的页面不存在或已被删除!', route('page404'));}$page = $pagenow-1;$nextpage=$pagenow+1;$previouspage=$pagenow-1;}else{$page = 0;$nextpage=2;$previouspage=0;}
  39. $this->assign('page',$page);
  40. $this->assign('pages',$pages);
  41. $this->assign('counts',$counts);
  42. $start=$page*$pagesize;
  43. $this->assign('posts',arclist(array("sql"=>$sql,"limit"=>"$start,$pagesize"))); //获取列表
  44. $this->assign('pagenav',get_listnav(array("counts"=>$counts,"pagesize"=>$pagesize,"pagenow"=>$page+1,"catid"=>$cat))); //获取分页列表
  45. if($post['templist']=='category2'){if(!empty($pagenow)){error_jump('您访问的页面不存在或已被删除!', route('page404'));}}
  46. return $this->fetch($post['templist']);
  47. return view('home.index.index');
  48. }
  49. //文章详情页
  50. public function detail($id)
  51. {
  52. if(empty($id) || !preg_match('/[0-9]+/',$id)){error_jump('您访问的页面不存在或已被删除!', route('page404'));}
  53. if(cache("detailid$id")){$post = cache("detailid$id");}else{$post = object_to_array(DB::table('article')->where('id', $id)->first(), 1);if(empty($post)){error_jump('您访问的页面不存在或已被删除!', route('page404'));}$post['typename'] = DB::table('arctype')->where('id', $post['typeid'])->value('typename');cache(["detailid$id"=>$post], \Carbon\Carbon::now()->addMinutes(2592000));}
  54. if($post)
  55. {
  56. $cat = $post['typeid'];
  57. $post['body'] = ReplaceKeyword($post['body']);
  58. if(!empty($post['writer'])){$post['writertitle']=$post['title'].' '.$post['writer'];}
  59. $data['post'] = $post;
  60. $data['pre'] = get_article_prenext(array('aid'=>$post["id"],'typeid'=>$post["typeid"],'type'=>"pre"));
  61. }
  62. else
  63. {
  64. error_jump('您访问的页面不存在或已被删除!', route('page404'));
  65. }
  66. if(cache("catid$cat")){$post=cache("catid$cat");}else{$post = object_to_array(DB::table('arctype')->where('id', $cat)->first(), 1);cache(["catid$cat"=>$post], \Carbon\Carbon::now()->addMinutes(2592000));}
  67. return view('home.index.'.$post['temparticle'], $data);
  68. }
  69. //标签详情页,共有3种显示方式,1正常列表,2列表显示文章,3显示描述
  70. public function tag($tag, $page)
  71. {
  72. $pagenow = $page;
  73. if(empty($tag) || !preg_match('/[0-9]+/',$tag)){error_jump('您访问的页面不存在或已被删除!', route('page404'));}
  74. if(cache("tagid$tag")){$post=cache("tagid$tag");}else{$post = db('tagindex')->where("id=$tag")->find();cache("tagid$tag",$post,2592000);}
  75. $this->assign('post',$post);
  76. $counts=db("taglist")->where("tid=$tag")->count('aid');
  77. if($counts>CMS_MAXARC){$counts=CMS_MAXARC;}
  78. $pagesize=CMS_PAGESIZE;$page=0;
  79. if($counts % $pagesize){//取总数据量除以每页数的余数
  80. $pages = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  81. }else{$pages = $counts/$pagesize;}
  82. if(!empty($pagenow)){if($pagenow==1 || $pagenow>$pages){header("HTTP/1.0 404 Not Found");error_jump('您访问的页面不存在或已被删除!', route('page404'));}$page = $pagenow-1;$nextpage=$pagenow+1;$previouspage=$pagenow-1;}else{$page = 0;$nextpage=2;$previouspage=0;}
  83. $this->assign('page',$page);
  84. $this->assign('pages',$pages);
  85. $this->assign('counts',$counts);
  86. $start=$page*$pagesize;
  87. $posts=db("taglist")->where("tid=$tag")->order('aid desc')->limit("$start,$pagesize")->select();
  88. foreach($posts as $row)
  89. {
  90. $aid[] = $row["aid"];
  91. }
  92. $aid = isset($aid)?implode(',',$aid):"";
  93. if($aid!="")
  94. {
  95. if($post['template']=='tag2')
  96. {
  97. $this->assign('posts',arclist(array("sql"=>"id in ($aid)","orderby"=>"id desc","limit"=>"$pagesize","field"=>"title,body"))); //获取列表
  98. }
  99. else
  100. {
  101. $this->assign('posts',arclist(array("sql"=>"id in ($aid)","orderby"=>"id desc","limit"=>"$pagesize"))); //获取列表
  102. }
  103. }
  104. else
  105. {
  106. $this->assign('posts',""); //获取列表
  107. }
  108. $this->assign('pagenav',get_listnav(array("counts"=>$counts,"pagesize"=>$pagesize,"pagenow"=>$page+1,"catid"=>$tag,"urltype"=>"tag"))); //获取分页列表
  109. if($post['template']=='tag2' || $post['template']=='tag3'){if(!empty($pagenow)){error_jump('您访问的页面不存在或已被删除!', route('page404'));}}
  110. return $this->fetch($post['template']);
  111. return view('home.index.index');
  112. }
  113. //标签页
  114. public function tags()
  115. {
  116. return view('home.index.tags');
  117. }
  118. //搜索页
  119. public function search()
  120. {
  121. if(!empty($_GET["keyword"]))
  122. {
  123. $keyword = $_GET["keyword"]; //搜索的关键词
  124. if(strstr($keyword,"&")) exit;
  125. $map['title'] = array('LIKE',"%$keyword%");
  126. $this->assign('posts',db("article")->field('body',true)->where($map)->order('id desc')->limit(30)->select());
  127. $this->assign('keyword',$keyword);
  128. }
  129. else
  130. {
  131. $this->error('请输入正确的关键词', '/' , 3);exit;
  132. }
  133. return view('home.index.search');
  134. }
  135. //单页面
  136. public function page($id)
  137. {
  138. if(!empty($id) && preg_match('/[a-z0-9]+/',$id))
  139. {
  140. $map['filename']=$id;
  141. if(cache("pageid$id")){$post=cache("pageid$id");}else{$post = db('page')->where($map)->find();cache("pageid$id",$post,2592000);}
  142. if($post)
  143. {
  144. $this->assign('post',$post);
  145. }
  146. else
  147. {
  148. error_jump('您访问的页面不存在或已被删除!', route('page404'));
  149. }
  150. }
  151. else
  152. {
  153. error_jump('您访问的页面不存在或已被删除!', route('page404'));
  154. }
  155. return view('home.index.'.$post['template']);
  156. }
  157. //sitemap页面
  158. public function sitemap()
  159. {
  160. return view('home.index.sitemap');
  161. }
  162. //404页面
  163. public function page404()
  164. {
  165. return view('home.404');
  166. }
  167. //测试页面
  168. public function test()
  169. {
  170. return date("Y-m-d H:i:s",strtotime("2017-04"));
  171. }
  172. }