| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- // +----------------------------------------------------------------------
- // +----------------------------------------------------------------------
- // | Author: liuyong <707024896@qq.com>
- // +----------------------------------------------------------------------
- namespace app\admin\controller;
- use cmf\controller\AdminBaseController;
- use think\Db;
- class SitemapController extends AdminBaseController
- {
- //pc
- public function index()
- {
- $local = $_SERVER['SERVER_NAME'];
- $site = 'http://'.$local;
- $time = date("Y-m-d",time());
- $url = $site."/\r\n";
- $xml = '<?xml version="1.0" encoding="utf-8"?><urlset>';
- $xml .= "<url>";
- $xml .= "<loc>{$site}</loc>";
- $xml .= "<lastmod>{$time}</lastmod> ";
- $xml .= "<changefreq>daily</changefreq>";
- $xml .= "<priority>1.0</priority>";
- $xml .= "</url>";
-
- $category = Db::name('category')->where('status', 1)->order('list_order')->select();
-
- foreach($category as $val)
- {
-
- $url .= $site."/xm".$val['enname']."\r\n";
- // $html .= '<a href="'.$site.'/'.$geturl['url'].'.html">'.$val['name'].'</a>'."<br>";
- $xml .="<url>";
- $xml .= '<loc>'.$site.'/xm'.$val['enname'].'/</loc>';
- $xml .= "<changefreq>daily</changefreq>";
- $xml .= "<priority>0.9</priority>";
- $xml .= "</url>";
- }
-
- // $article = Db::name('news')->alias('a')->join('news_category b','a.catid=b.id')->where('a.status',1)->order('a.id desc')->select();
- $article = Db::name('news')->field('id,create_time')->where('status',1)->order('id desc')->select();
-
- foreach($article as $value)
- {
- //dump($value);
- $url .= $site."/news".$value['id']."/\r\n";
- $xml .= "<url>";
- //$html .= '<a href="'.$site.'/article/'.$value['id'].'.html">'.$value['post_title'].'</a>'."<br>";
- $xml .= '<loc>'.$site."/news".$value['id'].'/</loc>';
- $xml .= "<lastmod>".date('Y-m-d',$value['create_time'])."</lastmod>";
- $xml .= "<changefreq>weekly</changefreq>";
- $xml .= "<priority>0.8</priority>";
- $xml .= "</url>";
- }
-
-
-
- $xiangmu = Db::name('jiameng')->field('id,create_time')->order('id desc')->select();
- foreach($xiangmu as $pvalue)
- {
- $portalArticleUrl = trim($site."/b".$pvalue['id']."/"."\r\n");
- $xml .= "<url>";
- $xml .= '<loc>'.$portalArticleUrl.'</loc>';
- $xml .= "<lastmod>".date('Y-m-d',$pvalue['create_time'])."</lastmod>";
- $xml .= "<changefreq>weekly</changefreq>";
- $xml .= "<priority>0.8</priority>";
- $xml .= "</url>";
- }
-
- $xml .= '</urlset>';
-
- $this->updatexml($xml);
- }
- //shouji
- public function sitePhoneMap()
- {
- $local = $_SERVER['SERVER_NAME'];
- $local = explode('.', $local);
- //dump($local);
- if(count($local)==3){
- $local = $local[1].'.'.$local[2];
- }else{
- $local = $local[1].'.'.$local[2].'.'.$local[3];
- }
- $local = 'm.'.$local;
- //dump($local);die;
- $site = 'http://'.$local;
- $time = date("Y-m-d",time());
- $url = $site."/\r\n";
- $xml = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
- xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
- $xml .= "<url>";
- $xml .= "<loc>{$site}</loc>";
- $xml .= "<mobile:mobile/>";
- $xml .= "<lastmod>{$time}</lastmod> ";
- $xml .= "<changefreq>daily</changefreq>";
- $xml .= "<priority>1.0</priority>";
- $xml .= "</url>";
-
- $brandCategoryModel = new BrandCategoryModel();
- $category = $brandCategoryModel->where('status', 1)->where('delete_time', 0)->select();
- foreach($category as $val)
- {
- $geturl = $this->geturl('brand/List/index?id='.$val['id'].'');
-
- $url .= $site."/".$geturl['url']."\r\n";
- // $html .= '<a href="'.$site.'/'.$geturl['url'].'.html">'.$val['name'].'</a>'."<br>";
- $xml .="<url>";
- $xml .= '<loc>'.$site.'/'.$geturl['url'].'</loc>';
- $xml .= "<mobile:mobile/>";
- $xml .= "<changefreq>daily</changefreq>";
- $xml .= "<priority>0.9</priority>";
- $xml .= "</url>";
- }
-
- foreach($article as $value)
- {
- //dump($value);
- $url .= $site."/".$value['alias']."/".$value['id'].".html"."\r\n";
- $xml .= "<url>";
- //$html .= '<a href="'.$site.'/article/'.$value['id'].'.html">'.$value['post_title'].'</a>'."<br>";
- $xml .= '<loc>'.$site."/".$value['alias']."/".$value['id'].'.html</loc>';
- $xml .= "<mobile:mobile/>";
- $xml .= "<lastmod>".date('Y-m-d',$value['create_time'])."</lastmod>";
- $xml .= "<changefreq>weekly</changefreq>";
- $xml .= "<priority>0.8</priority>";
- $xml .= "</url>";
- }
-
- //品牌
- $portalCategoryModel = new PortalCategoryModel();
- $portalCategory = $portalCategoryModel->where('status', 1)->where('delete_time', 0)->select();
- foreach($portalCategory as $val)
- {
- $getPortalUrl = 'xmk/cid/'.$val['id'];
-
- $url .= $site."/".$getPortalUrl."\r\n";
- // $html .= '<a href="'.$site.'/'.$geturl['url'].'.html">'.$val['name'].'</a>'."<br>";
- $xml .="<url>";
- $xml .= '<loc>'.$site.'/'.$getPortalUrl.'</loc>';
- $xml .= "<mobile:mobile/>";
- $xml .= "<changefreq>daily</changefreq>";
- $xml .= "<priority>0.9</priority>";
- $xml .= "</url>";
- }
- $portalPostModel = new PortalPostModel();
-
- $where = [
- 'a.delete_time' => 0,
- 'a.post_status' => 1
- ];
- $field = 'a.id,a.create_time,b.category_id,c.alias';
- $article = $portalPostModel->alias('a')->field($field)
- ->join('portal_category_post b','a.id = b.post_id')
- ->join('portal_category c','b.category_id = c.id')
- ->where($where)
- ->order('a.create_time', 'DESC')
- ->select();
- foreach($article as $value)
- {
- //dump($value);
- $url .= $site."/".$value['alias']."/".$value['id'].".html"."\r\n";
- $xml .= "<url>";
- //$html .= '<a href="'.$site.'/article/'.$value['id'].'.html">'.$value['post_title'].'</a>'."<br>";
- $xml .= '<loc>'.$site."/".$value['alias']."/".$value['id'].'.html</loc>';
- $xml .= "<mobile:mobile/>";
- $xml .= "<lastmod>".date('Y-m-d',$value['create_time'])."</lastmod>";
- $xml .= "<changefreq>weekly</changefreq>";
- $xml .= "<priority>0.8</priority>";
- $xml .= "</url>";
- }
-
- $xml .= '</urlset>';
-
- $this->updatesjxml($xml);
- }
- //公共
- private function geturl($url)
- {
- // $result = Db::name('route')->where(["full_url"=>$url])->find();
- // return $result;
- return $url;
- }
- //pc
- private function updatexml($data)
- {
- $this->writefile('xml',$data);
- }
- private function writefile($type,$data)
- {
- //dump($_SERVER['DOCUMENT_ROOT']);die;
- $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/sitemap.".$type, "w") or die("Unable to open file!");
- fwrite($myfile, $data);
- fclose($myfile);
- echo 'pc xml地图已生成<br/>'.'<a href="http://'.$_SERVER["HTTP_HOST"].'/sitemap.xml'.'">点击查看</a>';
- }
- //手机
- private function updatesjxml($data)
- {
- $this->writesjfile('xml',$data);
- }
- private function writesjfile($type,$data)
- {
- //dump($_SERVER['DOCUMENT_ROOT']);die;
- $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/sitemaps.".$type, "w") or die("Unable to open file!");
- fwrite($myfile, $data);
- fclose($myfile);
- echo '手机 xml地图已生成<br/>'.'<a href="http://'.$_SERVER["HTTP_HOST"].'/sitemaps.xml'.'">点击查看</a>';
- }
- }
|