2 人参与回答
因为业务需要,需要在目前项目上开发一个百度小程序,百度智能小程序上线了,但是内容每天得推送,不可能一个小程序路径一个推送吧,因为小程序路径和项目路径不一致。
因为项目是用ThinkPHP开发的,在此附上自动输出Sitemap.txt代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | <?php use think\Request; use think\Response; 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' ]); } } $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="" 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( '' ); $page = $page ? $page : 1; $itemList = model( 'app\article\model\Articles' )->getItemPushList( '' , $page , 200, 'publish_time' , 'desc' ); $xml = '<?xml version="" 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>' ; $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>' ; $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>' ; $xml .= '</url>' ; } $xml .= '</urlset>' ; return Response::create( $xml )->contentType( 'text/xml' );; } function tagXml() { $page = input( '' ); $page = $page ? $page : 1; $xml = '<?xml version="" 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>' ; $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="" 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( '' ); $page = $page ? $page : 1; $itemList = model( 'app\article\model\Articles' )->getItemPushList( '' , $page , 200, 'publish_time' , 'desc' ); $xml = '<?xml version="" 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>' ; $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( '' ); $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' );; } } |