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.

113 lines
4.1 KiB

  1. <?php
  2. namespace App\Http\Controllers\Home;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Http\Request;
  5. class ArticleController extends BaseController
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. //文章列表页
  12. public function index(Request $request)
  13. {
  14. $pagesize = 10;
  15. $offset = 0;
  16. //文章分类
  17. if ($request->input('typeid', null) != null) {
  18. $postdata = array(
  19. 'id' => $request->input('typeid')
  20. );
  21. $url = env('APP_API_URL') . "/arctype_detail";
  22. $arctype_detail = curl_request($url, $postdata, 'GET');
  23. $data['post'] = $arctype_detail['data'];
  24. }
  25. if (isset($_REQUEST['page'])) {
  26. $offset = ($_REQUEST['page'] - 1) * $pagesize;
  27. }
  28. //文章列表
  29. $postdata2 = array(
  30. 'limit' => $pagesize,
  31. 'offset' => $offset
  32. );
  33. if ($request->input('typeid', null) != null) {
  34. $postdata2['typeid'] = $request->input('typeid');
  35. }
  36. $url = env('APP_API_URL') . "/article_list";
  37. $res = curl_request($url, $postdata2, 'GET');
  38. $data['list'] = $res['data']['list'];
  39. $data['totalpage'] = ceil($res['data']['count'] / $pagesize);
  40. if (isset($_REQUEST['page_ajax']) && $_REQUEST['page_ajax'] == 1) {
  41. $html = '';
  42. if ($res['data']['list']) {
  43. foreach ($res['data']['list'] as $k => $v) {
  44. $html .= '<div class="list">';
  45. if (!empty($v['litpic'])) {
  46. $html .= '<a class="limg" href="' . get_front_url(array("id" => $v['id'], "catid" => $v['typeid'], "type" => 'content')) . '"><img alt="' . $v['title'] . '" src="' . $v['litpic'] . '"></a>';
  47. }
  48. $html .= '<strong class="tit"><a href="' . get_front_url(array("id" => $v['id'], "catid" => $v['typeid'], "type" => 'content')) . '">' . $v['title'] . '</a></strong><p>' . mb_strcut($v['description'], 0, 150, 'UTF-8') . '..</p>';
  49. $html .= '<div class="info"><span class="fl">';
  50. $taglist = taglist($v['id']);
  51. if ($taglist) {
  52. foreach ($taglist as $row) {
  53. $html .= '<a href="' . get_front_url(array("tagid" => $row['id'], "type" => 'tags')) . '">' . $row['tag'] . '</a>';
  54. }
  55. }
  56. $html .= '<em>' . date("m-d H:i", $v['pubdate']) . '</em></span><span class="fr"><em>' . $v['click'] . '</em>人阅读</span></div><div class="cl"></div></div>';
  57. }
  58. }
  59. exit(json_encode($html));
  60. }
  61. return view('home.article.index', $data);
  62. }
  63. //文章详情页
  64. public function detail($id)
  65. {
  66. if (empty($id) || !preg_match('/[0-9]+/', $id)) {
  67. return redirect()->route('page404');
  68. }
  69. $post = cache("detailid$id");
  70. if (!$post) {
  71. $post = object_to_array(DB::table('article')->where('id', $id)->first(), 1);
  72. if (empty($post)) {
  73. return redirect()->route('page404');
  74. }
  75. $post['name'] = DB::table('arctype')->where('id', $post['typeid'])->value('name');
  76. cache(["detailid$id" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  77. }
  78. if (!$post) {
  79. return redirect()->route('page404');
  80. }
  81. $cat = $post['typeid'];
  82. $post['body'] = ReplaceKeyword($post['body']);
  83. if (!empty($post['writer'])) {
  84. $post['writertitle'] = $post['title'] . ' ' . $post['writer'];
  85. }
  86. $data['post'] = $post;
  87. $data['pre'] = get_article_prenext(array('aid' => $post["id"], 'typeid' => $post["typeid"], 'type' => "pre"));
  88. $post = cache("catid$cat");
  89. if (!$post) {
  90. $post = object_to_array(DB::table('arctype')->where('id', $cat)->first(), 1);
  91. cache(["catid$cat" => $post], \Carbon\Carbon::now()->addMinutes(2592000));
  92. }
  93. return view('home.article.detail', $data);
  94. }
  95. }