NewsController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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'].'_'.intval(cmf_is_mobile());
  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. $id=$param['id'];
  95. //相关动态和品牌动态 2020-08-24
  96. $about_news = Db::name('news')->field('id,title,thumb,create_time')->where(['status'=> 1])
  97. ->where(function($query) use ($id){
  98. $query->where('id','gt',$id);
  99. })
  100. ->order('list_order')->limit(6)->select();
  101. $brand_news = Db::name('news')->field('id,title,thumb,create_time')->where(['catid'=>$info['catid'],'status'=> 1])->order('id desc')->limit(6)->select();
  102. $keywords = [];
  103. if(isset($info['keywords']) && !empty($info['keywords'])){
  104. $keywords = explode(',', $info['keywords']);
  105. }
  106. $this->assign('lastInfo',$lastInfo);
  107. $this->assign('nextInfo',$nextInfo);
  108. $this->assign('last',$last);
  109. $this->assign('next',$next);
  110. $this->assign('catinfo',$catinfo);
  111. $this->assign('info',$info);
  112. $this->assign('brand_jx',$brand_jx);
  113. $this->assign('jx_jm',$jx_jm);
  114. $this->assign('jm_news',$jm_news);
  115. $this->assign('other_news',$other_news);
  116. $this->assign('keywords',$keywords);
  117. //seo
  118. $this->assign('about_news',$about_news);
  119. $this->assign('brand_news',$brand_news);
  120. $seo_title = $info['seo_title'];
  121. $seo_keywords = $info['seo_keywords'];;
  122. $seo_desc = strip_tags($info['content']);
  123. $seo_desc = mb_substr($seo_desc,0,200,'utf8');
  124. $this->assign('seo_title',$seo_title);
  125. //$this->assign('seo_keywords',$seo_keywords);
  126. $this->assign('seo_keywords',$info['keywords']); //2020/8/16
  127. $this->assign('seo_desc',$seo_desc);
  128. $this->assign('searchType', 3);
  129. $pageHtml = $this->fetch();
  130. RedisService::set($cacheKey, $pageHtml, 24*3600);
  131. return $pageHtml;
  132. }
  133. public function lists(){
  134. $catinfo = [];
  135. $param = $this->request->param();
  136. $cacheKey = 'cache:news_pages:list:' . md5(json_encode($param)).'_'.intval(cmf_is_mobile());
  137. $pageHtml = RedisService::get($cacheKey);
  138. if ($pageHtml) {
  139. return $pageHtml;
  140. }
  141. $map['status'] = 1;
  142. if(isset($param['catname'])){
  143. $catinfo = Db::name('news_category')->where('enname',$param['catname'])->find();
  144. if(empty($catinfo)){
  145. header("Location:".url('/404'));
  146. exit;
  147. }
  148. $catid = $catinfo['id'];
  149. $map['ncatid'] = $catid;
  150. }
  151. $kw = isset($param['kw'])? trim($param['kw']) : '';
  152. if($kw){
  153. $map['kw'] = $kw;
  154. }
  155. // 分类
  156. $cate_list = NewsCategoryService::getCates(20);
  157. $cate_list = $cate_list ? $cate_list : [];
  158. $lists = NewsService::getList($map, 10);
  159. //排行
  160. $paihang = NewsService::getRankList(cmf_is_mobile()? 5: 18);
  161. // $paihang = JiamengService::getHotList(18);
  162. // 最新入驻
  163. $brand_jx = JiamengService::getNewList(12);
  164. $newsHot = NewsService::getHotList();
  165. // 友情链接
  166. $links = LinkService::getList(['catname'=> 'news'], 50);
  167. $this->assign('news1',$lists);
  168. $this->assign('data',$lists? $lists->toArray(): ['total'=>0]);
  169. $this->assign('page',$lists->render());
  170. $this->assign('brand_jx',$brand_jx);
  171. $this->assign('hots', $newsHot);
  172. $this->assign('cate_list',$cate_list? array_chunk($cate_list, 6) : []);
  173. $this->assign('paihang',$paihang);
  174. $this->assign('catinfo',$catinfo);
  175. $this->assign('links',$links);
  176. //seo
  177. $seo_title = $catinfo['seo_title'];
  178. $seo_keywords = $catinfo['seo_keywords'];
  179. $seo_desc = $catinfo['seo_desc'];
  180. $this->assign('seo_title',$seo_title);
  181. $this->assign('seo_keywords',$seo_keywords);
  182. $this->assign('seo_desc',$seo_desc);
  183. //seo
  184. $seo_title = str_replace('|',$catinfo['catname'],$catinfo['seo_title']);
  185. $seo_keywords = str_replace('|',$catinfo['catname'],$catinfo['seo_keywords']);
  186. $seo_desc = str_replace('|',$catinfo['catname'],$catinfo['seo_desc']);
  187. $this->assign('seo_title',$seo_title);
  188. $this->assign('seo_keywords',$seo_keywords);
  189. $this->assign('seo_desc',$seo_desc);
  190. $this->assign('searchType', 3);
  191. $pageHtml = $this->fetch('list');
  192. RedisService::set($cacheKey, $pageHtml, 24*3600);
  193. return $pageHtml;
  194. }
  195. public function message(){
  196. return $this->fetch();
  197. }
  198. public function complaint(){
  199. return $this->fetch();
  200. }
  201. }
  202. ?>