Files
RecentlyActive/使用说明.md
2026-02-23 19:51:00 +08:00

396 lines
9.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Typecho RecentlyActive 插件使用文档
插件信息
插件名称: RecentlyActive
功能: 显示用户最近活跃时间
版本: 1.0.0
兼容性: Typecho 1.x
作者: Your Name
网站: https://yourwebsite.com
功能特点
✅ 直接读取 Typecho 自带的 activated 字段,无需额外数据库操作
✅ 支持三种时间显示模式:相对时间、绝对时间、智能模式
✅ 自动判断在线/离线状态
✅ 显示状态点(可配置开关)
✅ Tooltip 提示完整时间
✅ 侧边栏最近活跃用户列表
✅ 后台全局配置,无需用户单独设置
✅ 轻量级,不影响性能
安装方法
方法一:手动安装
下载插件压缩包
解压得到 RecentlyActive 文件夹
上传到 Typecho 插件目录:/usr/plugins/
确保路径为:/usr/plugins/RecentlyActive/Plugin.php
登录 Typecho 后台,进入"控制台" → "插件"
找到"用户最近活跃时间显示插件",点击"启用"
方法二:服务器安装
bash
# 进入插件目录
cd /path/to/typecho/usr/plugins/
# 创建插件目录
mkdir RecentlyActive
# 上传 Plugin.php 到该目录
后台配置
激活插件后,在插件管理页面点击"设置"进行配置:
基本设置
配置项 说明 默认值
时间显示模式 相对时间/绝对时间/智能模式 智能模式
时间格式 绝对时间显示格式PHP date()格式) Y-m-d H:i
在线状态阈值 多少分钟内显示为"在线"状态(分钟) 10
状态点显示 是否显示在线/离线状态点 开启
默认显示文字 用户从未活跃时显示的文字 从未活跃
配置说明
1. 时间显示模式
相对时间: 显示"3小时前"、"2天前"等
绝对时间: 显示"2023-01-01 12:00"
智能模式: 24小时内用相对时间更早用绝对时间
2. 时间格式
支持所有 PHP date() 函数格式,常用格式:
Y-m-d H:i → 2023-01-01 12:00
m/d H:i → 01/01 12:00
H:i → 12:00
Y年m月d日 H:i → 2023年01月01日 12:00
3. 在线状态阈值
设置用户多少分钟内活跃算"在线"
例如设为10则表示10分钟内活跃的用户显示为在线状态绿色
主题调用方法
基本调用
显示文章作者活跃时间
php
<?php if ($this->authorId): ?>
<div class="author-info">
<span>作者: <?php $this->author(); ?></span>
<?php echo RecentlyActive_Plugin::show($this->authorId); ?>
</div>
<?php endif; ?>
显示评论者活跃时间
php
<?php $this->comments()->to($comments); ?>
<?php while($comments->next()): ?>
<div class="comment">
<div class="comment-author">
<?php $comments->author(); ?>
<?php echo RecentlyActive_Plugin::show($comments->authorId); ?>
</div>
<div class="comment-content">
<?php $comments->content(); ?>
</div>
</div>
<?php endwhile; ?>
安全调用(推荐)
php
<?php
// 安全调用,确保插件存在
if ($this->authorId && class_exists('RecentlyActive_Plugin')) {
echo RecentlyActive_Plugin::show($this->authorId);
}
?>
侧边栏活跃用户列表
php
<?php if (class_exists('RecentlyActive_Plugin')): ?>
<?php $activeUsers = RecentlyActive_Plugin::getActiveUsers(5); ?>
<?php if ($activeUsers): ?>
<div class="widget">
<h3>最近活跃用户</h3>
<ul class="active-users">
<?php foreach ($activeUsers as $user): ?>
<li>
<a href="<?php echo $user['url']; ?>" target="_blank" title="<?php echo $user['name']; ?>">
<img src="<?php echo $user['avatar']; ?>" width="32" height="32" alt="<?php echo $user['name']; ?>">
<span class="user-name"><?php echo $user['name']; ?></span>
<span class="active-time"><?php echo $user['timeText']; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>
自定义参数调用
php
<?php
// 自定义显示选项
echo RecentlyActive_Plugin::show($userId, array(
'display_mode' => 'relative', // 强制使用相对时间
'online_threshold' => 30, // 30分钟内算在线
'show_dot' => 0, // 不显示状态点
'date_format' => 'H:i', // 时间格式:只显示时分
'default_text' => '暂无记录' // 自定义默认文字
));
?>
参数说明
参数 类型 说明 默认值
display_mode string 显示模式relative/absolute/smart 插件设置
online_threshold int 在线状态阈值(分钟) 插件设置
show_dot int 是否显示状态点1显示/0不显示 插件设置
date_format string 绝对时间格式 插件设置
default_text string 默认显示文字 插件设置
CSS 样式定制
插件自带基本样式,如需自定义可覆盖以下 CSS 类:
css
/* 活跃时间基础样式 */
.recently-active {
font-size: 12px;
color: #666;
margin-left: 5px;
}
/* 在线状态 */
.recently-active.online {
color: #52c41a; /* 绿色 */
}
/* 离线状态 */
.recently-active.offline {
color: #999; /* 灰色 */
}
/* 状态点 */
.recently-active.online:before {
content: "● ";
font-size: 10px;
}
.recently-active.offline:before {
content: "○ ";
font-size: 10px;
}
/* 不显示状态点 */
.recently-active.no-dot:before {
content: "" !important;
}
/* 工具提示 */
.recently-active-tooltip {
cursor: help;
border-bottom: 1px dotted #ccc;
}
/* 侧边栏活跃用户列表 */
.active-users {
list-style: none;
padding: 0;
margin: 0;
}
.active-users li {
padding: 8px 0;
border-bottom: 1px solid #eee;
display: flex;
align-items: center;
}
.active-users li:last-child {
border-bottom: none;
}
.active-users img {
border-radius: 50%;
margin-right: 10px;
}
.active-users .user-name {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.active-users .active-time {
font-size: 11px;
color: #999;
}
显示效果示例
1. 在线状态
text
作者:张三 ● 3分钟前
(绿色文字,带实心圆点)
2. 离线状态
text
作者:李四 ○ 3小时前
(灰色文字,带空心圆点)
3. 智能模式
24小时内2小时前
24小时外2023-01-01 12:00
4. Tooltip 提示
鼠标悬停在活跃时间上,显示完整时间:
text
最后活跃: 2023-12-10 14:30:25
常见问题
Q1: 插件激活后没有任何显示?
A: 请检查:
插件是否已激活
主题中是否正确调用 RecentlyActive_Plugin::show()
用户是否有 activated 字段值
查看网页源代码,确认是否有输出
Q2: 显示"从未活跃"是什么情况?
A: 表示该用户的 activated 字段为 0 或空,可能是:
用户从未登录过
Typecho 未更新该字段
用户数据异常
Q3: 如何修改在线状态的颜色?
A: 在主题CSS中添加
css
.recently-active.online {
color: #ff0000; /* 改为红色 */
}
Q4: 侧边栏头像不显示?
A: Gravatar 可能需要科学上网,可以替换为本地头像:
php
// 修改 Plugin.php 中的 getGravatar 方法
private static function getGravatar($email, $size = 40)
{
// 使用本地默认头像
return Helper::options()->themeUrl . '/images/default-avatar.png';
}
Q5: 时间显示不正确?
A: 检查服务器时区设置:
Typecho 后台 → 设置 → 时区
服务器 PHP 时区设置
确保时间戳正确
高级用法
在 functions.php 中添加辅助函数
php
// 在主题的 functions.php 中添加
if (!function_exists('showActiveTime')) {
/**
* 显示用户活跃时间(简化调用)
* @param int $userId 用户ID
* @param array $options 自定义选项
* @return string
*/
function showActiveTime($userId = null, $options = array())
{
if (class_exists('RecentlyActive_Plugin')) {
return RecentlyActive_Plugin::show($userId, $options);
}
return '';
}
}
// 使用
echo showActiveTime($this->authorId);
获取原始活跃时间戳
php
<?php
if (class_exists('RecentlyActive_Plugin')) {
$activeTime = RecentlyActive_Plugin::getActiveTime($userId);
if ($activeTime) {
echo '时间戳:' . $activeTime;
echo '格式化:' . date('Y-m-d H:i:s', $activeTime);
}
}
?>
判断用户是否在线
php
<?php
if (class_exists('RecentlyActive_Plugin')) {
$activeTime = RecentlyActive_Plugin::getActiveTime($userId);
$isOnline = false;
if ($activeTime) {
$diff = time() - $activeTime;
$isOnline = ($diff < 600); // 10分钟内
}
if ($isOnline) {
echo '<span class="online-status">在线</span>';
} else {
echo '<span class="offline-status">离线</span>';
}
}
?>
文件结构
text
RecentlyActive/
├── Plugin.php # 插件主文件
├── README.md # 说明文档(本文件)
└── screenshot.png # 插件截图(可选)
更新日志
v1.0.0 (2023-12-10)
✅ 首次发布
✅ 基本活跃时间显示功能
✅ 三种时间显示模式
✅ 在线状态判断
✅ 侧边栏活跃用户列表
✅ 后台配置界面
技术支持
如有问题,可通过以下方式联系:
在插件发布页面留言
访问作者网站
GitHub Issues如有
注意事项
本插件依赖 Typecho 的 activated 字段,请确保该字段正常工作
部分主题可能需要调整 CSS 样式以适应显示
建议在正式使用前进行测试
定期备份数据库
许可证
MIT License
祝您使用愉快! 🎉
文档版本v1.0.0
最后更新2023-12-10
作者Your Name