Sitemap.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace addons\cms\controller;
  3. use think\Config;
  4. /**
  5. * Sitemap控制器
  6. * Class Sitemap
  7. * @package addons\cms\controller
  8. */
  9. class Sitemap extends Base
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $options = [
  13. 'item_key' => '',
  14. 'root_node' => 'urlset',
  15. 'item_node' => 'url',
  16. 'root_attr' => 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/"'
  17. ];
  18. //默认配置
  19. protected $config = [
  20. 'pagesize' => 50000,
  21. 'cache' => 3600
  22. ];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. Config::set('default_return_type', 'xml');
  27. }
  28. /**
  29. * Sitemap集合
  30. */
  31. public function index()
  32. {
  33. $pagesize = $this->request->param('pagesize/d', $this->config['pagesize']);
  34. $type = $this->request->param('type', '');
  35. $type = str_replace('.xml', '', $type);
  36. $list = [];
  37. $pagesizeStr = $pagesize != $this->config['pagesize'] ? "pagesize/{$pagesize}/" : '';
  38. if (!$type || $type == 'channel') {
  39. $path = "/addons/cms/sitemap/channels/{$pagesizeStr}page/[PAGE]";
  40. $channelsList = \addons\cms\model\Channel::where('status', 'normal')->field('id,name,diyname,createtime')->paginate($pagesize, false, ['path' => $path]);
  41. $lastPage = $channelsList->lastPage();
  42. foreach ($channelsList->getUrlRange(1, $lastPage) as $index => $item) {
  43. $list[] = ['loc' => url($item, '', 'xml', true)];
  44. }
  45. }
  46. if (!$type || $type == 'archives') {
  47. $path = "/addons/cms/sitemap/archives/{$pagesizeStr}page/[PAGE]";
  48. $archivesList = \addons\cms\model\Archives::where('status', 'normal')->field('id,channel_id,diyname,createtime,publishtime')->paginate($pagesize, false, ['path' => $path]);
  49. $lastPage = $archivesList->lastPage();
  50. foreach ($archivesList->getUrlRange(1, $lastPage) as $index => $item) {
  51. $list[] = ['loc' => url($item, '', 'xml', true)];
  52. }
  53. }
  54. if (!$type || $type == 'tags') {
  55. $path = "/addons/cms/sitemap/tags/{$pagesizeStr}page/[PAGE]";
  56. $tagsList = \addons\cms\model\Tag::where('status', 'normal')->field('id,name')->paginate($pagesize, false, ['path' => $path]);
  57. $lastPage = $tagsList->lastPage();
  58. foreach ($tagsList->getUrlRange(1, $lastPage) as $index => $item) {
  59. $list[] = ['loc' => url($item, '', 'xml', true)];
  60. }
  61. }
  62. if (!$type || $type == 'users') {
  63. $path = "/addons/cms/sitemap/users/{$pagesizeStr}page/[PAGE]";
  64. $usersList = \addons\cms\model\User::where('status', 'normal')->field('id')->paginate($pagesize, false, ['path' => $path]);
  65. $lastPage = $usersList->lastPage();
  66. foreach ($usersList->getUrlRange(1, $lastPage) as $index => $item) {
  67. $list[] = ['loc' => url($item, '', 'xml', true)];
  68. }
  69. }
  70. $this->options = [
  71. 'item_key' => '',
  72. 'root_node' => 'sitemapindex',
  73. 'item_node' => 'sitemap',
  74. 'root_attr' => ''
  75. ];
  76. return xml($list, 200, [], $this->options);
  77. }
  78. /**
  79. * 栏目
  80. */
  81. public function channels()
  82. {
  83. $pagesize = $this->request->param('pagesize/d', $this->config['pagesize']);
  84. $archivesList = \addons\cms\model\Channel::where('status', 'normal')->cache($this->config['cache'])->field('id,name,diyname,createtime')->paginate($pagesize);
  85. $list = [];
  86. foreach ($archivesList as $index => $item) {
  87. $list[] = [
  88. 'loc' => $item->fullurl,
  89. 'priority' => 0.6
  90. ];
  91. }
  92. return xml($list, 200, [], $this->options);
  93. }
  94. /**
  95. * 文章
  96. */
  97. public function archives()
  98. {
  99. $pagesize = $this->request->param('pagesize/d', $this->config['pagesize']);
  100. $archivesList = \addons\cms\model\Archives::where('status', 'normal')->cache($this->config['cache'])->field('id,channel_id,diyname,createtime')->paginate($pagesize);
  101. $list = [];
  102. foreach ($archivesList as $index => $item) {
  103. $list[] = [
  104. 'loc' => $item->fullurl,
  105. 'priority' => 0.8
  106. ];
  107. }
  108. return xml($list, 200, [], $this->options);
  109. }
  110. /**
  111. * 标签
  112. */
  113. public function tags()
  114. {
  115. $pagesize = $this->request->param('pagesize/d', $this->config['pagesize']);
  116. $tagsList = \addons\cms\model\Tag::where('status', 'normal')->cache($this->config['cache'])->field('id,name')->paginate($pagesize);
  117. $list = [];
  118. foreach ($tagsList as $index => $item) {
  119. $list[] = [
  120. 'loc' => $item->fullurl,
  121. 'priority' => 0.6
  122. ];
  123. }
  124. return xml($list, 200, [], $this->options);
  125. }
  126. /**
  127. * 用户
  128. */
  129. public function users()
  130. {
  131. $pagesize = $this->request->param('pagesize/d', $this->config['pagesize']);
  132. $userList = \addons\cms\model\User::where('status', 'normal')->cache($this->config['cache'])->field('id')->paginate($pagesize);
  133. $list = [];
  134. foreach ($userList as $index => $item) {
  135. $list[] = [
  136. 'loc' => $item->fullurl,
  137. 'priority' => 0.6
  138. ];
  139. }
  140. return xml($list, 200, [], $this->options);
  141. }
  142. }