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.

266 lines
11 KiB

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