Memos 是一款轻量级的开源笔记应用,不仅部署方便,还支持多端 APP 发布,非常适合用来当作个人的‘朋友圈’记录日常、发发牢骚。最近刚重新搭建了 Typecho 博客,便萌生了将 Memos 嵌入到博客页面中的想法。用龙虾搓了一个插件,使用中影响了侧边栏,干脆就直接把代码放入首页文章列表前。
<!--memos-->
<?php
// ===== Memos 说说卡片 =====
$memosApiUrl = 'https://memos网址/api/v1/memos';
$memosToken = 'token';
$memosCacheFile = __DIR__ . '/memos_cache.json';
$memosCacheTime = 300;
// 检查缓存
$memosData = null;
if (file_exists($memosCacheFile)) {
$memosCache = json_decode(file_get_contents($memosCacheFile), true);
if ($memosCache && (time() - $memosCache['time']) < $memosCacheTime) {
$memosData = $memosCache['data'];
}
}
// 无缓存则请求 API
if (!$memosData) {
$memosCh = curl_init();
curl_setopt_array($memosCh, [
CURLOPT_URL => $memosApiUrl . '?pageSize=1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $memosToken,
'Accept: application/json'
],
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false
]);
$memosResponse = curl_exec($memosCh);
curl_close($memosCh);
$memosJson = json_decode($memosResponse, true);
if (!empty($memosJson['memos'][0])) {
$memosData = $memosJson['memos'][0];
file_put_contents($memosCacheFile, json_encode([
'time' => time(),
'data' => $memosData
]));
}
}
// 输出卡片
if ($memosData):
$memosContent = nl2br(htmlspecialchars($memosData['content'] ?? '', ENT_QUOTES, 'UTF-8'));
// 时间格式化
$memosTime = strtotime($memosData['createTime'] ?? $memosData['createdTs'] ?? '');
$memosDiff = time() - $memosTime;
if ($memosDiff < 60) $memosTimeStr = '刚刚';
elseif ($memosDiff < 3600) $memosTimeStr = floor($memosDiff / 60) . ' 分钟前';
elseif ($memosDiff < 86400) $memosTimeStr = floor($memosDiff / 3600) . ' 小时前';
elseif ($memosDiff < 604800) $memosTimeStr = floor($memosDiff / 86400) . ' 天前';
else $memosTimeStr = date('Y 年 m 月 d 日', $memosTime);
// 图片处理
$memosImages = '';
$memosResources = $memosData['resources'] ?? $memosData['attachments'] ?? [];
foreach ($memosResources as $memosR) {
$memosImgUrl = $memosR['externalLink'] ?? '';
if (empty($memosImgUrl) && !empty($memosR['name'])) {
$memosImgUrl = 'https://028317.xyz/file/' . $memosR['name'];
}
if ($memosImgUrl) {
$memosImages .= '<img src="' . htmlspecialchars($memosImgUrl) . '" loading="lazy" style="width:100%;aspect-ratio:1/1;object-fit:cover;border-radius:6px;cursor:zoom-in;">';
}
}
?>
<div id="memos-card" style="background:var(--bg-color,#fff);border-radius:8px;padding:20px;box-shadow:0 1px 3px rgba(0,0,0,0.08);border:1px solid var(--border-color,#eee);margin-bottom:20px;">
<div style="font-size:15px;line-height:1.8;color:var(--text-color,#333);margin-bottom:12px;"><?php echo $memosContent; ?></div>
<?php if ($memosImages): ?>
<div style="display:grid;grid-template-columns:repeat(3,1fr);gap:8px;margin-bottom:12px;"><?php echo $memosImages; ?></div>
<?php endif; ?>
<div style="display:flex;align-items:center;justify-content:space-between;font-size:12px;color:var(--text-muted,#999);">
<span><i class="glyphicon glyphicon-time"></i> <?php echo $memosTimeStr; ?></span>
<a href="https://memos网址/explore" style="color:var(--link-color,#576b95);text-decoration:none;">查看更多说说</a>
</div>
</div>
<!-- 灯箱 -->
<div id="memos-lightbox" style="position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.92);display:none;align-items:center;justify-content:center;z-index:99999;cursor:zoom-out;">
<img src="" style="max-width:95%;max-height:95%;border-radius:4px;">
</div>
<script>
(function() {
var card = document.getElementById('memos-card');
var lightbox = document.getElementById('memos-lightbox');
var lightboxImg = lightbox.querySelector('img');
card.addEventListener('click', function(e) {
if (e.target.tagName === 'IMG') {
lightboxImg.src = e.target.src;
lightbox.style.display = 'flex';
document.body.style.overflow = 'hidden';
}
});
lightbox.addEventListener('click', function() {
this.style.display = 'none';
document.body.style.overflow = '';
});
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
lightbox.style.display = 'none';
document.body.style.overflow = '';
}
});
})();
</script>
<?php endif; ?>
<!--memos-->以上适用于0.26.2版本,新版本0.28需要更改$memosImgUrl = 'https://028317.xyz/file/' . $memosR['name'];
6 条评论
求稳,不升。
想要玩memos,固定在一个版本就不要升级,否则
如果再年轻点,肯定也要折腾HUGO去了,现在就是怎么稳定怎么来,以前有什么升级的肯定第一时间升,现在不管啥,都是稳定就行。
memos 之前一直使用,整合到博客中,发布什么的都在其中,蛮好。还花了很长时间来适配新的api 新功能。不过最近又不用了,直接用博客原生的 hugo 来搞,都是瞎折腾,哈哈
打错了,APP发布。
能网站和App同步,真是个不