IndexController.php 10 KB

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