IndexController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. namespace app\index\controller;
  3. use app\index\service\AdvertService;
  4. use app\index\service\CategoryService;
  5. use app\index\service\IpService;
  6. use app\index\service\JiamengService;
  7. use app\index\service\LinkService;
  8. use app\index\service\MessageService;
  9. use app\index\service\NewsCategoryService;
  10. use app\index\service\NewsService;
  11. use app\index\service\RedisService;
  12. use cmf\controller\HomeBaseController;
  13. use think\Db;
  14. class IndexController extends HomeBaseController
  15. {
  16. public function index()
  17. {
  18. $cacheKey = 'cache:index_pages:index_'.intval(cmf_is_mobile());
  19. $pageHtml = RedisService::get($cacheKey);
  20. if($pageHtml){
  21. //return $pageHtml;
  22. }
  23. // 热门分类
  24. $recCates = CategoryService::getRecCates(6);
  25. $recCates = $recCates? array_chunk($recCates, 3) : [];
  26. // 最新招商
  27. $brandNews = JiamengService::getNewList(10);
  28. /* 轮播广告 */
  29. //横幅广告位
  30. $banner = AdvertService::getListBySlide(1, 6);
  31. // 小图
  32. $bannerMin = AdvertService::getListBySlide(2, 5);
  33. // 特许加盟
  34. $banners = AdvertService::getListBySlides([3,4,5,6], 1);
  35. $banner1 = isset($banners[3])? $banners[3] : [];
  36. // 严选品牌上方广告
  37. $banner2 = isset($banners[4])? $banners[4] : [];
  38. // 品牌排行榜下方广告
  39. $banner3 = isset($banners[6])? $banners[6] : [];
  40. // 加盟动态下方广告
  41. $banner4 = isset($banners[5])? $banners[5] : [];
  42. /* 品牌栏位列表数据 */
  43. // 今日之星
  44. $brand1 = JiamengService::getListByLevel(1,3);
  45. // 匹配精选
  46. $brand2 = JiamengService::getListByLevel(2);
  47. // 品牌推荐
  48. $brand3 = JiamengService::getListByLevel(3);
  49. // 品牌严选
  50. $brand4 = JiamengService::getListByLevel(4, 21);
  51. // 专题品牌
  52. $brand6 = JiamengService::getListByLevel(6, 10);
  53. // 品牌聚焦
  54. $brand7 = JiamengService::getListByLevel(7, 56);
  55. $brand7 = $brand7? array_chunk($brand7, 28, true) : [];
  56. // 最新品牌
  57. $brand8 = JiamengService::getNewList(12);
  58. /* 人气排行榜1 */
  59. $phb1 = JiamengService::getTopList('touzi_level <= 2');
  60. $phb2 = JiamengService::getTopList('touzi_level = 3');
  61. $phb3 = JiamengService::getTopList('touzi_level >= 4');
  62. /* 新闻资讯 */
  63. // 分类1
  64. $cateNews1 = $cateNews2 = [];
  65. $newsCates = NewsCategoryService::getCates(6);
  66. $newsCates1 = NewsCategoryService::getCatesListByIds([17,18,19]);
  67. $newsCates2 = NewsCategoryService::getCatesListByIds([1,4,20]);
  68. if($newsCates1){
  69. foreach($newsCates1 as $k=>$v){
  70. $catid = isset($v['id'])? intval($v['id']): 0;
  71. if($catid){
  72. $cateNews1[] = NewsService::getListByCate($catid, 6);
  73. }
  74. }
  75. }
  76. if($newsCates2){
  77. foreach($newsCates2 as $k=>$v){
  78. $catid = isset($v['id'])? intval($v['id']): 0;
  79. if($catid){
  80. $cateNews2[] = NewsService::getListByCate($catid, 7);
  81. }
  82. }
  83. }
  84. // 热点新闻
  85. $recNews = NewsService::getNewList(6);
  86. // 加盟问答
  87. $answerList = Db::name('know')->whereIn('status',[1,2])
  88. ->field('id,title,description')
  89. ->order('create_time desc')
  90. ->limit(13)
  91. ->select();
  92. // 课堂列表
  93. $courseList = NewsService::getListByLevel(1, 8);
  94. // 参数
  95. $cateList = CategoryService::getCates();
  96. $touziarr = config('params.touziLevels');
  97. // 分类品牌
  98. $cateBrands = [];
  99. $hotCates = $cateList? array_slice($cateList, 0, 6) : [];
  100. if($hotCates){
  101. foreach ($hotCates as $k => $cate){
  102. $catid = isset($cate['id'])? $cate['id'] : '';
  103. if($catid){
  104. $dataList = JiamengService::getList(['pcid'=> $catid], '',16);
  105. $cateBrands[] = $dataList? $dataList : [];
  106. }
  107. }
  108. }
  109. // 热门品牌
  110. $brandHot = JiamengService::getTopList([], 16);
  111. // 加盟动态
  112. $jmMessages = NewsService::getRandList(10);
  113. // 友情链接
  114. $links = LinkService::getList(['catname'=> 'index'], 50);
  115. // 新增
  116. $this->assign('banner',$banner);
  117. $this->assign('banner1',$banner1);
  118. $this->assign('banner2',$banner2);
  119. $this->assign('banner3',$banner3);
  120. $this->assign('banner4',$banner4);
  121. $this->assign('bannerMin',$bannerMin);
  122. $this->assign('brandNews',$brandNews);
  123. $this->assign('cateList',$cateList);
  124. $this->assign('touziarr',$touziarr);
  125. $this->assign('hotCates',$hotCates);
  126. $this->assign('cateBrands',$cateBrands);
  127. $this->assign('brandHot',$brandHot);
  128. $this->assign('newsCates1',$newsCates1);
  129. $this->assign('newsCates2',$newsCates2);
  130. $this->assign('cateNews1',$cateNews1);
  131. $this->assign('cateNews2',$cateNews2);
  132. $this->assign('cateNews2',$cateNews2);
  133. $this->assign('newsCates',$newsCates);
  134. $this->assign('recNews',$recNews);
  135. $this->assign('answerList',$answerList);
  136. $this->assign('jmMessages',$jmMessages);
  137. $this->assign('courseList',$courseList);
  138. $this->assign('recCates',$recCates);
  139. $this->assign('links',$links);
  140. // 品牌
  141. $this->assign('brand1',$brand1);
  142. $this->assign('brand2',$brand2);
  143. $this->assign('brand3',$brand3);
  144. $this->assign('brand4',$brand4);
  145. // $this->assign('brand5',$brand5);
  146. $this->assign('brand6',$brand6);
  147. $this->assign('brand7',$brand7);
  148. $this->assign('brand8',$brand8);
  149. // 品牌排行榜
  150. $this->assign('phb1',$phb1);
  151. $this->assign('phb2',$phb2);
  152. $this->assign('phb3',$phb3);
  153. // end
  154. $pageHtml = $this->fetch();
  155. //var_dump($pageHtml);
  156. // 页面缓存
  157. $pageHtml = filterHtmlUrl($pageHtml);
  158. RedisService::set($cacheKey, $pageHtml , 7*24*3600);
  159. return $pageHtml;
  160. }
  161. /**
  162. * 投诉
  163. */
  164. public function complaint(){
  165. $param = $this->request->param();
  166. isset($param['mobile'])&&$param['mobile']!='' or $this->error('请填写手机号');
  167. $param['create_time'] = time();
  168. $param['ip'] = get_client_ip();
  169. $param['address'] = IpService::getAddress(get_client_ip());
  170. if(strlen($param['content'])>200){
  171. $this->error('字符长度不超过300个');
  172. }
  173. $param['content'] = strFilter($param['content']);
  174. $param['name'] = strFilter($param['name']);
  175. $param['mobile'] = strFilter($param['mobile']);
  176. $param['type'] = 2;
  177. //判断是否已超过10次 今天这个ip是否投诉过
  178. $sum = MessageService::getTodyCount(['ip'=> $param['ip'],'type'=> 2]);
  179. if($sum>0){
  180. $this->error('今日投诉已超过1次,明天再来吧!');
  181. }
  182. $res = Db::name('message')->insert($param);
  183. if($res){
  184. $this->success('投诉提交成功');
  185. }else{
  186. $this->error('投诉提交失败');
  187. }
  188. }
  189. /**
  190. * 网站地图
  191. * @return mixed
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @throws \think\exception\DbException
  195. */
  196. public function sitemap(){
  197. $page = input('p', 1);
  198. $cacheKey = 'cache:sitemap:list_';
  199. $datas = RedisService::get($cacheKey.$page);
  200. if(empty($datas) ){
  201. $dataList = CategoryService::getCates(1000, -1, "enname as id, catname as title, '1' as 'type'", 'create_time desc,list_order');
  202. $brands = JiamengService::getList(['status'=> 1], "j.id,j.title, '2' as 'type'", 3000);
  203. $brands = $brands? $brands->toArray() : [];
  204. $news = NewsService::getList(['status'=> 1], 3000,"id,title, '3' as 'type'");
  205. $news = $news? $news->toArray() : [];
  206. $dataList = isset($brands['data']) && $brands['data']? array_merge($dataList, $brands['data']) : $dataList;
  207. $dataList = isset($news['data']) && $news['data']? array_merge($dataList, $news['data']) : $dataList;
  208. // 生成XML文件
  209. if($dataList){
  210. $domain = request()->domain();
  211. $urls = [
  212. 0=> "<url><loc>".$domain."</loc><lastmod>".date('Y-m-d')."</lastmod><changefreq>daily</changefreq><priority>1.0</priority></url>",
  213. ];
  214. foreach ($dataList as $item){
  215. $type = isset($item['type'])? intval($item['type']) : 0;
  216. switch($type){
  217. case 1: $url = $domain.'/xm'.$item['id'].'/'; break;
  218. case 2: $url = $domain.'/b'.$item['id'].'/'; break;
  219. default: $url = $domain.'/news'.$item['id'].'/'; break;
  220. }
  221. $urls[] = "<url><loc>{$url}</loc><changefreq>daily</changefreq><priority>0.9</priority></url>";
  222. }
  223. if($urls){
  224. $strUrls = '<?xml version="1.0" encoding="utf-8"?><urlset>'.implode('',$urls).'</urlset>';
  225. file_put_contents('./sitemap.xml', $strUrls);
  226. }
  227. }
  228. $result = $dataList? array_chunk(array_values($dataList), 500) : [];
  229. if($result){
  230. foreach ($result as $k => $list){
  231. RedisService::set($cacheKey.($k+1), $list, 30*3600);
  232. }
  233. $datas = isset($result[$page-1])? $result[$page-1] : [];
  234. }
  235. }
  236. $nextPage = $page+1;
  237. $this->assign('datas', $datas);
  238. $this->assign('lastPage', $page-1>0? ($page-1) : 1);
  239. $this->assign('nextPage', RedisService::get($cacheKey.$nextPage)? $nextPage : 0);
  240. return $this->fetch('../sitemap');
  241. }
  242. public function clearCache(){
  243. // 清楚Redis缓存
  244. RedisService::delByKeys('cache:*');
  245. exit('清除成功');
  246. // end
  247. }
  248. }
  249. ?>