控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sitemap.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | sitemap管理
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use addons\sitemap\library\Sitemap as SitemapLib;
  16. use app\common\controller\Adminbase;
  17. use think\Db;
  18. class Sitemap extends Adminbase
  19. {
  20. protected $filename = 'sitemap.xml';
  21. protected $data = [];
  22. protected $directory;
  23. protected $url_mode = 1;
  24. protected function initialize()
  25. {
  26. parent::initialize();
  27. $this->url_mode = (int) get_addon_config('cms')['site_url_mode'];
  28. $this->directory = (defined('IF_PUBLIC') ? ROOT_PATH . 'public/' : ROOT_PATH);
  29. }
  30. public function index()
  31. {
  32. if ($this->request->isPost()) {
  33. $info = get_addon_info('cms');
  34. if (!$info || $info['status'] != 1) {
  35. $this->error("请在后台插件管理中安装《内容管理系统》并启用后再尝试");
  36. }
  37. $Category = cache('Category');
  38. $data = $this->request->post();
  39. $Sitemap = new SitemapLib();
  40. $rootUrl = $this->request->domain();
  41. $data['num'] = intval($data['num']);
  42. $type = isset($data['type']) ? intval($data['type']) : 1;
  43. $item = $this->_sitemap_item($rootUrl, intval($data['index']['priority']), $data['index']['changefreq'], time());
  44. $this->_add_data($item);
  45. //栏目
  46. $List = Db::name('Category')->where('status', 1)->order('id desc')->field('id,url,catdir')->select();
  47. if (!empty($List)) {
  48. foreach ($List as $vo) {
  49. $cat = $this->url_mode == 1 ? $vo['id'] : $vo['catdir'];
  50. $item = $this->_sitemap_item($rootUrl . buildCatUrl($cat), intval($data['category']['priority']), $data['category']['changefreq'], time());
  51. $this->_add_data($item);
  52. }
  53. }
  54. //列表
  55. $modelList = cache('Model');
  56. if (!empty($modelList)) {
  57. $num = 1;
  58. $volist = [];
  59. foreach ($modelList as $vo) {
  60. if ($vo['module'] == "cms") {
  61. $volist = Db::name($vo['tablename'])->where('status', 1)->order('update_time desc')->field('id,catid,update_time')->select();
  62. if (!empty($volist)) {
  63. foreach ($volist as $v) {
  64. $cat = $this->url_mode == 1 ? $v['catid'] : (isset($Category[$v['catid']]) ? $Category[$v['catid']]['catdir'] : getCategory($v['catid'], 'catdir'));
  65. $item = $this->_sitemap_item($rootUrl . buildContentUrl($cat, $v['id']), intval($data['content']['priority']), $data['content']['changefreq'], $v['update_time']);
  66. $this->_add_data($item);
  67. $num++;
  68. if ($num >= $data['num']) {
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. //标签
  77. $tags = Db::name('tags')->order('create_time desc')->field('tag,update_time')->select();
  78. if (!empty($tags)) {
  79. foreach ($tags as $vo) {
  80. $item = $this->_sitemap_item($rootUrl . url('cms/index/tags', ['tag' => $vo['tag']]), intval($data['tag']['priority']), $data['tag']['changefreq'], time());
  81. $this->_add_data($item);
  82. }
  83. }
  84. if (!$type) {
  85. try {
  86. foreach ($this->data as $val) {
  87. $Sitemap->AddItem($val['loc'], $val['priority'], $val['changefreq'], $val['lastmod']);
  88. }
  89. $Sitemap->SaveToFile($this->directory . $this->filename);
  90. } catch (\Exception $ex) {
  91. $this->error($ex->getMessage());
  92. }
  93. } else {
  94. $str = $this->_txt_format();
  95. $this->filename = 'sitemap.txt';
  96. @file_put_contents($this->directory . $this->filename, $str);
  97. }
  98. $this->success($this->filename . "文件已生成到运行根目录");
  99. } else {
  100. if (is_file($this->directory . 'sitemap.xml')) {
  101. $make_xml_time = date('Y-m-d H:i:s', filemtime($this->directory . 'sitemap.xml'));
  102. $this->assign('make_xml_time', $make_xml_time);
  103. }
  104. if (is_file($this->directory . 'sitemap.txt')) {
  105. $make_txt_time = date('Y-m-d H:i:s', filemtime($this->directory . 'sitemap.txt'));
  106. $this->assign('make_txt_time', $make_txt_time);
  107. }
  108. return $this->fetch();
  109. }
  110. }
  111. /**
  112. * 添加数据
  113. */
  114. private function _add_data($new_item)
  115. {
  116. $this->data[] = $new_item;
  117. }
  118. /**
  119. * 生成txt格式
  120. */
  121. private function _txt_format()
  122. {
  123. $str = '';
  124. foreach ($this->data as $val) {
  125. $str .= $val['loc'] . PHP_EOL;
  126. }
  127. return $str;
  128. }
  129. /**
  130. * 创建地图格式
  131. */
  132. private function _sitemap_item($loc, $priority = '', $changefreq = '', $lastmod = '')
  133. {
  134. $data = [];
  135. $data['loc'] = $loc;
  136. $data['priority'] = $priority;
  137. $data['changefreq'] = $changefreq;
  138. $data['lastmod'] = $lastmod;
  139. return $data;
  140. }
  141. }