IndexController.php 11 KB

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