NewsController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\service\CategoryService;
  4. use app\index\service\JiamengService;
  5. use app\index\service\NewsCategoryService;
  6. use app\index\service\NewsService;
  7. use cmf\controller\HomeBaseController;
  8. use think\Db;
  9. class NewsController extends HomeBaseController
  10. {
  11. public function index()
  12. {
  13. $map['status'] = 1;
  14. $cate_list = NewsCategoryService::getCates(20);
  15. $cate_list = $cate_list? $cate_list->toArray() : [];
  16. $catinfo = array('id'=>0);
  17. //排行
  18. $paihang = JiamengService::getHotList(18);
  19. // 最新入驻
  20. $brand_jx = JiamengService::getNewList(12);
  21. $news1 = NewsService::getList(['level'=> 1], 18);
  22. $newsHot = NewsService::getHotList();
  23. $this->assign('news1',$news1);
  24. $this->assign('data',$news1? $news1->toArray(): ['total'=>0]);
  25. $this->assign('page',$news1->render());
  26. $this->assign('brand_jx',$brand_jx);
  27. $this->assign('hots', $newsHot);
  28. $this->assign('cate_list',$cate_list? array_chunk($cate_list, 6) : []);
  29. $this->assign('paihang',$paihang);
  30. $this->assign('catinfo',$catinfo);
  31. $this->assign('link_cate_name','news');
  32. return $this->fetch();
  33. }
  34. /**
  35. * 详情
  36. * @return mixed
  37. * @throws \think\Exception
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. public function show(){
  43. $param = $this->request->param();
  44. isset($param['id']) or $this->error('非法访问');
  45. $info = Db::name('news')->where('id',$param['id'])->where('status',1)->find();
  46. if(empty($info)){
  47. $this->error('未找到新闻');
  48. }
  49. $catinfo = Db::name('news_category')->where('id',$info['ncatid'])->find();
  50. $bcate = Db::name('category')->where('id',$info['catid'])->find();
  51. $info['catname'] = $bcate['catname'];
  52. //更新点击次数
  53. Db::name('news')->where('id',$param['id'])->setInc('hits',1);
  54. //内容
  55. $content = Db::name('news_data')->where('id',$param['id'])->find();
  56. $info['content'] = $content['content'];
  57. $map['status'] = 1;
  58. // 上一篇
  59. $lastInfo = Db::name('news')->field('id,title')->where($map)->where('id','lt',$param['id'])->order('id desc')->limit(1)->find();
  60. //下一篇
  61. $nextInfo = Db::name('news')->field('id,title')->where($map)->where('id','gt',$param['id'])->order('id')->limit(1)->find();
  62. $last = $lastInfo? 1 : 0;
  63. $next = $nextInfo? 1 : 0;
  64. // 最新入驻
  65. $brand_jx = JiamengService::getNewList(12);
  66. // 推荐品牌
  67. $touziarr = config('params.touziLevels');
  68. $jx_jm = Db::name('jiameng')
  69. ->field('id,title,logo,touzi_level,hits')
  70. ->whereNotIn('id',$info['id'])
  71. ->orderRaw('rand()')
  72. ->limit(10)
  73. ->select()
  74. ->each(function($item, $k) use ($touziarr){
  75. $touziLevel = isset($item['touzi_level']) ? $item['touzi_level'] : '-1';
  76. $item['touzi_level_name'] = isset($touziarr[$touziLevel]) ? $touziarr[$touziLevel] : '';
  77. return $item;
  78. });
  79. // 加盟资讯
  80. $jm_news = NewsService::getListByCate(1, 6);
  81. // 相关资讯
  82. $other_news = NewsService::getListByCate($info['catid'], 10);
  83. $keywords = [];
  84. if(isset($info['keywords']) && !empty($info['keywords'])){
  85. $keywords = explode(',', $info['keywords']);
  86. }
  87. $this->assign('lastInfo',$lastInfo);
  88. $this->assign('nextInfo',$nextInfo);
  89. $this->assign('last',$last);
  90. $this->assign('next',$next);
  91. $this->assign('catinfo',$catinfo);
  92. $this->assign('info',$info);
  93. $this->assign('brand_jx',$brand_jx);
  94. $this->assign('jx_jm',$jx_jm);
  95. $this->assign('jm_news',$jm_news);
  96. $this->assign('other_news',$other_news);
  97. $this->assign('keywords',$keywords);
  98. //seo
  99. $seo_title = $info['seo_title'];
  100. $seo_keywords = $info['seo_keywords'];;
  101. $seo_desc = mb_substr($info['content'],0,200,'utf8');
  102. $seo_desc = strip_tags($seo_desc);
  103. $this->assign('seo_title',$seo_title);
  104. $this->assign('seo_keywords',$seo_keywords);
  105. $this->assign('seo_desc',$seo_desc);
  106. return $this->fetch();
  107. }
  108. public function lists(){
  109. $catinfo = [];
  110. $param = $this->request->param();
  111. $map['status'] = 1;
  112. if(isset($param['catname'])){
  113. $catinfo = Db::name('news_category')->where('enname',$param['catname'])->find();
  114. if(empty($catinfo)){
  115. $this->error('未找到分类');
  116. }
  117. $catid = $catinfo['id'];
  118. $map['ncatid'] = $catid;
  119. }
  120. $kw = isset($param['kw'])? trim($param['kw']) : '';
  121. if($kw){
  122. $map['kw'] = $kw;
  123. }
  124. // 分类
  125. $cate_list = NewsCategoryService::getCates(20);
  126. $cate_list = $cate_list? $cate_list->toArray() : [];
  127. $lists = NewsService::getList($map, 10);
  128. //排行
  129. $paihang = JiamengService::getHotList(18);
  130. // 最新入驻
  131. $brand_jx = JiamengService::getNewList(12);
  132. $newsHot = NewsService::getHotList();
  133. $this->assign('news1',$lists);
  134. $this->assign('data',$lists? $lists->toArray(): ['total'=>0]);
  135. $this->assign('page',$lists->render());
  136. $this->assign('brand_jx',$brand_jx);
  137. $this->assign('hots', $newsHot);
  138. $this->assign('cate_list',$cate_list? array_chunk($cate_list, 6) : []);
  139. $this->assign('paihang',$paihang);
  140. $this->assign('catinfo',$catinfo);
  141. $this->assign('link_cate_name','news');
  142. //seo
  143. $seo_title = $catinfo['seo_title'];
  144. $seo_keywords = $catinfo['seo_keywords'];
  145. $seo_desc = $catinfo['seo_desc'];
  146. $this->assign('seo_title',$seo_title);
  147. $this->assign('seo_keywords',$seo_keywords);
  148. $this->assign('seo_desc',$seo_desc);
  149. //seo
  150. $seo_title = str_replace('|',$catinfo['catname'],$catinfo['seo_title']);
  151. $seo_keywords = str_replace('|',$catinfo['catname'],$catinfo['seo_keywords']);
  152. $seo_desc = str_replace('|',$catinfo['catname'],$catinfo['seo_desc']);
  153. $this->assign('seo_title',$seo_title);
  154. $this->assign('seo_keywords',$seo_keywords);
  155. $this->assign('seo_desc',$seo_desc);
  156. return $this->fetch('list');
  157. }
  158. public function message(){
  159. return $this->fetch();
  160. }
  161. public function complaint(){
  162. return $this->fetch();
  163. }
  164. }
  165. ?>