赛亿官网

Article.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\index\controller;
  3. use think\Db;
  4. use think\Request;
  5. use app\admin\model\Category as CategoryModel;
  6. use app\admin\model\Article as ArticleModel;
  7. use app\index\model\Tags as TagsModel;
  8. /**
  9. * 文章管理
  10. * @author huwhis@163.com
  11. * @version 0.0.1
  12. */
  13. class Article extends Base
  14. {
  15. protected $model;
  16. protected $category_model;
  17. public function __construct(Request $request = null)
  18. {
  19. parent::__construct($request);
  20. $this->model = new ArticleModel();
  21. $this->category_model = new CategoryModel();
  22. $this->assign('timeList', $this->model->createTimeArchive());
  23. }
  24. public function index($cid = 0)
  25. {
  26. if (!$cid) {
  27. throw new \think\exception\HttpException(404, '页面不存在');
  28. }
  29. $category_info = $this->category_model->field('id, pid, name, url, template')->find($cid);
  30. $data = $this->model->where(['is_del'=>0, 'cid'=>$cid])->field('id, cid, title, username, title_pic, summary, clicks, create_time')->order('id', 'desc')->paginate();
  31. $this->assign('category_info', $category_info);
  32. $this->assign('data', $data);
  33. $this->assign('cid', $cid);
  34. return $this->fetch($category_info->template);
  35. }
  36. public function detail($cid = 0, $id = 0)
  37. {
  38. if (!$cid || !$id) {
  39. throw new \think\exception\HttpException(404, '页面不存在');
  40. }
  41. $category_info = $this->category_model->field('id, pid, name, url, template')->where('is_del', 0)->find($cid);
  42. $this->assign('category_info', $category_info);
  43. $data = $this->model->field('id, cid, title, clicks, title_pic, keywords, summary, content, create_time')->find($id);
  44. $data['clicks'] += 1;
  45. @$data->save(['clicks'=>$data['clicks']], ['id'=>$id]);
  46. $this->assign('data', $data);
  47. // $next_prev = $this->getNextPrev($id, $cid);
  48. // $this->assign('next_prev', $next_prev);
  49. $hot = $this->hot($cid);
  50. $this->assign('hot', $hot);
  51. return $this->fetch();
  52. }
  53. /**
  54. * 获取文章上 or 下一篇
  55. */
  56. public function getNextPrev($id, $cid = null)
  57. {
  58. $data_p = null;
  59. $data_N = null;
  60. if ($cid) {
  61. $id_list = $this->model->where(['is_del' => 0, 'cid' => $cid])
  62. ->order(['sort' => 'desc', 'id' => 'desc'])
  63. ->column('id');
  64. if (count($id_list) > 1) {
  65. $key = array_search($id, $id_list);
  66. if ($key == 0) {
  67. $data_p = null;
  68. $N = $id_list[1];
  69. $data_N = $this->model->field('id,title')->find($N);
  70. } elseif ($key == count($id_list) - 1) {
  71. $P = $id_list[count($id_list) - 2];
  72. $data_p = $this->model->field('id,title')->find($P);
  73. $data_N = null;
  74. } else {
  75. $P = $id_list[$key - 1];
  76. $data_p = $this->model->field('id,title')->find($P);
  77. $N = $id_list[$key + 1];
  78. $data_N = $this->model->field('id,title')->find($N);
  79. }
  80. }
  81. }
  82. return ['prev' => $data_p, 'next' => $data_N];
  83. }
  84. /**
  85. * 感兴趣文章
  86. */
  87. public function hot($cid = 0)
  88. {
  89. $data = null;
  90. if ($cid) {
  91. $data = $this->model->where(['is_del' => 0, 'cid' => $cid])->field('id,cid,title,title_pic,create_time')->order('clicks','desc')->limit(3)->select();
  92. }
  93. return $data;
  94. }
  95. /**
  96. * 归档页面(时间)
  97. */
  98. public function archive($year=0,$month=0)
  99. {
  100. $time = mFristAndLast($year, $month);
  101. $data = Db::table('sur_article')->alias('a')->join('sur_category c','a.cid=c.id')
  102. ->where('a.create_time','>',$time['firstday'])
  103. ->where('a.create_time','<',$time['lastday'])
  104. ->field('a.id,a.cid,a.title,a.username,a.title_pic,a.summary,a.clicks,a.create_time,c.url,c.name as cname')
  105. ->order('id','desc')
  106. ->paginate();
  107. $this->assign('data', $data);
  108. $this->assign('year', $year);
  109. $this->assign('month', $month);
  110. return $this->fetch();
  111. }
  112. /**
  113. * 归档页面(发布者)
  114. */
  115. public function author($name = null)
  116. {
  117. if (!$name) {
  118. throw new \think\exception\HttpException(404, '页面不存在');
  119. }
  120. $data = Db::table('sur_article')->alias('a')->join('sur_category c','a.cid=c.id')
  121. ->where('a.username',$name)
  122. ->field('a.id,a.cid,a.title,a.username,a.title_pic,a.summary,a.clicks,a.create_time,c.url,c.name as cname')
  123. ->order('id','desc')
  124. ->paginate();
  125. $this->assign('data', $data);
  126. $this->assign('username', $name);
  127. return $this->fetch();
  128. }
  129. public function tags($name=null)
  130. {
  131. if (!$name) {
  132. throw new \think\exception\HttpException(404, '页面不存在');
  133. }
  134. $tagsModel = new TagsModel();
  135. $data = $tagsModel->where('tag', $name)->field('id, cid, title, username, title_pic, summary, clicks, create_time, url, cname')->order('id', 'desc')->paginate();
  136. $this->assign('data', $data);
  137. $this->assign('tag', $name);
  138. return $this->fetch();
  139. }
  140. }