NewsController.php 8.1 KB

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