mirror of
https://gitee.com/ulthon/ulthon_information.git
synced 2026-03-03 16:24:28 +08:00
优化查询代码;增加RSS1
This commit is contained in:
48
app/common/tools/Rss.php
Normal file
48
app/common/tools/Rss.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace app\common\tools;
|
||||
|
||||
use FeedWriter\RSS1;
|
||||
|
||||
class Rss
|
||||
{
|
||||
public static function initRss1()
|
||||
{
|
||||
$feed = new RSS1();
|
||||
|
||||
|
||||
$feed->setTitle(Site::name());
|
||||
$feed->setLink(Site::indexUrl());
|
||||
$feed->setDescription(Site::desc());
|
||||
|
||||
//It's important for RSS 1.0
|
||||
$feed->setChannelAbout(Site::keywords());
|
||||
|
||||
// An image is optional.
|
||||
$feed->setImage(Site::logo(), 'PHP武器库头像', get_source_link('/'));
|
||||
|
||||
//Adding a feed. Generally this portion will be in a loop and add all feeds.
|
||||
|
||||
$list_post = Site::mapRecentlyPost();
|
||||
|
||||
foreach ($list_post as $model_post) {
|
||||
//Create an empty FeedItem
|
||||
|
||||
$new_item = $feed->createNewItem();
|
||||
|
||||
$new_item->setTitle($model_post->title);
|
||||
|
||||
$new_item->setLink($model_post->read_url);
|
||||
//The parameter is a timestamp for setDate() function
|
||||
$new_item->setDate($model_post->publish_time);
|
||||
$new_item->setDescription($model_post->desc);
|
||||
//Use core addElement() function for other supported optional elements
|
||||
|
||||
//Now add the feed item
|
||||
$feed->addItem($new_item);
|
||||
}
|
||||
|
||||
$content = $feed->generateFeed();
|
||||
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
45
app/common/tools/Site.php
Normal file
45
app/common/tools/Site.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
namespace app\common\tools;
|
||||
|
||||
use think\facade\Request;
|
||||
|
||||
class Site
|
||||
{
|
||||
public static function mapAllPost()
|
||||
{
|
||||
$list_post = \app\model\Post::cache(60)->where('status', 1)->select();
|
||||
|
||||
return $list_post;
|
||||
}
|
||||
public static function mapRecentlyPost()
|
||||
{
|
||||
$list_post = \app\model\Post::cache(60)->where('status', 1)->order('publish_time', "desc")->limit(25)->select();
|
||||
|
||||
return $list_post;
|
||||
}
|
||||
|
||||
public static function name()
|
||||
{
|
||||
return get_system_config('site_name');
|
||||
}
|
||||
|
||||
public static function indexUrl()
|
||||
{
|
||||
return Request::scheme() . '://' . get_system_config('main_domain', Request::host());
|
||||
}
|
||||
|
||||
public static function desc()
|
||||
{
|
||||
return get_system_config('site_desc');
|
||||
}
|
||||
|
||||
public static function logo()
|
||||
{
|
||||
return get_source_link(get_system_config('site_logo'));
|
||||
}
|
||||
|
||||
public static function keywords()
|
||||
{
|
||||
return get_system_config('site_keywords');
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace app\common\tools;
|
||||
|
||||
use app\model\Post;
|
||||
|
||||
use DateTime;
|
||||
use Thepixeldeveloper\Sitemap\Drivers\XmlWriterDriver;
|
||||
use Thepixeldeveloper\Sitemap\Url;
|
||||
@@ -18,7 +18,7 @@ class Sitemap
|
||||
|
||||
$urlset_page_post = new Urlset();
|
||||
|
||||
$index_url = new Url(Request::scheme() . '://' . get_system_config('main_domain', Request::host()));
|
||||
$index_url = new Url(Site::indexUrl());
|
||||
|
||||
$index_url->setChangeFreq('always');
|
||||
|
||||
@@ -26,7 +26,7 @@ class Sitemap
|
||||
|
||||
$urlset_page_post->add($index_url);
|
||||
|
||||
$list_post = Post::cache(60)->where('status', 1)->select();
|
||||
$list_post = Site::mapAllPost();
|
||||
|
||||
foreach ($list_post as $model_post) {
|
||||
$url_post = new Url($model_post->read_url);
|
||||
|
||||
@@ -98,6 +98,15 @@ class Post extends Base
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getDescAttr($value)
|
||||
{
|
||||
if(empty($value)){
|
||||
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getDescShortAttr()
|
||||
{
|
||||
$desc = $this->getData('desc');
|
||||
@@ -181,8 +190,9 @@ class Post extends Base
|
||||
|
||||
public function getReadUrlAttr()
|
||||
{
|
||||
$path = '/index/a' . $this->getData('uid') . '.html';
|
||||
|
||||
return Request::domain() . '/index/a' . $this->getData('uid') . '.html';
|
||||
return Request::domain() . $path;
|
||||
}
|
||||
|
||||
public function getShareTextAttr()
|
||||
@@ -195,4 +205,4 @@ class Post extends Base
|
||||
|
||||
return $share_text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,8 @@
|
||||
"thepixeldeveloper/sitemap": "^5.1",
|
||||
"jaeger/phpquery-single": "^1.1",
|
||||
"league/html-to-markdown": "^5.1",
|
||||
"topthink/think-filesystem": "^2.0"
|
||||
"topthink/think-filesystem": "^2.0",
|
||||
"mibe/feedwriter": "^1.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/var-dumper": "^4.2"
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
use app\common\tools\Rss;
|
||||
use app\common\tools\Sitemap;
|
||||
use app\Request;
|
||||
use think\facade\Cache;
|
||||
@@ -27,7 +28,6 @@ Route::rule('/sitemap.xml', function (Request $request) {
|
||||
|
||||
if (!empty($if_not_match)) {
|
||||
if ($if_not_match == $last_etag) {
|
||||
Log::debug('sitemap go for etag cache');
|
||||
return xml('', 304)->eTag($last_etag);
|
||||
}
|
||||
}
|
||||
@@ -41,3 +41,30 @@ Route::rule('/sitemap.xml', function (Request $request) {
|
||||
|
||||
return xml($content)->eTag($last_etag);
|
||||
});
|
||||
|
||||
|
||||
Route::rule('/rss1.xml', function (Request $request) {
|
||||
|
||||
$cache_key = 'rss1_cache_key';
|
||||
|
||||
$last_etag = Cache::get($cache_key);
|
||||
|
||||
if (!empty($cache_key)) {
|
||||
|
||||
$if_not_match = $request->header('If-None-Match');
|
||||
|
||||
if (!empty($if_not_match)) {
|
||||
if ($if_not_match == $last_etag) {
|
||||
return xml('', 304)->eTag($last_etag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$content = Rss::initRss1();
|
||||
|
||||
$last_etag = md5($content);
|
||||
|
||||
Cache::set($cache_key, $last_etag);
|
||||
|
||||
return xml($content)->eTag($last_etag);
|
||||
});
|
||||
Reference in New Issue
Block a user