fix 随机文章跳转
fix 无说说时无评论框的bug
This commit is contained in:
浪子 2025-08-04 19:46:24 +08:00
parent 4dcaefb291
commit 57aeb81c62
3 changed files with 169 additions and 81 deletions

View File

@ -66,4 +66,17 @@
- 增加侧边栏显示的全局开关
- 修复代码块中的短代码解析问题
- 新增一个随机文章阅读的独立页面
- 新增一个随机文章阅读的独立页面
- 2025.08.03
- 1.2.3
- 修复随机页面在 PJAX 模式下的缓存问题
- 在随机页面添加缓存控制头,防止 PJAX 缓存
- 在 JavaScript 中添加随机页面检测,强制刷新确保获取新文章
- 修复后端登录验证问题,确保密码错误时返回正确的错误信息
- 重新设计随机页面模板,添加完整的页面结构和加载动画
- 修复随机页面重定向问题,使用 JavaScript 跳转避免缓存
- 修复随机页面 JavaScript 错误,移除不存在的 InstantClick.preload 方法调用
- 优化随机页面跳转逻辑,确保在 PJAX 模式下也能正常自动跳转

View File

@ -6,38 +6,113 @@
*/
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$db = Typecho_Db::get();
// 防止 PJAX 缓存,确保每次都是随机文章
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
header('X-InstantClick: no-cache');
// 统计符合条件的文章总数
$countSql = $db->select('COUNT(*) AS count')
->from('table.contents')
->where('status = ?', 'publish')
->where('type = ?', 'post')
->where('created <= ?', time());
$countResult = $db->fetchRow($countSql);
$total = $countResult['count'];
if ($total > 0) {
// 随机选择一个偏移量
$offset = mt_rand(0, $total - 1);
// 根据偏移量获取一篇文章
$sql = $db->select()
// 获取随机文章
function getRandomPost() {
$db = Typecho_Db::get();
// 统计符合条件的文章总数
$countSql = $db->select('COUNT(*) AS count')
->from('table.contents')
->where('status = ?', 'publish')
->where('type = ?', 'post')
->where('created <= ?', time())
->limit(1)
->offset($offset);
->where('created <= ?', time());
$countResult = $db->fetchRow($countSql);
$total = $countResult['count'];
$result = $db->fetchRow($sql);
if ($total > 0) {
// 随机选择一个偏移量
$offset = mt_rand(0, $total - 1);
if (!empty($result)) {
$target = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($result);
$this->response->redirect($target['permalink'], 307);
// 根据偏移量获取一篇文章
$sql = $db->select()
->from('table.contents')
->where('status = ?', 'publish')
->where('type = ?', 'post')
->where('created <= ?', time())
->limit(1)
->offset($offset);
$result = $db->fetchRow($sql);
if (!empty($result)) {
$target = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($result);
return $target['permalink'];
}
}
return false;
}
// 如果没有找到文章,重定向到首页
$this->response->redirect($this->options->siteUrl, 307);
?>
// 获取随机文章链接
$randomPostUrl = getRandomPost();
$this->need('header.php');
?>
<div id="breadcrumb" class="animated fadeInUp">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a class="a-link" href="<?php $this->options->siteUrl(); ?>">首页</a></li>
<li class="breadcrumb-item active" aria-current="page">随机阅读</li>
</ol>
</nav>
</div>
<div id="page-empty">
<div id="page" class="row row-cols-1">
<?php if ($this->options->showsidebar): ?>
<div id="post-main" class="col-lg-8 col-md-12 animated fadeInLeft">
<?php else: ?>
<div id="post-main" class="col-lg-12 col-md-12">
<?php endif; ?>
<div class="p-block">
<div>
<h1 id="post-title" class="mb-0 puock-text t-xxl">随机阅读</h1>
</div>
<div class="mt20 puock-text entry-content show-link-icon">
<div class="text-center">
<div class="mb-4">
<i class="fa fa-random fa-3x text-primary mb-3"></i>
<h3>正在为您随机选择一篇文章...</h3>
<p class="text-muted">请稍候,正在跳转中...</p>
</div>
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</div>
</div>
<?php if ($this->options->showsidebar): ?>
<?php $this->need('sidebar.php'); ?>
<?php endif; ?>
</div>
</div>
<script>
// 立即执行的跳转函数
function performRandomRedirect() {
setTimeout(function() {
<?php if ($randomPostUrl): ?>
// 跳转到随机文章
window.location.href = '<?php echo $randomPostUrl; ?>';
<?php else: ?>
// 如果没有文章,跳转到首页
window.location.href = '<?php $this->options->siteUrl(); ?>';
<?php endif; ?>
}, 1500);
}
// 在 DOMContentLoaded 和页面加载完成后都执行
document.addEventListener('DOMContentLoaded', performRandomRedirect);
window.addEventListener('load', performRandomRedirect);
// 立即执行一次,确保在 PJAX 模式下也能工作
performRandomRedirect();
</script>
<?php $this->need('footer.php'); ?>

View File

@ -70,61 +70,61 @@ $this->need('header.php');
</div>
<?php } ?>
<?php $this->comments()->to($comments); ?>
<?php if ($this->user->hasLogin() && $this->user->group == 'administrator') : ?>
<div class="p-block">
<input type="hidden" value="<?php $this->commentUrl() ?>">
<div>
<span class="t-lg border-bottom border-primary puock-text pb-2">
<i class="fa-regular fa-comments mr-1"></i>有什么新鲜事
</span>
</div>
<div class="mt20 clearfix" id="comment-form-box">
<form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" class="mt10" role="form">
<div class="form-group">
<textarea rows="4" name="text" id="comment" class="form-control form-control-sm t-sm" placeholder="发表您的新鲜事儿..." required><?php $this->remember('text'); ?></textarea>
</div>
<input type="hidden" value="<?php $this->user->screenName(); ?>" name="author" />
<input type="hidden" value="<?php $this->user->mail(); ?>" name="mail" />
<input type="hidden" value="<?php $this->options->siteUrl(); ?>" name="url" />
<input type="hidden" name="_" value="<?php Typecho_Widget::widget('Widget_Security')->to($security);
echo $security->getToken($this->request->getRequestUrl()); ?>">
<div class="p-flex-sbc mt10">
<div class="form-foot">
<?php if($this->options->social): ?>
<button id="comment-insert-image" class="btn btn-outline-secondary btn-ssm pk-modal-toggle" type="button" title="插入图片">
<i class="fa-solid fa-image"></i>
</button>
<button id="comment-smiley" class="btn btn-outline-secondary btn-ssm pk-modal-toggle" type="button" title="表情" data-once-load="true"
data-url="<?php echo get_correct_url('/emoji/'); ?>">
<i class="fa-regular fa-face-smile t-md"></i>
</button>
<?php endif; ?>
<button type="submit" class="btn btn-primary btn-ssm"><i class="fa-regular fa-paper-plane"></i> 立即发表</button>
</div>
</div>
</form>
</div>
</div>
<?php endif; ?>
<?php if ($comments->have()): ?>
<?php if ($this->user->hasLogin() && $this->user->group == 'administrator') : ?>
<div class="p-block">
<input type="hidden" value="<?php $this->commentUrl() ?>">
<div>
<span class="t-lg border-bottom border-primary puock-text pb-2">
<i class="fa-regular fa-comments mr-1"></i>有什么新鲜事
</span>
</div>
<div class="mt20 clearfix" id="comment-form-box">
<form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" class="mt10" role="form">
<div class="form-group">
<textarea rows="4" name="text" id="comment" class="form-control form-control-sm t-sm" placeholder="发表您的新鲜事儿..." required><?php $this->remember('text'); ?></textarea>
</div>
<input type="hidden" value="<?php $this->user->screenName(); ?>" name="author" />
<input type="hidden" value="<?php $this->user->mail(); ?>" name="mail" />
<input type="hidden" value="<?php $this->options->siteUrl(); ?>" name="url" />
<input type="hidden" name="_" value="<?php Typecho_Widget::widget('Widget_Security')->to($security);
echo $security->getToken($this->request->getRequestUrl()); ?>">
<div class="p-flex-sbc mt10">
<div class="form-foot">
<?php if($this->options->social): ?>
<button id="comment-insert-image" class="btn btn-outline-secondary btn-ssm pk-modal-toggle" type="button" title="插入图片">
<i class="fa-solid fa-image"></i>
</button>
<button id="comment-smiley" class="btn btn-outline-secondary btn-ssm pk-modal-toggle" type="button" title="表情" data-once-load="true"
data-url="<?php echo get_correct_url('/emoji/'); ?>">
<i class="fa-regular fa-face-smile t-md"></i>
</button>
<?php endif; ?>
<button type="submit" class="btn btn-primary btn-ssm"><i class="fa-regular fa-paper-plane"></i> 立即发表</button>
</div>
</div>
</form>
</div>
</div>
<?php endif; ?>
<!-- 评论列表 -->
<?php while ($comments->next()): ?>
<?php threadedComments($comments, $this->options); ?>
<?php endwhile; ?>
<!-- 分页导航 -->
<div class="mt20 p-flex-s-right" data-no-instant>
<?php $comments->pageNav('&laquo;', '&raquo;', 1, '...', array(
'wrapTag' => 'ul',
'wrapClass' => 'pagination comment-ajax-load',
'itemTag' => 'li',
'textTag' => 'span',
'currentClass' => 'active',
'prevClass' => 'prev',
'nextClass' => 'next'
)); ?>
</div>
</div>
<?php endif; ?>
<!-- 评论列表 -->
<?php while ($comments->next()): ?>
<?php threadedComments($comments, $this->options); ?>
<?php endwhile; ?>
<!-- 分页导航 -->
<div class="mt20 p-flex-s-right" data-no-instant>
<?php $comments->pageNav('&laquo;', '&raquo;', 1, '...', array(
'wrapTag' => 'ul',
'wrapClass' => 'pagination comment-ajax-load',
'itemTag' => 'li',
'textTag' => 'span',
'currentClass' => 'active',
'prevClass' => 'prev',
'nextClass' => 'next'
)); ?>
</div>
</div>
<?php endif; ?>
<?php if ($this->options->showsidebar): ?>
<?php $this->need('sidebar.php'); ?>
<?php endif; ?>