Article.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Article extends BaseMall
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize(); // TODO: Change the autogenerated stub
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/article.lang.php');
  18. }
  19. /**
  20. * 文章列表显示页面
  21. */
  22. public function index()
  23. {
  24. $ac_id = intval(input('param.ac_id'));
  25. if (empty($ac_id)) {
  26. $this->error(lang('param_error')); //'缺少参数:文章类别编号'
  27. }
  28. /**
  29. * 得到导航ID
  30. */
  31. $nav_id = intval(input('param.nav_id'));
  32. View::assign('index_sign', $nav_id);
  33. /**
  34. * 根据类别编号获取文章类别信息
  35. */
  36. $articleclass_model = model('articleclass');
  37. $article_class = $articleclass_model->getOneArticleclass($ac_id);
  38. View::assign('class_name', $article_class['ac_name']);
  39. if (empty($article_class) || !is_array($article_class)) {
  40. $this->error(lang('article_article_class_not_exists')); //'该文章分类并不存在'
  41. }
  42. $default_count = 5; //定义最新文章列表显示文章的数量
  43. /**
  44. * 分类导航
  45. */
  46. $nav_link = array(
  47. array(
  48. 'title' => lang('homepage'), 'link' => HOME_SITE_URL
  49. ), array(
  50. 'title' => $article_class['ac_name']
  51. )
  52. );
  53. View::assign('nav_link_list', $nav_link);
  54. /**
  55. * 左侧分类导航
  56. */
  57. $articleclass_list = $articleclass_model->getArticleclassList(array());
  58. $tree = new \mall\Tree();
  59. $tree->setTree($articleclass_list, 'ac_id', 'ac_parent_id', 'ac_name');
  60. View::assign('sub_class_list', $tree->getArrayList());
  61. /**
  62. * 文章列表
  63. */
  64. $child_class_list = $articleclass_model->getChildClass($ac_id);
  65. $ac_ids = array();
  66. if (!empty($child_class_list) && is_array($child_class_list)) {
  67. foreach ($child_class_list as $v) {
  68. $ac_ids[] = $v['ac_id'];
  69. }
  70. }
  71. $ac_ids = implode(',', $ac_ids);
  72. $article_model = model('article');
  73. $condition = array();
  74. $condition[] = array('ac_id', 'in', $ac_ids);
  75. $condition[] = array('article_show', '=', '1');
  76. $article_list = $article_model->getArticleList($condition, 12);
  77. View::assign('article', $article_list);
  78. View::assign('show_page', $article_model->page_info->render());
  79. /**
  80. * 最新文章列表
  81. */
  82. $count = count($article_list);
  83. $new_article_list = array();
  84. if (!empty($article_list) && is_array($article_list)) {
  85. for ($i = 0; $i < ($count > $default_count ? $default_count : $count); $i++) {
  86. $new_article_list[] = $article_list[$i];
  87. }
  88. }
  89. View::assign('new_article_list', $new_article_list);
  90. //SEO 设置
  91. $seo = model('seo')->type('article')->param(array('article_class' => $article_class['ac_name']))->show();
  92. $this->_assign_seo($seo);
  93. return View::fetch($this->template_dir . 'article_list');
  94. }
  95. /**
  96. * 单篇文章显示页面
  97. */
  98. public function show()
  99. {
  100. $article_id = intval(input('param.article_id'));
  101. if (empty($article_id)) {
  102. $this->error(lang('param_error')); //'缺少参数:文章编号'
  103. }
  104. /**
  105. * 根据文章编号获取文章信息
  106. */
  107. $article_model = model('article');
  108. $condition = array();
  109. $condition[] = array('article_id', '=', $article_id);
  110. $article = $article_model->getOneArticle($condition);
  111. if (empty($article) || !is_array($article) || $article['article_show'] == '0') {
  112. $this->error(lang('article_show_not_exists')); //'该文章并不存在'
  113. }
  114. View::assign('article', $article);
  115. /**
  116. * 根据类别编号获取文章类别信息
  117. */
  118. $articleclass_model = model('articleclass');
  119. $article_class = $articleclass_model->getOneArticleclass($article['ac_id']);
  120. if (empty($article_class) || !is_array($article_class)) {
  121. $this->error(lang('article_show_delete')); //'该文章已随所属类别被删除'
  122. }
  123. $default_count = 5; //定义最新文章列表显示文章的数量
  124. /**
  125. * 分类导航
  126. */
  127. $nav_link = array(
  128. array(
  129. 'title' => lang('homepage'), 'link' => HOME_SITE_URL
  130. ), array(
  131. 'title' => $article_class['ac_name'],
  132. 'link' => (string)url('Article/index', array('ac_id' => $article_class['ac_id']))
  133. ), array(
  134. 'title' => lang('article_show_article_content')
  135. )
  136. );
  137. View::assign('nav_link_list', $nav_link);
  138. /**
  139. * 左侧分类导航
  140. */
  141. $articleclass_list = $articleclass_model->getArticleclassList(array());
  142. $tree = new \mall\Tree();
  143. $tree->setTree($articleclass_list, 'ac_id', 'ac_parent_id', 'ac_name');
  144. View::assign('sub_class_list', $tree->getArrayList());
  145. /**
  146. * 文章列表
  147. */
  148. $child_class_list = $articleclass_model->getChildClass($article_class['ac_id']);
  149. $ac_ids = array();
  150. if (!empty($child_class_list) && is_array($child_class_list)) {
  151. foreach ($child_class_list as $v) {
  152. $ac_ids[] = $v['ac_id'];
  153. }
  154. }
  155. $ac_ids = implode(',', $ac_ids);
  156. $article_model = model('article');
  157. $condition = array();
  158. $condition[] = array('ac_id', 'in', $ac_ids);
  159. $condition[] = array('article_show', '=', '1');
  160. $article_list = $article_model->getArticleList($condition);
  161. /**
  162. * 寻找上一篇与下一篇
  163. */
  164. $pre_article = $next_article = array();
  165. if (!empty($article_list) && is_array($article_list)) {
  166. $pos = 0;
  167. foreach ($article_list as $k => $v) {
  168. if ($v['article_id'] == $article['article_id']) {
  169. $pos = $k;
  170. break;
  171. }
  172. }
  173. if ($pos > 0 && is_array($article_list[$pos - 1])) {
  174. $pre_article = $article_list[$pos - 1];
  175. }
  176. if ($pos < count($article_list) - 1 and is_array($article_list[$pos + 1])) {
  177. $next_article = $article_list[$pos + 1];
  178. }
  179. }
  180. View::assign('pre_article', $pre_article);
  181. View::assign('next_article', $next_article);
  182. /**
  183. * 最新文章列表
  184. */
  185. $count = count($article_list);
  186. $new_article_list = array();
  187. if (!empty($article_list) && is_array($article_list)) {
  188. for ($i = 0; $i < ($count > $default_count ? $default_count : $count); $i++) {
  189. $new_article_list[] = $article_list[$i];
  190. }
  191. }
  192. View::assign('new_article_list', $new_article_list);
  193. $seo_param = array();
  194. $seo_param['name'] = $article['article_title'];
  195. $seo_param['article_class'] = $article_class['ac_name'];
  196. $seo_param['description'] = ds_substing(htmlspecialchars_decode($article['article_content']));
  197. //SEO 设置
  198. $seo = model('seo')->type('article_content')->param($seo_param)->show();
  199. $this->_assign_seo($seo);
  200. return View::fetch($this->template_dir . 'article_show');
  201. }
  202. }