widgetsAB Cards
directions_walk

Blog Helper · 小程序动态

小程序(免开发)一键设置心情状态、同步微信运动、快速发布说说|时光机|碎语|日记|心情
verified最低支持版本
AB Admin ≥ 2.1.59
person作者
Chrison
event收录时间
2026-10-27 22:38
update最近更新
2026-10-27 22:38
visibility浏览 / 截图
14 次 · 84.9 KB

image效果截图

点击放大
Blog Helper · 小程序动态 截图

code配置代码

这张卡片没有 HTML 内容
/**
 * Blog Helper × AdminBeautify 概要页自定义卡片
 * 展示:微信步数 / 步数日期 / 心情状态(全站最新一条)
 *
 * 数据:ab.db.read 只读博客本地表 blog_helper_wechat、blog_helper_status(push.php 写入)
 * 缓存:ab.data.getCached 全站共享 60 秒;每 5 分钟自动刷新;「刷新」按钮清缓存立即查库
 * 图标:心情 PNG 为白色线稿,置于深色圆角底色块显示;样式均带 bhelper- 前缀避免污染后台
 *
 * card 与 ab 两个变量由 AdminBeautify 注入,无需声明。
 */
(function () {
    var CACHE_KEY = 'bloghelper_summary';   // 键规则 A-Za-z0-9_-:服务端会自动拼 card- 前缀
    var CACHE_TTL = 60 * 1000;              // 缓存 60 秒
    var REFRESH_MS = 5 * 60 * 1000;         // 自动刷新间隔
    var ICON_BASE = window.location.origin + '/usr/plugins/BlogHelper/assets/status/';
    // 后台目录若不是默认的 /admin/,请同步修改下面这行
    var SETTINGS_URL = window.location.origin + '/admin/options-plugin.php?config=BlogHelper';

    var body = card.querySelector('.ab-custom-body') || card;

    // ---- 最少量样式:仅用于心情图标(白色线稿需深色底)与心情块排版,前缀 bhelper- 防冲突 ----
    var style = ab.ui.el('style');
    style.textContent = ''
        + '.bhelper-mood{display:flex;align-items:center;gap:12px;margin-top:8px;padding:10px 12px;border-radius:12px;background:rgba(128,128,128,.08)}'
        + '.bhelper-mood-main{flex:1;min-width:0}'
        + '.bhelper-mood-name{font-weight:600;font-size:14px}'
        + '.bhelper-mood-sub{opacity:.7;font-size:12px;margin-top:2px;word-break:break-all}'
        + '.bhelper-caption{opacity:.65;font-size:12px;margin:12px 0 4px}'
        + '.bhelper-chip{display:inline-flex;align-items:center;justify-content:center;flex:none;width:40px;height:40px;border-radius:12px;background:#4A5568;color:#fff;font-size:20px;line-height:1;overflow:hidden}'
        + '.bhelper-chip img{width:28px;height:28px;object-fit:contain;display:block}';

    // ---- 工具:相对天数 / 时间格式化(自行计算,避免依赖不确定的 ab.format 签名) ----
    function parseDate(s) {
        var d = new Date(String(s || '').replace(/-/g, '/'));
        return isNaN(d.getTime()) ? null : d;
    }
    function pad(n) {
        return n < 10 ? '0' + n : '' + n;
    }
    function fmtNum(n) {
        return (Number(n) || 0).toLocaleString('zh-CN');
    }
    function fmtTime(s) {
        var d = parseDate(s);
        return d ? pad(d.getMonth() + 1) + '月' + pad(d.getDate()) + '日 ' + pad(d.getHours()) + ':' + pad(d.getMinutes()) : String(s || '');
    }
    function relDay(s) {
        var d = parseDate(s);
        if (!d) return '';
        var today = new Date();
        today.setHours(0, 0, 0, 0);
        var that = new Date(d.getTime());
        that.setHours(0, 0, 0, 0);
        var days = Math.round((today - that) / 86400000);
        if (days <= 0) return '今天';
        if (days === 1) return '昨天';
        return days + ' 天前';
    }

    // ---- emoji_id → 图标代码,规则与插件 Api::iconCode 一致:取最后一段,仅限小写字母数字 ----
    function iconCode(emojiId) {
        var id = String(emojiId || '').trim();
        if (id === '' || id === 'emoji') return '';
        var seg = id.indexOf('-') !== -1 ? id.slice(id.lastIndexOf('-') + 1) : id;
        return /^[a-z0-9]+$/.test(seg) ? seg : '';
    }

    // ---- 心情图标:深色圆角底 + 内置白色线稿 PNG;无图标代码时退回 emoji 字符 ----
    function moodChip(row) {
        var chip = ab.ui.el('span', 'bhelper-chip');
        var code = iconCode(row.emoji_id);
        if (code !== '') {
            var img = document.createElement('img');
            img.alt = '心情图标';
            img.loading = 'lazy';
            img.src = ICON_BASE + code + '.png';
            img.onerror = function () {
                if (img.parentNode === chip) chip.removeChild(img);
                chip.textContent = String(row.emoji || '🙂');
            };
            chip.appendChild(img);
        } else {
            chip.textContent = String(row.emoji || '🙂');
        }
        return chip;
    }

    // ---- 取数:两张表各取 created 倒序第一条 ----
    function fetchData() {
        return Promise.all([
            ab.db.read('blog_helper_wechat', { orderBy: { field: 'created', dir: 'desc' }, limit: 1 }),
            ab.db.read('blog_helper_status', { orderBy: { field: 'created', dir: 'desc' }, limit: 1 })
        ]).then(function (results) {
            var stepsRes = results[0] || {};
            var statusRes = results[1] || {};
            return {
                steps: (stepsRes.rows && stepsRes.rows[0]) || null,
                status: (statusRes.rows && statusRes.rows[0]) || null
            };
        });
    }

    // ---- 渲染 ----
    function render(data) {
        var parts = [style];

        if (data.steps) {
            parts.push(ab.ui.hero({
                icon: 'directions_walk',
                label: '微信步数',
                value: fmtNum(data.steps.steps),
                hint: '同步于 ' + fmtTime(data.steps.created)
            }));
            // 注意:ab.ui.row 不渲染 hint 字段,相对天数并入标签
            var rel = relDay(data.steps.step_date);
            parts.push(ab.ui.rows([
                { icon: 'event', label: rel ? '步数日期 · ' + rel : '步数日期', value: String(data.steps.step_date || '—') }
            ]));
        }

        if (data.status) {
            var box = ab.ui.el('div', 'bhelper-mood');
            var main = ab.ui.el('div', 'bhelper-mood-main');
            main.appendChild(ab.ui.el('div', 'bhelper-mood-name', String(data.status.emoji_name || data.status.emoji || '未知状态')));
            var sub = String(data.status.custom_text || '');
            if (sub === '') sub = '同步于 ' + fmtTime(data.status.created);
            main.appendChild(ab.ui.el('div', 'bhelper-mood-sub', sub));
            box.appendChild(moodChip(data.status));
            box.appendChild(main);
            parts.push(ab.ui.el('div', 'bhelper-caption', '心情状态'));
            parts.push(box);
        }

        if (!data.steps && !data.status) {
            parts.push(ab.ui.empty({
                icon: 'directions_walk',
                text: '暂无数据',
                hint: '在小程序「发射」一次步数或心情后即可展示'
            }));
        }

        parts.push(ab.ui.actions([
            ab.ui.button({ text: '刷新', icon: 'refresh', onClick: function () { refresh(); } })
        ]));

        // 关键:直接传数组。ab.ui.mount 的 append 只接受元素节点(nodeType===1)、
        // 字符串和数组——DocumentFragment(nodeType 11)会被静默丢弃,导致卡片空白
        ab.ui.mount(body, parts);

        ab.ui.footer(card, { text: 'Blog Helper · 插件设置', href: SETTINGS_URL, icon: 'settings' });
    }

    function fail(err) {
        var msg = err && err.message ? err.message : '未知错误';
        ab.ui.mount(body, ab.ui.notice({
            icon: 'error',
            text: '读取失败:' + msg + '(请确认 BlogHelper 插件已启用且数据表存在)',
            tone: 'danger'
        }));
    }

    var notifyAfterLoad = false;

    function load(showSkeleton) {
        // 占位:请求前与缓存未命中期间显示骨架
        if (showSkeleton !== false) {
            ab.ui.mount(body, ab.ui.rows([
                { icon: 'directions_walk', label: '微信步数', value: '—', skeleton: true },
                { icon: 'event', label: '步数日期', value: '—', skeleton: true },
                { icon: 'mood', label: '心情状态', value: '—', skeleton: true }
            ]));
        }
        ab.data.getCached(CACHE_KEY, CACHE_TTL, fetchData, { scope: 'site' })
            .then(function (data) {
                render(data);
                if (notifyAfterLoad) {
                    notifyAfterLoad = false;
                    ab.notify('已刷新', 'success');
                }
            })
            .catch(fail);
    }

    function refresh() {
        ab.data.clearCached(CACHE_KEY, { scope: 'site' });
        notifyAfterLoad = true;
        load(true);
    }

    load();
    ab.interval(function () { load(false); }, REFRESH_MS);   // 卡片销毁时由 AB 自动清理
})();

download_for_offline如何导入

Typecho 后台 → 控制台 → 插件 → AB Admin(AdminBeautify)→「概要页卡片设置」
找到 自定义卡片 区域,打开总开关(默认关闭)
点 添加卡片,按下表填写:
图标 directions_walk
卡片标题 Blog Helper · 小程序动态
HTML 内容 (留空)
JavaScript 见上方「JavaScript」标签页
点右下角 保存设置,回到概要页即可看到卡片(顺序可在排序列表里拖动调整)

shield安全信息

verified_user 扫描通过
database数据来源
databaseab.api(blog_helper_wechat)databaseab.api(blog_helper_status)
widgets使用的内置组件
ab.ui.elab.ui.heroab.ui.rowab.ui.rowsab.ui.emptyab.ui.actionsab.ui.buttonab.ui.mountab.ui.footerab.ui.notice
warning
卡片 JavaScript 会在你的后台页面里执行。本站已做敏感信息、站外请求与越权操作的自动扫描 + 人工审核,但仍请在使用前自行阅读上方代码;发现可疑卡片请到 GitHub 反馈。