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.

125 lines
4.3 KiB

  1. <?php
  2. namespace App\Http\Controllers\Wap;
  3. use Illuminate\Support\Facades\DB;
  4. class ArticleController extends BaseController
  5. {
  6. public function __construct()
  7. {
  8. parent::__construct();
  9. }
  10. //列表页
  11. public function index($cat, $page = 0)
  12. {
  13. $pagenow = $page;
  14. if (empty($cat) || !preg_match('/[0-9]+/', $cat)) {
  15. return redirect()->route('page404');
  16. }
  17. if (cache("catid$cat")) {
  18. $post = cache("catid$cat");
  19. } else {
  20. $post = object_to_array(DB::table('arctype')->where('id', $cat)->first(), 1);
  21. if (empty($post)) {
  22. return redirect()->route('page404');
  23. }
  24. cache(["catid$cat" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  25. }
  26. $data['post'] = $post;
  27. $subcat = "";
  28. $sql = "";
  29. $post2 = object_to_array(DB::table('arctype')->select('id')->where('pid', $cat)->get());
  30. if (!empty($post2)) {
  31. foreach ($post2 as $row) {
  32. $subcat = $subcat . "typeid=" . $row["id"] . " or ";
  33. }
  34. }
  35. $subcat = $subcat . "typeid=" . $cat;
  36. $sql = $subcat . " or typeid2 in (" . $cat . ")";//echo $subcat2;exit;
  37. $data['sql'] = $sql;
  38. $counts = DB::table("article")->whereRaw($sql)->count();
  39. if ($counts > sysconfig('CMS_MAXARC')) {
  40. $counts = sysconfig('CMS_MAXARC');
  41. dd($counts);
  42. }
  43. $pagesize = sysconfig('CMS_PAGESIZE');
  44. $page = 0;
  45. if ($counts % $pagesize) {//取总数据量除以每页数的余数
  46. $pages = intval($counts / $pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  47. } else {
  48. $pages = $counts / $pagesize;
  49. }
  50. if (!empty($pagenow)) {
  51. if ($pagenow == 1 || $pagenow > $pages) {
  52. return redirect()->route('page404');
  53. }
  54. $page = $pagenow - 1;
  55. $nextpage = $pagenow + 1;
  56. $previouspage = $pagenow - 1;
  57. } else {
  58. $page = 0;
  59. $nextpage = 2;
  60. $previouspage = 0;
  61. }
  62. $data['page'] = $page;
  63. $data['pages'] = $pages;
  64. $data['counts'] = $counts;
  65. $start = $page * $pagesize;
  66. $data['posts'] = arclist(array("sql" => $sql, "limit" => "$start,$pagesize")); //获取列表
  67. $data['pagenav'] = get_listnav(array("counts" => $counts, "pagesize" => $pagesize, "pagenow" => $page + 1, "catid" => $cat)); //获取分页列表
  68. if ($post['templist'] == 'category2') {
  69. if (!empty($pagenow)) {
  70. return redirect()->route('page404');
  71. }
  72. }
  73. return view('wap.article.index', $data);
  74. }
  75. //文章详情页
  76. public function detail($id)
  77. {
  78. if (empty($id) || !preg_match('/[0-9]+/', $id)) {
  79. return redirect()->route('page404');
  80. }
  81. if (cache("detailid$id")) {
  82. $post = cache("detailid$id");
  83. } else {
  84. $post = object_to_array(DB::table('article')->where('id', $id)->first(), 1);
  85. if (empty($post)) {
  86. return redirect()->route('page404');
  87. }
  88. $post['name'] = DB::table('arctype')->where('id', $post['typeid'])->value('name');
  89. cache(["detailid$id" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  90. }
  91. if ($post) {
  92. $cat = $post['typeid'];
  93. $post['body'] = ReplaceKeyword($post['body']);
  94. if (!empty($post['writer'])) {
  95. $post['writertitle'] = $post['title'] . ' ' . $post['writer'];
  96. }
  97. $data['post'] = $post;
  98. $data['pre'] = get_article_prenext(array('aid' => $post["id"], 'typeid' => $post["typeid"], 'type' => "pre"));
  99. } else {
  100. return redirect()->route('page404');
  101. }
  102. if (cache("catid$cat")) {
  103. $post = cache("catid$cat");
  104. } else {
  105. $post = object_to_array(DB::table('arctype')->where('id', $cat)->first(), 1);
  106. cache(["catid$cat" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  107. }
  108. return view('wap.article.detail', $data);
  109. }
  110. }