This commit is contained in:
浪子
2025-08-05 10:04:56 +08:00
parent 57aeb81c62
commit a106080633
5 changed files with 50 additions and 79 deletions
+10 -63
View File
@@ -52,67 +52,14 @@ function getRandomPost() {
// 获取随机文章链接
$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);
// 直接重定向,实现无感跳转
if ($randomPostUrl) {
// 使用 302 临时重定向,避免缓存
header('Location: ' . $randomPostUrl, true, 302);
exit;
} else {
// 如果没有文章,重定向到首页
header('Location: ' . $this->options->siteUrl, true, 302);
exit;
}
// 在 DOMContentLoaded 和页面加载完成后都执行
document.addEventListener('DOMContentLoaded', performRandomRedirect);
window.addEventListener('load', performRandomRedirect);
// 立即执行一次,确保在 PJAX 模式下也能工作
performRandomRedirect();
</script>
<?php $this->need('footer.php'); ?>
?>