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.

290 lines
11 KiB

8 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 TestController extends CommonController
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. //首页
  12. public function index()
  13. {
  14. //Excel文件导出功能,如果出现文件名中文乱码,iconv('UTF-8', 'GBK', '学生成绩')
  15. /* $cellData = [
  16. ['学号','姓名','成绩'],
  17. ['10001','AAAAA','99'],
  18. ['10002','BBBBB','92'],
  19. ['10003','CCCCC','95'],
  20. ['10004','DDDDD','89'],
  21. ['10005','EEEEE','96'],
  22. ];
  23. \Excel::create('学生成绩',function($excel) use ($cellData){
  24. //第一个工作簿,score是工作簿的名称
  25. $excel->sheet('score', function($sheet) use ($cellData){
  26. $sheet->rows($cellData);
  27. });
  28. //第二个工作簿
  29. $excel->sheet('score', function($sheet) use ($cellData){
  30. $sheet->rows($cellData);
  31. });
  32. })->export('xls');
  33. //Excel文件导入功能
  34. $filePath = 'storage/'.iconv('UTF-8', 'GBK', '学生成绩').'.xls';
  35. \Excel::load($filePath, function($reader) {
  36. $reader = $reader->getSheet(0);
  37. $res = $reader->toArray();
  38. dd($res);
  39. }); */
  40. }
  41. //列表页
  42. public function category($cat, $page=0)
  43. {
  44. $pagenow = $page;
  45. if(empty($cat) || !preg_match('/[0-9]+/',$cat)){return redirect()->route('page404');}
  46. if(cache("catid$cat")){$post = cache("catid$cat");}else{$post = object_to_array(DB::table('arctype')->where('id', $cat)->first(), 1);if(empty($post)){return redirect()->route('page404');} cache(["catid$cat"=>$post], \Carbon\Carbon::now()->addMinutes(2592000));}
  47. $data['post'] = $post;
  48. $subcat="";$sql="";
  49. $post2 = object_to_array(DB::table('arctype')->select('id')->where('pid', $cat)->get());
  50. if(!empty($post2)){foreach($post2 as $row){$subcat=$subcat."typeid=".$row["id"]." or ";}}
  51. $subcat=$subcat."typeid=".$cat;
  52. $sql=$subcat." or typeid2 in (".$cat.")";//echo $subcat2;exit;
  53. $data['sql'] = $sql;
  54. $counts = DB::table("article")->whereRaw($sql)->count();
  55. if($counts>sysconfig('CMS_MAXARC')){$counts=sysconfig('CMS_MAXARC');dd($counts);}
  56. $pagesize = sysconfig('CMS_PAGESIZE');$page=0;
  57. if($counts % $pagesize){//取总数据量除以每页数的余数
  58. $pages = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  59. }else{$pages = $counts/$pagesize;}
  60. if(!empty($pagenow)){if($pagenow==1 || $pagenow>$pages){return redirect()->route('page404');}$page = $pagenow-1;$nextpage=$pagenow+1;$previouspage=$pagenow-1;}else{$page = 0;$nextpage=2;$previouspage=0;}
  61. $data['page'] = $page;
  62. $data['pages'] = $pages;
  63. $data['counts'] = $counts;
  64. $start = $page*$pagesize;
  65. $data['posts'] = arclist(array("sql"=>$sql, "limit"=>"$start,$pagesize")); //获取列表
  66. $data['pagenav'] = get_listnav(array("counts"=>$counts,"pagesize"=>$pagesize,"pagenow"=>$page+1,"catid"=>$cat)); //获取分页列表
  67. if($post['templist']=='category2'){if(!empty($pagenow)){return redirect()->route('page404');}}
  68. return view('home.index.'.$post['templist'], $data);
  69. }
  70. //文章详情页
  71. public function detail($id)
  72. {
  73. if(empty($id) || !preg_match('/[0-9]+/',$id)){return redirect()->route('page404');}
  74. if(cache("detailid$id")){$post = cache("detailid$id");}else{$post = object_to_array(DB::table('article')->where('id', $id)->first(), 1);if(empty($post)){return redirect()->route('page404');}$post['name'] = DB::table('arctype')->where('id', $post['typeid'])->value('name');cache(["detailid$id"=>$post], \Carbon\Carbon::now()->addMinutes(2592000));}
  75. if($post)
  76. {
  77. $cat = $post['typeid'];
  78. $post['body'] = ReplaceKeyword($post['body']);
  79. if(!empty($post['writer'])){$post['writertitle']=$post['title'].' '.$post['writer'];}
  80. $data['post'] = $post;
  81. $data['pre'] = get_article_prenext(array('aid'=>$post["id"],'typeid'=>$post["typeid"],'type'=>"pre"));
  82. }
  83. else
  84. {
  85. return redirect()->route('page404');
  86. }
  87. 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));}
  88. return view('home.index.'.$post['temparticle'], $data);
  89. }
  90. //标签详情页,共有3种显示方式,1正常列表,2列表显示文章,3显示描述
  91. public function tag($tag, $page=0)
  92. {
  93. $pagenow = $page;
  94. if(empty($tag) || !preg_match('/[0-9]+/',$tag)){return redirect()->route('page404');}
  95. $post = object_to_array(DB::table('tagindex')->where('id',$tag)->first(), 1);
  96. $data['post'] = $post;
  97. $counts=DB::table("taglist")->where('tid',$tag)->count('aid');
  98. if($counts>sysconfig('CMS_MAXARC')){$counts=sysconfig('CMS_MAXARC');}
  99. $pagesize=sysconfig('CMS_PAGESIZE');$page=0;
  100. if($counts % $pagesize){//取总数据量除以每页数的余数
  101. $pages = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  102. }else{$pages = $counts/$pagesize;}
  103. if(!empty($pagenow)){if($pagenow==1 || $pagenow>$pages){return redirect()->route('page404');}$page = $pagenow-1;$nextpage=$pagenow+1;$previouspage=$pagenow-1;}else{$page = 0;$nextpage=2;$previouspage=0;}
  104. $data['page'] = $page;
  105. $data['pages'] = $pages;
  106. $data['counts'] = $counts;
  107. $start=$page*$pagesize;
  108. $posts=object_to_array(DB::table("taglist")->where('tid',$tag)->orderBy('aid', 'desc')->skip($start)->take($pagesize)->get());
  109. foreach($posts as $row)
  110. {
  111. $aid[] = $row["aid"];
  112. }
  113. $aid = isset($aid)?implode(',',$aid):"";
  114. if($aid!="")
  115. {
  116. if($post['template']=='tag2')
  117. {
  118. $data['posts'] = arclist(array("sql"=>"id in ($aid)","orderby"=>['id', 'desc'],"row"=>"$pagesize","field"=>"title,body")); //获取列表
  119. }
  120. else
  121. {
  122. $data['posts'] = arclist(array("sql"=>"id in ($aid)","orderby"=>['id', 'desc'],"row"=>"$pagesize")); //获取列表
  123. }
  124. }
  125. else
  126. {
  127. $data['posts'] = ''; //获取列表
  128. }
  129. $data['pagenav'] = get_listnav(array("counts"=>$counts,"pagesize"=>$pagesize,"pagenow"=>$page+1,"catid"=>$tag,"urltype"=>"tag")); //获取分页列表
  130. if($post['template']=='tag2' || $post['template']=='tag3'){if(!empty($pagenow)){return redirect()->route('page404');}}
  131. return view('home.index.'.$post['template'], $data);
  132. }
  133. //标签页
  134. public function tags()
  135. {
  136. return view('home.index.tags');
  137. }
  138. //搜索页
  139. public function search($keyword)
  140. {
  141. if(empty($keyword))
  142. {
  143. echo '请输入正确的关键词';exit;
  144. }
  145. if(strstr($keyword,"&")) exit;
  146. $data['posts']= object_to_array(DB::table("article")->where("title", "like", "%$keyword%")->orderBy('id', 'desc')->take(30)->get());
  147. $data['keyword']= $keyword;
  148. return view('home.index.search', $data);
  149. }
  150. //单页面
  151. public function page($id)
  152. {
  153. $data = [];
  154. if(!empty($id) && preg_match('/[a-z0-9]+/',$id))
  155. {
  156. $map['filename']=$id;
  157. 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));}
  158. if($post)
  159. {
  160. $data['post'] = $post;
  161. }
  162. else
  163. {
  164. return redirect()->route('page404');
  165. }
  166. }
  167. else
  168. {
  169. return redirect()->route('page404');
  170. }
  171. $data['posts'] = object_to_array(DB::table('page')->orderBy(\DB::raw('rand()'))->take(5)->get());
  172. return view('home.index.'.$post['template'], $data);
  173. }
  174. //商品列表页
  175. public function productcat($cat, $page=0)
  176. {
  177. $pagenow = $page;
  178. if(empty($cat) || !preg_match('/[0-9]+/',$cat)){return redirect()->route('page404');}
  179. $post = object_to_array(DB::table('product_type')->where('id', $cat)->first(), 1);if(empty($post)){return redirect()->route('page404');}
  180. $data['post'] = $post;
  181. $subcat="";
  182. $post2 = object_to_array(DB::table('product_type')->select('id')->where('pid', $cat)->get());
  183. if(!empty($post2)){foreach($post2 as $row){$subcat=$subcat."typeid=".$row["id"]." or ";}}
  184. $subcat=$subcat."typeid=".$cat;
  185. $data['sql'] = $subcat;
  186. $counts = DB::table("product")->whereRaw($subcat)->count();
  187. if($counts>sysconfig('CMS_MAXARC')){$counts=sysconfig('CMS_MAXARC');dd($counts);}
  188. $pagesize = sysconfig('CMS_PAGESIZE');$page=0;
  189. if($counts % $pagesize){//取总数据量除以每页数的余数
  190. $pages = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  191. }else{$pages = $counts/$pagesize;}
  192. if(!empty($pagenow)){if($pagenow==1 || $pagenow>$pages){return redirect()->route('page404');}$page = $pagenow-1;$nextpage=$pagenow+1;$previouspage=$pagenow-1;}else{$page = 0;$nextpage=2;$previouspage=0;}
  193. $data['page'] = $page;
  194. $data['pages'] = $pages;
  195. $data['counts'] = $counts;
  196. $start = $page*$pagesize;
  197. $data['posts'] = arclist(array("table"=>"product","sql"=>$subcat, "limit"=>"$start,$pagesize")); //获取列表
  198. $data['pagenav'] = get_listnav(array("counts"=>$counts,"pagesize"=>$pagesize,"pagenow"=>$page+1,"catid"=>$cat,"urltype"=>"product")); //获取分页列表
  199. if($post['templist']=='category2'){if(!empty($pagenow)){return redirect()->route('page404');}}
  200. return view('home.index.'.$post['templist'], $data);
  201. }
  202. //商品详情页
  203. public function product($id)
  204. {
  205. if(empty($id) || !preg_match('/[0-9]+/',$id)){return redirect()->route('page404');}
  206. $post = object_to_array(DB::table('product')->where('id', $id)->first(), 1);if(empty($post)){return redirect()->route('page404');}$post['name'] = DB::table('product_type')->where('id', $post['typeid'])->value('name');
  207. if($post)
  208. {
  209. $cat = $post['typeid'];
  210. $post['body'] = ReplaceKeyword($post['body']);
  211. if(!empty($post['writer'])){$post['writertitle']=$post['title'].' '.$post['writer'];}
  212. $data['post'] = $post;
  213. $data['pre'] = get_article_prenext(array('aid'=>$post["id"],'typeid'=>$post["typeid"],'type'=>"pre"));
  214. }
  215. else
  216. {
  217. return redirect()->route('page404');
  218. }
  219. $post = object_to_array(DB::table('product_type')->where('id', $cat)->first(), 1);
  220. return view('home.index.'.$post['temparticle'], $data);
  221. }
  222. //sitemap页面
  223. public function sitemap()
  224. {
  225. return view('home.index.sitemap');
  226. }
  227. //404页面
  228. public function page404()
  229. {
  230. return view('home.404');
  231. }
  232. //测试页面
  233. public function test()
  234. {
  235. return date("Y-m-d H:i:s",strtotime("2017-04"));
  236. }
  237. }