自动生成百度小程序sitemap.txt文件路径

    因为业务需要,需要在目前项目上开发一个百度小程序,百度智能小程序上线了,但是内容每天得推送,不可能一个小程序路径一个推送吧,因为小程序路径和项目路径不一致。

因为项目是用ThinkPHP开发的,在此附上自动输出sitemap.txt代码

   

<?php
namespace app\index\controller;
use think\Request;
use think\Response;
use mip\Mip;
class Index extends Mip
{
    public function index()
    {
        if ($this->domainSettingsInfo && $this->domainSettingsInfo['diySiteName']) {
            $this->assign('mipTitle',$this->domainSettingsInfo['diySiteName']);
        } else {
            $this->assign('mipTitle',$this->mipInfo['siteName'].$this->mipInfo['indexTitle']);
        }
      
        return $this->mipView('index/index');
    }

   function sitemap() {
        $count = model('app\article\model\Articles')->getCount(0);
        $tagsCount = db('Tags')->count();
        $pageNum = ceil($count / 200)+1;
        $tagPageNum = ceil($tagsCount / 200)+1;
        $sitemap = '<?xml version="1.0" encoding="utf-8"?>';
        $sitemap .= '<sitemapindex>';
        for ($i=1; $i < $pageNum; $i++) {
        $sitemap .= '<sitemap>';
            $sitemap .= '<loc>' . $this->domain . '/xml/' . $i . '.xml' . '</loc>';
            $sitemap .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
        $sitemap .= '</sitemap>';
        }
        for ($i=1; $i < $tagPageNum; $i++) {
        $sitemap .= '<sitemap>';
            $sitemap .= '<loc>' . $this->domain . '/tagXml/' . $i . '.xml' . '</loc>';
            $sitemap .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
        $sitemap .= '</sitemap>';
        }
        $sitemap .= '</sitemapindex>';
        return Response::create($sitemap)->contentType('text/xml');;
    }
    
    function xml() {
        $page = input('param.id');
        $page = $page ? $page : 1;
        $itemList = model('app\article\model\Articles')->getItemPushList('', $page, 200, 'publish_time', 'desc');

        $xml = '<?xml version="1.0" encoding="utf-8"?>';
        $xml .= '<urlset>';
        if ($page == 1) {
            $xml .= '<url>';
            $xml .= '<loc>' . $this->domain . '/' . '</loc>';
            $xml .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
            $xml .= '<changefreq>daily</changefreq>';
            $xml .= '<priority>1.0</priority>';
            $xml .= '</url>';
            if ($this->itemCategoryList = model('app\article\model\Articles')->getCategory()) {
                foreach($this->itemCategoryList as $k => $v) {
                    $xml .= '<url>';
                    $xml .= '<loc>' . $v["url"] . '</loc>';
                    $xml .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
                    $xml .= '<changefreq>daily</changefreq>';
                    $xml .= '<priority>0.9</priority>';
                    $xml .= '</url>';
                }
            }
        }
        foreach($itemList as $k => $v) {
            $xml .= '<url>';
            $xml .= '<loc>' . $v["url"] . '</loc>';
            $xml .= '<lastmod>' . date("Y-m-d", $v["publish_time"]) . '</lastmod>';
            $xml .= '<changefreq>daily</changefreq>';
            $xml .= '<priority>0.6</priority>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        return Response::create($xml)->contentType('text/xml');;
    }


    function tagXml() {
        $page = input('param.id');
        $page = $page ? $page : 1;
        $xml = '<?xml version="1.0" encoding="utf-8"?>';
        $xml .= '<urlset>';
        $tagsList = db($this->tags)->page($page,200)->select();
        if ($tagsList) {
            foreach ($tagsList as $key => $val) {
                if ($val['url_name']) {
                    $tagsList[$key]['url'] = $this->domain . '/' . $this->mipInfo['tagModelUrl'] .'/' . $val['url_name'] . '/';
                } else {
                    $tagsList[$key]['url'] = $this->domain . '/' . $this->mipInfo['tagModelUrl'] .'/' . $val['id'] . '/';
                }
                $tagsList[$key]['time'] = $val['add_time'] ? date("Y-m-d", $val["add_time"]) : date("Y-m-d");
            }
            foreach ($tagsList as $key => $val) {
                $xml .= '<url>';
                $xml .= '<loc>' . $val["url"] . '</loc>';
                $xml .= '<lastmod>' . $tagsList[$key]['time'] . '</lastmod>';
                $xml .= '<changefreq>daily</changefreq>';
                $xml .= '<priority>0.9</priority>';
                $xml .= '</url>';
            }
        }
        $xml .= '</urlset>';
        return Response::create($xml)->contentType('text/xml');;
    }
    
     function baiduSitemapPc() {
        $count = model('app\article\model\Articles')->getCount(0);
        $pageNum = ceil($count / 200)+1;
        $sitemap = '<?xml version="1.0" encoding="utf-8"?>';
        $sitemap .= '<sitemapindex>';
        for ($i=1; $i < $pageNum; $i++) {
        $sitemap .= '<sitemap>';
            $sitemap .= '<loc>' . $this->domain . '/pcXml/' . $i . '.xml' . '</loc>';
            $sitemap .= '<lastmod>' . date("Y-m-d") . '</lastmod>';
        $sitemap .= '</sitemap>';
        }
        $sitemap .= '</sitemapindex>';
        return Response::create($sitemap)->contentType('text/xml');;
    }
    function pcXml() {
        $page = input('param.id');
        $page = $page ? $page : 1;
        $itemList = model('app\article\model\Articles')->getItemPushList('', $page, 200, 'publish_time', 'desc');

        $xml = '<?xml version="1.0" encoding="utf-8"?>';
        $xml .= '<urlset>';
        foreach($itemList as $k => $v) {
            $xml .= '<url>';
            $xml .= '<loc>' . $v["url"] . '</loc>';
            $xml .= '<lastmod>' . date("Y-m-d", $v["publish_time"]) . '</lastmod>';
            $xml .= '<changefreq>daily</changefreq>';
            $xml .= '<priority>0.6</priority>';
            $xml .= '<data>';
            $xml .= '<display>';
            $xml .= '<title>' . $v['title'] . '</title>';
            $xml .= '</display>';
            $xml .= '</data>';
            $xml .= '</url>';
        }
        $xml .= '</urlset>';
        return Response::create($xml)->contentType('text/xml');;
    }
  //百度小程序输出
   function badiuSmart() {
      $page = input('param.id');
        $page = $page ? $page : 1;
        $itemList = model('app\article\model\Articles')->getItemPushList('', $page, 3000, 'publish_time', 'desc');
        //添加小程序路径
        $smarturl="pages/articleDetail/articleDetail?uuid=";
        foreach($itemList as $k => $v) {
            $smart .=$smarturl. $v["uuid"]."\r\n";
        }
        return Response::create($smart)->contentType('text/html');;
    }
    
    
}

  

上一篇:javascript – 将XML解析为UL


下一篇:php – 迁移到“Sitemap协议0.9”