NewsController.php 8.7 KB

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