SitemapController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // +----------------------------------------------------------------------
  4. // | Author: liuyong <707024896@qq.com>
  5. // +----------------------------------------------------------------------
  6. namespace app\admin\controller;
  7. use cmf\controller\AdminBaseController;
  8. use think\Db;
  9. class SitemapController extends AdminBaseController
  10. {
  11. //pc
  12. public function index()
  13. {
  14. $local = $_SERVER['SERVER_NAME'];
  15. $site = 'http://'.$local;
  16. $time = date("Y-m-d",time());
  17. $url = $site."/\r\n";
  18. $xml = '<?xml version="1.0" encoding="utf-8"?><urlset>';
  19. $xml .= "<url>";
  20. $xml .= "<loc>{$site}</loc>";
  21. $xml .= "<lastmod>{$time}</lastmod> ";
  22. $xml .= "<changefreq>daily</changefreq>";
  23. $xml .= "<priority>1.0</priority>";
  24. $xml .= "</url>";
  25. $category = Db::name('category')->where('status', 1)->order('list_order')->select();
  26. foreach($category as $val)
  27. {
  28. $url .= $site."/xm".$val['enname']."\r\n";
  29. // $html .= '<a href="'.$site.'/'.$geturl['url'].'.html">'.$val['name'].'</a>'."<br>";
  30. $xml .="<url>";
  31. $xml .= '<loc>'.$site.'/xm'.$val['enname'].'/</loc>';
  32. $xml .= "<changefreq>daily</changefreq>";
  33. $xml .= "<priority>0.9</priority>";
  34. $xml .= "</url>";
  35. }
  36. // $article = Db::name('news')->alias('a')->join('news_category b','a.catid=b.id')->where('a.status',1)->order('a.id desc')->select();
  37. $article = Db::name('news')->field('id,create_time')->where('status',1)->order('id desc')->select();
  38. foreach($article as $value)
  39. {
  40. //dump($value);
  41. $url .= $site."/news".$value['id']."/\r\n";
  42. $xml .= "<url>";
  43. //$html .= '<a href="'.$site.'/article/'.$value['id'].'.html">'.$value['post_title'].'</a>'."<br>";
  44. $xml .= '<loc>'.$site."/news".$value['id'].'/</loc>';
  45. $xml .= "<lastmod>".date('Y-m-d',$value['create_time'])."</lastmod>";
  46. $xml .= "<changefreq>weekly</changefreq>";
  47. $xml .= "<priority>0.8</priority>";
  48. $xml .= "</url>";
  49. }
  50. $xiangmu = Db::name('jiameng')->field('id,create_time')->order('id desc')->select();
  51. foreach($xiangmu as $pvalue)
  52. {
  53. $portalArticleUrl = trim($site."/b".$pvalue['id']."/"."\r\n");
  54. $xml .= "<url>";
  55. $xml .= '<loc>'.$portalArticleUrl.'</loc>';
  56. $xml .= "<lastmod>".date('Y-m-d',$pvalue['create_time'])."</lastmod>";
  57. $xml .= "<changefreq>weekly</changefreq>";
  58. $xml .= "<priority>0.8</priority>";
  59. $xml .= "</url>";
  60. }
  61. $xml .= '</urlset>';
  62. $this->updatexml($xml);
  63. }
  64. //shouji
  65. public function sitePhoneMap()
  66. {
  67. $local = $_SERVER['SERVER_NAME'];
  68. $local = explode('.', $local);
  69. //dump($local);
  70. if(count($local)==3){
  71. $local = $local[1].'.'.$local[2];
  72. }else{
  73. $local = $local[1].'.'.$local[2].'.'.$local[3];
  74. }
  75. $local = 'm.'.$local;
  76. //dump($local);die;
  77. $site = 'http://'.$local;
  78. $time = date("Y-m-d",time());
  79. $url = $site."/\r\n";
  80. $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  81. xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
  82. $xml .= "<url>";
  83. $xml .= "<loc>{$site}</loc>";
  84. $xml .= "<mobile:mobile/>";
  85. $xml .= "<lastmod>{$time}</lastmod> ";
  86. $xml .= "<changefreq>daily</changefreq>";
  87. $xml .= "<priority>1.0</priority>";
  88. $xml .= "</url>";
  89. $brandCategoryModel = new BrandCategoryModel();
  90. $category = $brandCategoryModel->where('status', 1)->where('delete_time', 0)->select();
  91. foreach($category as $val)
  92. {
  93. $geturl = $this->geturl('brand/List/index?id='.$val['id'].'');
  94. $url .= $site."/".$geturl['url']."\r\n";
  95. // $html .= '<a href="'.$site.'/'.$geturl['url'].'.html">'.$val['name'].'</a>'."<br>";
  96. $xml .="<url>";
  97. $xml .= '<loc>'.$site.'/'.$geturl['url'].'</loc>';
  98. $xml .= "<mobile:mobile/>";
  99. $xml .= "<changefreq>daily</changefreq>";
  100. $xml .= "<priority>0.9</priority>";
  101. $xml .= "</url>";
  102. }
  103. foreach($article as $value)
  104. {
  105. //dump($value);
  106. $url .= $site."/".$value['alias']."/".$value['id'].".html"."\r\n";
  107. $xml .= "<url>";
  108. //$html .= '<a href="'.$site.'/article/'.$value['id'].'.html">'.$value['post_title'].'</a>'."<br>";
  109. $xml .= '<loc>'.$site."/".$value['alias']."/".$value['id'].'.html</loc>';
  110. $xml .= "<mobile:mobile/>";
  111. $xml .= "<lastmod>".date('Y-m-d',$value['create_time'])."</lastmod>";
  112. $xml .= "<changefreq>weekly</changefreq>";
  113. $xml .= "<priority>0.8</priority>";
  114. $xml .= "</url>";
  115. }
  116. //品牌
  117. $portalCategoryModel = new PortalCategoryModel();
  118. $portalCategory = $portalCategoryModel->where('status', 1)->where('delete_time', 0)->select();
  119. foreach($portalCategory as $val)
  120. {
  121. $getPortalUrl = 'xmk/cid/'.$val['id'];
  122. $url .= $site."/".$getPortalUrl."\r\n";
  123. // $html .= '<a href="'.$site.'/'.$geturl['url'].'.html">'.$val['name'].'</a>'."<br>";
  124. $xml .="<url>";
  125. $xml .= '<loc>'.$site.'/'.$getPortalUrl.'</loc>';
  126. $xml .= "<mobile:mobile/>";
  127. $xml .= "<changefreq>daily</changefreq>";
  128. $xml .= "<priority>0.9</priority>";
  129. $xml .= "</url>";
  130. }
  131. $portalPostModel = new PortalPostModel();
  132. $where = [
  133. 'a.delete_time' => 0,
  134. 'a.post_status' => 1
  135. ];
  136. $field = 'a.id,a.create_time,b.category_id,c.alias';
  137. $article = $portalPostModel->alias('a')->field($field)
  138. ->join('portal_category_post b','a.id = b.post_id')
  139. ->join('portal_category c','b.category_id = c.id')
  140. ->where($where)
  141. ->order('a.create_time', 'DESC')
  142. ->select();
  143. foreach($article as $value)
  144. {
  145. //dump($value);
  146. $url .= $site."/".$value['alias']."/".$value['id'].".html"."\r\n";
  147. $xml .= "<url>";
  148. //$html .= '<a href="'.$site.'/article/'.$value['id'].'.html">'.$value['post_title'].'</a>'."<br>";
  149. $xml .= '<loc>'.$site."/".$value['alias']."/".$value['id'].'.html</loc>';
  150. $xml .= "<mobile:mobile/>";
  151. $xml .= "<lastmod>".date('Y-m-d',$value['create_time'])."</lastmod>";
  152. $xml .= "<changefreq>weekly</changefreq>";
  153. $xml .= "<priority>0.8</priority>";
  154. $xml .= "</url>";
  155. }
  156. $xml .= '</urlset>';
  157. $this->updatesjxml($xml);
  158. }
  159. //公共
  160. private function geturl($url)
  161. {
  162. // $result = Db::name('route')->where(["full_url"=>$url])->find();
  163. // return $result;
  164. return $url;
  165. }
  166. //pc
  167. private function updatexml($data)
  168. {
  169. $this->writefile('xml',$data);
  170. }
  171. private function writefile($type,$data)
  172. {
  173. //dump($_SERVER['DOCUMENT_ROOT']);die;
  174. $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/sitemap.".$type, "w") or die("Unable to open file!");
  175. fwrite($myfile, $data);
  176. fclose($myfile);
  177. echo 'pc xml地图已生成<br/>'.'<a href="http://'.$_SERVER["HTTP_HOST"].'/sitemap.xml'.'">点击查看</a>';
  178. }
  179. //手机
  180. private function updatesjxml($data)
  181. {
  182. $this->writesjfile('xml',$data);
  183. }
  184. private function writesjfile($type,$data)
  185. {
  186. //dump($_SERVER['DOCUMENT_ROOT']);die;
  187. $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/sitemaps.".$type, "w") or die("Unable to open file!");
  188. fwrite($myfile, $data);
  189. fclose($myfile);
  190. echo '手机 xml地图已生成<br/>'.'<a href="http://'.$_SERVER["HTTP_HOST"].'/sitemaps.xml'.'">点击查看</a>';
  191. }
  192. }