SiteMap.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. class SiteMap {
  3. public $xmlBegin = '<?xml version="1.0" encoding="UTF-8"?>
  4. <?xml-stylesheet type="text/xsl" href="sitemap.xsl"?>
  5. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  6. public $xmlEnd = '</urlset>';
  7. public $xmlData = '';
  8. public $txtData = '';
  9. public $defaultDate;
  10. public $defaultChangeFreq;
  11. public $defaultPriority;
  12. /**
  13. * @var int 官网类型 与 Article表use_type 同步 0 乐外卖官网 1 逐趣同城 2 逐趣校园
  14. */
  15. private $type;
  16. public function __construct($type)
  17. {
  18. $this->type = $type;
  19. $this->defaultDate = date("Y-m-d", strtotime("-1 days"));
  20. $this->defaultChangeFreq = 'daily';
  21. $this->defaultPriority = 0.6;
  22. }
  23. /**
  24. * 生成 sitemap.xml 内容
  25. *
  26. * @param $data
  27. *
  28. * @author lizhi <1458705589@qq.com>
  29. * @date 2021/7/29
  30. */
  31. public function generateXml($data = [])
  32. {
  33. $dataXml = '';
  34. foreach ($data as $datum) {
  35. if (!isset($datum['url']) || empty($datum['url'])) {
  36. continue;
  37. }
  38. $url = $datum['url'];
  39. $time = isset($datum['time']) ? $datum['time'] : $this->defaultDate;
  40. $changeFreq = isset($datum['changefreq']) ? $datum['changefreq'] : $this->defaultChangeFreq;
  41. $priority = isset($datum['priority']) ? $datum['priority'] : $this->defaultPriority;
  42. $dataXml.=<<<EOF
  43. <url>
  44. <loc>{$url}</loc>
  45. <mobile:mobile type="htmladapt"/>
  46. <lastmod>{$time}</lastmod>
  47. <changefreq>{$changeFreq}</changefreq>
  48. <priority>{$priority}</priority>
  49. </url>
  50. EOF;
  51. }
  52. $this->xmlData = "{$this->xmlBegin}
  53. {$dataXml}
  54. {$this->xmlEnd}";
  55. }
  56. /**
  57. * 生成 sitemap.txt 内容
  58. *
  59. * @param $data
  60. *
  61. * @author lizhi <1458705589@qq.com>
  62. * @date 2021/7/29
  63. */
  64. public function generateTxt($data)
  65. {
  66. $dataText = '';
  67. foreach ($data as $datum) {
  68. if (!isset($datum['url']) || empty($datum['url'])) {
  69. continue;
  70. }
  71. $url = $datum['url'];
  72. $dataText.= $url . "\n";
  73. }
  74. $this->txtData = "{$dataText}";
  75. }
  76. /**
  77. * 网页下载 sitemap.xml
  78. *
  79. * @author lizhi <1458705589@qq.com>
  80. * @date 2021/7/29
  81. */
  82. public function downLoadXml()
  83. {
  84. if (empty($this->xmlData)) {
  85. throw new \Exception("empty xml data");
  86. }
  87. header("Content-Type:text/xml charset=UTF-8");
  88. header("Content-Disposition:attachment; filename=sitemap.xml");
  89. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  90. header('Expires:0');
  91. header('Pragma:public');
  92. $fp = fopen('php://output', 'a');
  93. fwrite($fp, $this->xmlData);
  94. fclose($fp);
  95. die();
  96. }
  97. /**
  98. * 网页下载 sitemap.txt
  99. *
  100. * @author lizhi <1458705589@qq.com>
  101. * @date 2021/7/29
  102. */
  103. public function downLoadTxt()
  104. {
  105. if (empty($this->txtData)) {
  106. throw new \Exception("empty txt data");
  107. }
  108. header("Content-Type:text/text charset=UTF-8");
  109. header("Content-Disposition:attachment; filename=sitemap.txt");
  110. header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
  111. header('Expires:0');
  112. header('Pragma:public');
  113. $fp = fopen('php://output', 'a');
  114. fwrite($fp, $this->txtData);
  115. fclose($fp);
  116. die();
  117. }
  118. public function getXmlDate()
  119. {
  120. return $this->xmlData;
  121. }
  122. public function getTxtDate()
  123. {
  124. return $this->txtData;
  125. }
  126. public function syncTxtToOffice()
  127. {
  128. if (empty($this->txtData)) {
  129. return "生成数据错误";
  130. }
  131. $urlList = [
  132. Article::TYPE_ZHUQU_CITY => 'https://www.zhuqutongcheng.com/site/UpdateSiteMapTxt',
  133. Article::TYPE_ZHUQU_SCHOOL => 'https://www.zhuquxiaoyuan.com/site/UpdateSiteMapTxt',
  134. Article::TYPE_LEWAIMAI => 'https://www.lewaimai.com/site/UpdateSiteMapTxt',
  135. ];
  136. if (!Helper::isProductEnv()) {
  137. // 测试环境
  138. $urlList = [
  139. Article::TYPE_ZHUQU_CITY => 'http://tc.lewaimai.com/site/UpdateSiteMapTxt',
  140. Article::TYPE_ZHUQU_SCHOOL => 'http://xy.lewaimai.com/site/UpdateSiteMapTxt',
  141. Article::TYPE_LEWAIMAI => 'http://office.lewaimai.com/site/UpdateSiteMapTxt',
  142. ];
  143. }
  144. if (!isset($urlList[$this->type])) {
  145. return '类型错误';
  146. }
  147. $gzData = gzcompress($this->txtData);
  148. return LewaimaiHttp::POST($urlList[$this->type], [
  149. 'data' => $gzData,
  150. 'sign' => Helper::getSign($gzData)
  151. ]);
  152. }
  153. public function syncXmlToOffice()
  154. {
  155. if (empty($this->xmlData)) {
  156. return "生成数据错误";
  157. }
  158. $urlList = [
  159. Article::TYPE_ZHUQU_CITY => 'https://www.zhuqutongcheng.com/site/UpdateSiteMapXml',
  160. Article::TYPE_ZHUQU_SCHOOL => 'https://www.zhuquxiaoyuan.com/site/UpdateSiteMapXml',
  161. Article::TYPE_LEWAIMAI => 'https://www.lewaimai.com/site/UpdateSiteMapXml',
  162. ];
  163. if (!Helper::isProductEnv()) {
  164. // 测试环境
  165. $urlList = [
  166. Article::TYPE_ZHUQU_CITY => 'http://tc.lewaimai.com/site/UpdateSiteMapXml',
  167. Article::TYPE_ZHUQU_SCHOOL => 'http://xy.lewaimai.com/site/UpdateSiteMapXml',
  168. Article::TYPE_LEWAIMAI => 'http://office.lewaimai.com/site/UpdateSiteMapXml',
  169. ];
  170. }
  171. if (!isset($urlList[$this->type])) {
  172. return '类型错误';
  173. }
  174. $gzData = gzcompress($this->xmlData);
  175. return LewaimaiHttp::POST($urlList[$this->type], [
  176. 'data' => $gzData,
  177. 'sign' => Helper::getSign($gzData)
  178. ]);
  179. }
  180. }