qq-bot-roost 1.1.8 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -9
- package/docs/images/qq-group.jpg +0 -0
- package/docs//345/233/236/346/273/232/351/242/204/346/241/210.md +6 -6
- package/package.json +1 -1
- package/src/hashdir.js +10 -0
- package/src/main.js +28 -0
- package/src/markdown.js +33 -9
- package/src/web/media.js +232 -0
- package/src/web/server.js +43 -0
- package/web/app.js +777 -159
- package/web/index.html +125 -4
- package/web/qq-group.jpg +0 -0
- package/web/style.css +343 -17
package/web/app.js
CHANGED
|
@@ -294,6 +294,7 @@ async function selectBot(id) {
|
|
|
294
294
|
if (tab === 'delivery') loadDelivery();
|
|
295
295
|
if (tab === 'groups') loadGroups();
|
|
296
296
|
if (tab === 'roles') { loadRoles(); loadPeople(); }
|
|
297
|
+
if (tab === 'media') loadMedia();
|
|
297
298
|
if (tab === 'stats') loadStats();
|
|
298
299
|
// 体检是按机器人做的,切了要重查
|
|
299
300
|
loadDoctor();
|
|
@@ -1357,6 +1358,29 @@ function showPageError(name, err) {
|
|
|
1357
1358
|
}
|
|
1358
1359
|
|
|
1359
1360
|
/** 包一层:同步抛错和未处理的 Promise 都收敛到 showPageError */
|
|
1361
|
+
/**
|
|
1362
|
+
* 插件页的子页签(已安装 / 市场)。
|
|
1363
|
+
*
|
|
1364
|
+
* 放在 activateTab 定义之前,让「进插件页先回到已安装」的调用点在同一个函数里。
|
|
1365
|
+
* 这里只切显隐和按钮状态,不碰 MARKET——市场数据由 activateTab 之后单独安排
|
|
1366
|
+
* 后台预拉,两者职责分开。
|
|
1367
|
+
*
|
|
1368
|
+
* 属性用 data-plugin-subtab,不用 data-tab:后者会被全局 activateTab() 抢走。
|
|
1369
|
+
*/
|
|
1370
|
+
let activeSubtab = 'installed';
|
|
1371
|
+
function setPluginSubtab(which) {
|
|
1372
|
+
activeSubtab = which === 'market' ? 'market' : 'installed';
|
|
1373
|
+
const installed = $('#installedBox');
|
|
1374
|
+
const market = $('#marketBox');
|
|
1375
|
+
if (installed) installed.hidden = activeSubtab !== 'installed';
|
|
1376
|
+
if (market) market.hidden = activeSubtab !== 'market';
|
|
1377
|
+
$$('[data-plugin-subtab]').forEach((b) => {
|
|
1378
|
+
const on = b.dataset.pluginSubtab === activeSubtab;
|
|
1379
|
+
b.classList.toggle('active', on);
|
|
1380
|
+
b.setAttribute('aria-selected', on ? 'true' : 'false');
|
|
1381
|
+
});
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1360
1384
|
function safeLoad(name, fn) {
|
|
1361
1385
|
try {
|
|
1362
1386
|
const r = fn();
|
|
@@ -1391,8 +1415,11 @@ function activateTab(name) {
|
|
|
1391
1415
|
$$('.panel').forEach((p) => p.classList.toggle('active', p.id === `tab-${name}`));
|
|
1392
1416
|
// 插件指标只在插件页轮询,切走就停——别在后台白耗请求
|
|
1393
1417
|
if (name === 'plugins') {
|
|
1418
|
+
// 每次进插件页先回到「已安装」;市场数据在后台预拉,不阻塞列表渲染
|
|
1419
|
+
setPluginSubtab('installed');
|
|
1394
1420
|
safeLoad('plugins', loadPlugins);
|
|
1395
1421
|
startPluginDashPolling();
|
|
1422
|
+
safeLoad('market', () => loadMarket({ reveal: false }));
|
|
1396
1423
|
} else {
|
|
1397
1424
|
stopPluginDashPolling();
|
|
1398
1425
|
}
|
|
@@ -1436,6 +1463,7 @@ function activateTab(name) {
|
|
|
1436
1463
|
safeLoad('roles', loadPeople);
|
|
1437
1464
|
}
|
|
1438
1465
|
if (name === 'identity') safeLoad('identity', loadIdentity);
|
|
1466
|
+
if (name === 'media') safeLoad('media', loadMedia);
|
|
1439
1467
|
if (name === 'notify') safeLoad('notify', loadNotify);
|
|
1440
1468
|
if (name === 'onebot') safeLoad('onebot', loadOnebot);
|
|
1441
1469
|
// 纯展示同步:侧栏 / 底栏高亮、面包屑、抽屉收起。不碰任何数据加载。
|
|
@@ -3097,6 +3125,74 @@ function renderUpdateTip(upd) {
|
|
|
3097
3125
|
if (lnk) lnk.addEventListener('click', () => updateAllFromMarket());
|
|
3098
3126
|
}
|
|
3099
3127
|
|
|
3128
|
+
/* ---------------- 插件列表:视图偏好 / 筛选工具 ---------------- */
|
|
3129
|
+
|
|
3130
|
+
/** 视图偏好键:和侧栏紧凑模式同一套 localStorage 范式,隐私模式下静默失败 */
|
|
3131
|
+
const PLUGIN_VIEW_KEY = 'roost_plugin_view';
|
|
3132
|
+
const PLUGIN_VIEWS = ['list', 'grid', 'large'];
|
|
3133
|
+
|
|
3134
|
+
/** 已安装列表的视图状态;all/dash/updates 是缓存,筛选排序只重渲染不请求 */
|
|
3135
|
+
const PLUGINS = { all: [], dash: {}, updates: {}, q: '', status: '', sort: '' };
|
|
3136
|
+
|
|
3137
|
+
/** 输入防抖:搜索框用,避免每次按键都重画整个列表 */
|
|
3138
|
+
function debounce(fn, ms = 180) {
|
|
3139
|
+
let timer;
|
|
3140
|
+
return (...args) => {
|
|
3141
|
+
clearTimeout(timer);
|
|
3142
|
+
timer = setTimeout(() => fn(...args), ms);
|
|
3143
|
+
};
|
|
3144
|
+
}
|
|
3145
|
+
|
|
3146
|
+
/** 版本比较:a>b 为 1,a<b 为 -1,相等 0。按 . 和 - 分段,纯数字段按数值比 */
|
|
3147
|
+
function compareVersion(a, b) {
|
|
3148
|
+
const pa = String(a || '0').split(/[.-]/);
|
|
3149
|
+
const pb = String(b || '0').split(/[.-]/);
|
|
3150
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
3151
|
+
const x = pa[i] ?? '0';
|
|
3152
|
+
const y = pb[i] ?? '0';
|
|
3153
|
+
const nx = Number(x);
|
|
3154
|
+
const ny = Number(y);
|
|
3155
|
+
if (Number.isFinite(nx) && Number.isFinite(ny)) {
|
|
3156
|
+
if (nx !== ny) return nx > ny ? 1 : -1;
|
|
3157
|
+
} else if (x !== y) {
|
|
3158
|
+
return x > y ? 1 : -1;
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
return 0;
|
|
3162
|
+
}
|
|
3163
|
+
|
|
3164
|
+
/** 状态归一:筛选、排序、徽章共用一份口径 */
|
|
3165
|
+
function pluginState(p) {
|
|
3166
|
+
if (p.error || (p.runtimeErrors && p.runtimeErrors.length)) return 'error';
|
|
3167
|
+
if (!p.enabled) return 'disabled';
|
|
3168
|
+
if (p.loaded) return 'running';
|
|
3169
|
+
return 'idle';
|
|
3170
|
+
}
|
|
3171
|
+
const PLUGIN_STATE_ORDER = ['running', 'idle', 'disabled', 'error'];
|
|
3172
|
+
const PLUGIN_STATE_TEXT = {
|
|
3173
|
+
running: '运行中', idle: '未加载', disabled: '已禁用', error: '出错',
|
|
3174
|
+
};
|
|
3175
|
+
|
|
3176
|
+
/** 切换视图:只改容器 class,不重建卡片、不重新请求,滚动位置原样保留 */
|
|
3177
|
+
function applyPluginView() {
|
|
3178
|
+
let view = 'grid';
|
|
3179
|
+
try {
|
|
3180
|
+
const saved = localStorage.getItem(PLUGIN_VIEW_KEY);
|
|
3181
|
+
if (PLUGIN_VIEWS.includes(saved)) view = saved;
|
|
3182
|
+
} catch {}
|
|
3183
|
+
['pluginList', 'marketList'].forEach((id) => {
|
|
3184
|
+
const el = document.getElementById(id);
|
|
3185
|
+
if (!el) return;
|
|
3186
|
+
el.classList.remove('view-list', 'view-grid', 'view-large');
|
|
3187
|
+
el.classList.add(`view-${view}`);
|
|
3188
|
+
});
|
|
3189
|
+
document.querySelectorAll('.view-btn').forEach((btn) => {
|
|
3190
|
+
const on = btn.dataset.view === view;
|
|
3191
|
+
btn.classList.toggle('on', on);
|
|
3192
|
+
btn.setAttribute('aria-pressed', String(on));
|
|
3193
|
+
});
|
|
3194
|
+
}
|
|
3195
|
+
|
|
3100
3196
|
/**
|
|
3101
3197
|
* 把「可更新」标记补到**已经画好的**卡片上——不重建卡片。
|
|
3102
3198
|
*
|
|
@@ -3107,6 +3203,8 @@ function renderUpdateTip(upd) {
|
|
|
3107
3203
|
* 否则更新完还一直挂着「可更新 4.4.6」,看着像没成功。
|
|
3108
3204
|
*/
|
|
3109
3205
|
function applyUpdateMarks(upd) {
|
|
3206
|
+
// 结果存进缓存:筛选/排序重渲染时,「可更新」标记和按钮不能丢
|
|
3207
|
+
PLUGINS.updates = Object.fromEntries((upd.updates || []).map((u) => [u.id, u]));
|
|
3110
3208
|
// 先刷顶部提示:列表可能是空的(插件全卸载了),提示也得跟着清掉
|
|
3111
3209
|
renderUpdateTip(upd);
|
|
3112
3210
|
|
|
@@ -3134,17 +3232,16 @@ function applyUpdateMarks(upd) {
|
|
|
3134
3232
|
continue;
|
|
3135
3233
|
}
|
|
3136
3234
|
|
|
3137
|
-
const
|
|
3138
|
-
if (
|
|
3235
|
+
const title = card.querySelector('.plugin-title');
|
|
3236
|
+
if (title && !title.querySelector('.upd-tag')) {
|
|
3139
3237
|
const tag = document.createElement('span');
|
|
3140
3238
|
tag.className = 'upd-tag';
|
|
3141
3239
|
tag.textContent = `可更新 ${u.latest}`;
|
|
3142
|
-
|
|
3240
|
+
title.appendChild(tag);
|
|
3143
3241
|
}
|
|
3144
3242
|
|
|
3145
|
-
|
|
3146
|
-
|
|
3147
|
-
if (!badge) continue;
|
|
3243
|
+
const actions = card.querySelector('.plugin-actions');
|
|
3244
|
+
if (!actions) continue;
|
|
3148
3245
|
const btn = document.createElement('button');
|
|
3149
3246
|
btn.className = 'btn small primary';
|
|
3150
3247
|
btn.dataset.updateOne = id;
|
|
@@ -3152,53 +3249,89 @@ function applyUpdateMarks(upd) {
|
|
|
3152
3249
|
btn.dataset.to = u.latest;
|
|
3153
3250
|
btn.dataset.name = u.name || id;
|
|
3154
3251
|
btn.textContent = '更新';
|
|
3155
|
-
|
|
3156
|
-
|
|
3252
|
+
// 点击统一走 #pluginList 上的事件委托(见 bindPluginListActions),列表重建也不会重复绑
|
|
3253
|
+
actions.prepend(btn);
|
|
3157
3254
|
}
|
|
3158
3255
|
}
|
|
3159
3256
|
|
|
3160
|
-
|
|
3257
|
+
/**
|
|
3258
|
+
* 按当前筛选/排序重渲染已安装插件。
|
|
3259
|
+
* 只从 PLUGINS 缓存取数,不请求后端——搜索/切状态/排序都是纯前端操作。
|
|
3260
|
+
*/
|
|
3261
|
+
function renderPluginRows() {
|
|
3161
3262
|
const wrap = $('#pluginList');
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
* 现在拆成两趟:这里先把卡片画出来,更新标记由 `applyUpdateMarks` 异步补。
|
|
3171
|
-
*/
|
|
3172
|
-
const [list, dash] = await Promise.all([
|
|
3173
|
-
api('/api/plugins'),
|
|
3174
|
-
api('/api/plugins/dashboard').catch(() => ({})),
|
|
3175
|
-
]);
|
|
3176
|
-
/**
|
|
3177
|
-
* 首屏一律按「没有新版」画:卡片上不会出现「可更新」和「更新」按钮,
|
|
3178
|
-
* 都由第二趟的 applyUpdateMarks 补。这样不会先画错再改,闪一下。
|
|
3179
|
-
*/
|
|
3180
|
-
if (!list.length) {
|
|
3181
|
-
wrap.innerHTML = '<div class="empty">还没有插件,把文件夹放进 plugins/ 即可</div>';
|
|
3182
|
-
// 没有插件就不可能有更新,顺手把上次留下的「N 个有新版本」清掉
|
|
3183
|
-
renderUpdateTip({ updates: [] });
|
|
3184
|
-
return;
|
|
3263
|
+
if (!wrap) return;
|
|
3264
|
+
|
|
3265
|
+
const q = PLUGINS.q.trim().toLowerCase();
|
|
3266
|
+
let rows = PLUGINS.all.filter((p) => {
|
|
3267
|
+
if (q) {
|
|
3268
|
+
const hay = [p.id, p.name, p.shortDesc, p.description, ...(p.tags || [])]
|
|
3269
|
+
.filter(Boolean).join(' ').toLowerCase();
|
|
3270
|
+
if (!hay.includes(q)) return false;
|
|
3185
3271
|
}
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
|
|
3189
|
-
|
|
3272
|
+
if (PLUGINS.status === 'update') return !!PLUGINS.updates[p.id];
|
|
3273
|
+
if (PLUGINS.status) return pluginState(p) === PLUGINS.status;
|
|
3274
|
+
return true;
|
|
3275
|
+
});
|
|
3190
3276
|
|
|
3191
|
-
|
|
3192
|
-
|
|
3193
|
-
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3277
|
+
if (PLUGINS.sort === 'name') {
|
|
3278
|
+
rows.sort((a, b) => String(a.name || a.id).localeCompare(String(b.name || b.id), 'zh-CN'));
|
|
3279
|
+
} else if (PLUGINS.sort === 'status') {
|
|
3280
|
+
rows.sort((a, b) =>
|
|
3281
|
+
PLUGIN_STATE_ORDER.indexOf(pluginState(a)) - PLUGIN_STATE_ORDER.indexOf(pluginState(b)) ||
|
|
3282
|
+
String(a.name || a.id).localeCompare(String(b.name || b.id), 'zh-CN'));
|
|
3283
|
+
} else if (PLUGINS.sort === 'version') {
|
|
3284
|
+
rows.sort((a, b) => compareVersion(b.version, a.version));
|
|
3285
|
+
}
|
|
3286
|
+
|
|
3287
|
+
const count = $('#pluginCount');
|
|
3288
|
+
if (count) count.textContent = `显示 ${rows.length} / ${PLUGINS.all.length}`;
|
|
3289
|
+
|
|
3290
|
+
if (!PLUGINS.all.length) {
|
|
3291
|
+
wrap.innerHTML = '<div class="empty">还没有插件,把文件夹放进 plugins/ 即可</div>';
|
|
3292
|
+
return;
|
|
3293
|
+
}
|
|
3294
|
+
if (!rows.length) {
|
|
3295
|
+
wrap.innerHTML = `<div class="empty">没有匹配的插件,换个关键词或状态试试 ${emptyAction('清除筛选', 'clear-plugins')}</div>`;
|
|
3296
|
+
return;
|
|
3297
|
+
}
|
|
3298
|
+
|
|
3299
|
+
wrap.innerHTML = rows.map((p) => {
|
|
3300
|
+
// 插件自己上报的指标,卡片上最多露 3 个,详情在「设置 → 运行数据」
|
|
3301
|
+
const d = PLUGINS.dash[p.id];
|
|
3302
|
+
const chips = renderDashChips(d?.metrics);
|
|
3303
|
+
const u = PLUGINS.updates[p.id];
|
|
3304
|
+
const state = pluginState(p);
|
|
3305
|
+
const badgeCls = state === 'running' ? 'on' : state === 'disabled' ? 'off' : '';
|
|
3306
|
+
|
|
3307
|
+
return `
|
|
3308
|
+
<div class="plugin" data-id="${escapeHtml(p.id)}" data-name="${escapeHtml(p.name || p.id)}" data-state="${state}">
|
|
3309
|
+
<div class="plugin-body">
|
|
3310
|
+
<div class="plugin-head">
|
|
3311
|
+
<div class="plugin-title">
|
|
3312
|
+
${
|
|
3313
|
+
p.icon
|
|
3314
|
+
? `<img class="plugin-icon" src="${escapeHtml(p.icon)}" alt="" loading="lazy">`
|
|
3315
|
+
: ''
|
|
3316
|
+
}<span class="name">${escapeHtml(p.name || p.id)}</span>
|
|
3317
|
+
<span class="hint plugin-ver">v${escapeHtml(p.version || '0.0.0')}</span>
|
|
3318
|
+
${u ? `<span class="upd-tag">可更新 ${escapeHtml(u.latest)}</span>` : ''}
|
|
3319
|
+
</div>
|
|
3320
|
+
<button type="button" class="plugin-toggle${p.enabled ? ' on' : ''}" role="switch"
|
|
3321
|
+
aria-checked="${p.enabled ? 'true' : 'false'}" data-act="${p.enabled ? 'disable' : 'enable'}"
|
|
3322
|
+
title="${p.enabled ? '点击禁用' : '点击启用'} ${escapeHtml(p.name || p.id)}"
|
|
3323
|
+
aria-label="${p.enabled ? '禁用' : '启用'}插件 ${escapeHtml(p.name || p.id)}">
|
|
3324
|
+
<span class="plugin-toggle-knob"></span>
|
|
3325
|
+
</button>
|
|
3199
3326
|
</div>
|
|
3200
|
-
|
|
3201
|
-
|
|
3327
|
+
${
|
|
3328
|
+
(p.shortDesc || p.description)
|
|
3329
|
+
? `<div class="plugin-desc">${escapeHtml(p.shortDesc || p.description)}</div>`
|
|
3330
|
+
: ''
|
|
3331
|
+
}
|
|
3332
|
+
<div class="plugin-meta">
|
|
3333
|
+
<span class="badge ${badgeCls} plugin-status" title="运行状态">${PLUGIN_STATE_TEXT[state]}</span>
|
|
3334
|
+
<span class="plugin-id">${escapeHtml(p.id)}</span>
|
|
3202
3335
|
<span class="kind-tag ${p.kind === 'external' ? 'ext' : 'int'}">
|
|
3203
3336
|
${p.kind === 'external' ? '外部 · ' + escapeHtml(p.runtime) : '内建 · node'}
|
|
3204
3337
|
</span>
|
|
@@ -3209,7 +3342,6 @@ async function loadPlugins() {
|
|
|
3209
3342
|
: ''
|
|
3210
3343
|
}
|
|
3211
3344
|
${renderDecl(p)}
|
|
3212
|
-
${(p.shortDesc || p.description) ? '<br>' + escapeHtml(p.shortDesc || p.description) : ''}
|
|
3213
3345
|
</div>
|
|
3214
3346
|
${chips ? `<div class="plugin-metrics">${chips}</div>` : ''}
|
|
3215
3347
|
${
|
|
@@ -3235,62 +3367,99 @@ async function loadPlugins() {
|
|
|
3235
3367
|
}
|
|
3236
3368
|
${p.menuMissing ? '<div class="err">未提供指令菜单,用户看不到它能做什么</div>' : ''}
|
|
3237
3369
|
</div>
|
|
3238
|
-
<
|
|
3239
|
-
${
|
|
3240
|
-
|
|
3241
|
-
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3370
|
+
<div class="plugin-actions">
|
|
3371
|
+
${
|
|
3372
|
+
u
|
|
3373
|
+
? `<button class="btn small primary" data-update-one="${escapeHtml(p.id)}" data-from="${escapeHtml(u.current)}" data-to="${escapeHtml(u.latest)}" data-name="${escapeHtml(u.name || p.id)}">更新</button>`
|
|
3374
|
+
: ''
|
|
3375
|
+
}
|
|
3376
|
+
<button class="btn small" data-act="reload">重载</button>
|
|
3377
|
+
<button class="btn small" data-act="panel">设置</button>
|
|
3378
|
+
<button class="btn small danger" data-act="remove" title="删除插件代码,数据保留">卸载</button>
|
|
3379
|
+
</div>
|
|
3247
3380
|
</div>`;
|
|
3248
|
-
|
|
3249
|
-
|
|
3250
|
-
|
|
3251
|
-
|
|
3252
|
-
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
});
|
|
3277
|
-
if (!ok) return;
|
|
3278
|
-
}
|
|
3381
|
+
}).join('');
|
|
3382
|
+
|
|
3383
|
+
bindPluginListActions(wrap);
|
|
3384
|
+
// 缓存里已有更新数据时,同步标记到新画的卡片(第二趟尚未回来也可能发生)
|
|
3385
|
+
if (Object.keys(PLUGINS.updates).length) applyUpdateMarks({ updates: Object.values(PLUGINS.updates) });
|
|
3386
|
+
}
|
|
3387
|
+
|
|
3388
|
+
/** 事件委托:只在容器上绑一次,列表重建后按钮照样能用 */
|
|
3389
|
+
let pluginListBound = false;
|
|
3390
|
+
function bindPluginListActions(wrap) {
|
|
3391
|
+
if (pluginListBound) return;
|
|
3392
|
+
pluginListBound = true;
|
|
3393
|
+
wrap.addEventListener('click', async (e) => {
|
|
3394
|
+
const upd = e.target.closest('[data-update-one]');
|
|
3395
|
+
if (upd) {
|
|
3396
|
+
updateFromMarket(upd.dataset.updateOne, upd);
|
|
3397
|
+
return;
|
|
3398
|
+
}
|
|
3399
|
+
const btn = e.target.closest('button[data-act]');
|
|
3400
|
+
if (!btn) return;
|
|
3401
|
+
const card = btn.closest('.plugin');
|
|
3402
|
+
if (!card) return;
|
|
3403
|
+
const id = card.dataset.id;
|
|
3404
|
+
// 「设置」不是启停操作,走独立弹窗
|
|
3405
|
+
if (btn.dataset.act === 'panel') {
|
|
3406
|
+
openPluginPanel(id);
|
|
3407
|
+
return;
|
|
3408
|
+
}
|
|
3279
3409
|
|
|
3280
|
-
|
|
3281
|
-
|
|
3282
|
-
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3287
|
-
}
|
|
3410
|
+
/**
|
|
3411
|
+
* 卸载是不可逆的(代码目录直接删),必须先问清楚。
|
|
3412
|
+
* 数据目录不删——用户重装后还想接着用存档。
|
|
3413
|
+
*/
|
|
3414
|
+
if (btn.dataset.act === 'remove') {
|
|
3415
|
+
const name = card.dataset.name || id;
|
|
3416
|
+
const ok = await confirmDialog({
|
|
3417
|
+
title: `卸载插件「${name}」`,
|
|
3418
|
+
message:
|
|
3419
|
+
`会删除 <code>plugins/${escapeHtml(id)}</code> 整个目录,插件代码就没了。\n\n` +
|
|
3420
|
+
'它的<b>数据会保留</b>(群档案、插件存档都在 data/ 里),重装之后还能接着用。\n\n' +
|
|
3421
|
+
'确定卸载?',
|
|
3422
|
+
okText: '卸载',
|
|
3423
|
+
danger: true,
|
|
3288
3424
|
});
|
|
3289
|
-
|
|
3425
|
+
if (!ok) return;
|
|
3426
|
+
}
|
|
3290
3427
|
|
|
3291
|
-
|
|
3428
|
+
btn.disabled = true;
|
|
3429
|
+
try {
|
|
3430
|
+
await api(`/api/plugins/${encodeURIComponent(id)}/${btn.dataset.act}`, { method: 'POST' });
|
|
3431
|
+
await loadPlugins();
|
|
3432
|
+
} catch (e) {
|
|
3433
|
+
alert('操作失败: ' + e.message);
|
|
3434
|
+
btn.disabled = false;
|
|
3435
|
+
}
|
|
3436
|
+
});
|
|
3437
|
+
}
|
|
3438
|
+
|
|
3439
|
+
async function loadPlugins() {
|
|
3440
|
+
// 插件装了/卸了,它注册的面板页面也会变,顺带刷一下标签栏
|
|
3441
|
+
loadPluginPages();
|
|
3442
|
+
try {
|
|
3443
|
+
/**
|
|
3444
|
+
* 只等「列表 + 运行数据」——都是毫秒级。
|
|
3445
|
+
*
|
|
3446
|
+
* 「有没有新版」以前也塞在同一个 `Promise.all` 里,于是整页要陪它等一次市场索引
|
|
3447
|
+
* (国内直连 GitHub 常要 8~11 秒才失败切镜像),表现就是进插件页后整块空白。
|
|
3448
|
+
* 现在拆成两趟:这里先把卡片画出来,更新标记由 `applyUpdateMarks` 异步补。
|
|
3449
|
+
*/
|
|
3450
|
+
const [list, dash] = await Promise.all([
|
|
3451
|
+
api('/api/plugins'),
|
|
3452
|
+
api('/api/plugins/dashboard').catch(() => ({})),
|
|
3453
|
+
]);
|
|
3454
|
+
PLUGINS.all = list || [];
|
|
3455
|
+
PLUGINS.dash = dash || {};
|
|
3456
|
+
renderPluginRows();
|
|
3457
|
+
// 没有插件就不可能有更新,顺手把上次留下的「N 个有新版本」清掉
|
|
3458
|
+
if (!PLUGINS.all.length) renderUpdateTip({ updates: [] });
|
|
3292
3459
|
} catch (e) {
|
|
3293
|
-
wrap
|
|
3460
|
+
const wrap = $('#pluginList');
|
|
3461
|
+
if (wrap) wrap.innerHTML = `<div class="empty">加载失败: ${escapeHtml(e.message)}</div>`;
|
|
3462
|
+
return;
|
|
3294
3463
|
}
|
|
3295
3464
|
|
|
3296
3465
|
/**
|
|
@@ -4025,48 +4194,64 @@ async function testMirrors() {
|
|
|
4025
4194
|
}
|
|
4026
4195
|
}
|
|
4027
4196
|
|
|
4028
|
-
|
|
4197
|
+
/**
|
|
4198
|
+
* 拉市场数据并渲染。reveal=false 用于进插件页时的后台预拉:只填数据,不切页签。
|
|
4199
|
+
* 在途请求复用(loading),成功过的用缓存(loaded),只有 force 才真重拉。
|
|
4200
|
+
*/
|
|
4201
|
+
async function loadMarket({ reveal = true, force = false } = {}) {
|
|
4029
4202
|
const box = $('#marketBox');
|
|
4030
4203
|
const list = $('#marketList');
|
|
4031
4204
|
const meta = $('#marketMeta');
|
|
4032
|
-
box.hidden = false;
|
|
4205
|
+
if (reveal) box.hidden = false;
|
|
4206
|
+
if (MARKET.loading && !force) return MARKET.loading;
|
|
4207
|
+
if (MARKET.loaded && !force) return;
|
|
4208
|
+
|
|
4033
4209
|
meta.textContent = '获取中…';
|
|
4034
4210
|
list.innerHTML = '<div class="empty">正在从 GitHub 获取插件列表…</div>';
|
|
4035
|
-
//
|
|
4211
|
+
// 下载源选择器:第一次拉数据时就准备好,切到市场页签不用再等
|
|
4036
4212
|
if (!MIRRORS) loadMirrorBar();
|
|
4037
4213
|
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
meta.textContent = `${d.repo} · 更新于 ${d.updated || '未知'}`;
|
|
4048
|
-
// 网络不通时后端会退回本地缓存,得让用户知道这是旧数据
|
|
4049
|
-
if (d.stale) {
|
|
4050
|
-
meta.innerHTML =
|
|
4051
|
-
`${escapeHtml(d.repo)} · <span class="stale-tip">离线缓存(${escapeHtml(d.staleAge || '未知')})</span>`;
|
|
4052
|
-
}
|
|
4053
|
-
if (!d.plugins.length) {
|
|
4054
|
-
MARKET.all = [];
|
|
4055
|
-
list.innerHTML = '<div class="empty">市场里还没有插件</div>';
|
|
4056
|
-
return;
|
|
4057
|
-
}
|
|
4214
|
+
const task = (async () => {
|
|
4215
|
+
let d;
|
|
4216
|
+
try {
|
|
4217
|
+
d = await api('/api/market');
|
|
4218
|
+
} catch (e) {
|
|
4219
|
+
meta.textContent = '获取失败';
|
|
4220
|
+
list.innerHTML = `<div class="empty">${escapeHtml(e.message)}</div>`;
|
|
4221
|
+
return; // 不置 loaded:切到市场页签时会再试一次
|
|
4222
|
+
}
|
|
4058
4223
|
|
|
4059
|
-
|
|
4060
|
-
|
|
4224
|
+
meta.textContent = `${d.repo} · 更新于 ${d.updated || '未知'}`;
|
|
4225
|
+
// 网络不通时后端会退回本地缓存,得让用户知道这是旧数据
|
|
4226
|
+
if (d.stale) {
|
|
4227
|
+
meta.innerHTML =
|
|
4228
|
+
`${escapeHtml(d.repo)} · <span class="stale-tip">离线缓存(${escapeHtml(d.staleAge || '未知')})</span>`;
|
|
4229
|
+
}
|
|
4230
|
+
if (!d.plugins.length) {
|
|
4231
|
+
MARKET.all = [];
|
|
4232
|
+
list.innerHTML = '<div class="empty">市场里还没有插件</div>';
|
|
4233
|
+
MARKET.loaded = true;
|
|
4234
|
+
return;
|
|
4235
|
+
}
|
|
4061
4236
|
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4237
|
+
const upd = {};
|
|
4238
|
+
for (const u of d.updates || []) upd[u.id] = u;
|
|
4239
|
+
MARKET.all = d.plugins;
|
|
4240
|
+
MARKET.upd = upd;
|
|
4241
|
+
MARKET.loaded = true;
|
|
4242
|
+
buildMarketCats();
|
|
4243
|
+
renderMarketRows();
|
|
4244
|
+
})();
|
|
4245
|
+
MARKET.loading = task;
|
|
4246
|
+
try {
|
|
4247
|
+
await task;
|
|
4248
|
+
} finally {
|
|
4249
|
+
MARKET.loading = null;
|
|
4250
|
+
}
|
|
4066
4251
|
}
|
|
4067
4252
|
|
|
4068
4253
|
/** 市场数据与筛选状态 */
|
|
4069
|
-
const MARKET = { all: [], upd: {}, q: '', tag: '' };
|
|
4254
|
+
const MARKET = { all: [], upd: {}, q: '', tag: '', sort: '', loaded: false, loading: null };
|
|
4070
4255
|
|
|
4071
4256
|
/** 搜索框和标签筛选都在前端做——索引就几十条,没必要每次都请求 */
|
|
4072
4257
|
function matchMarket(p) {
|
|
@@ -4150,6 +4335,18 @@ function renderMarketRows() {
|
|
|
4150
4335
|
const shown = MARKET.all.filter(matchMarket);
|
|
4151
4336
|
syncCatChips();
|
|
4152
4337
|
|
|
4338
|
+
// 排序只动Shown数组,不改 MARKET.all(分类 chip 的计数依赖原始顺序)
|
|
4339
|
+
if (MARKET.sort === 'name') {
|
|
4340
|
+
shown.sort((a, b) => String(a.name || a.id).localeCompare(String(b.name || b.id), 'zh-CN'));
|
|
4341
|
+
} else if (MARKET.sort === 'version') {
|
|
4342
|
+
shown.sort((a, b) => compareVersion(b.version, a.version));
|
|
4343
|
+
} else if (MARKET.sort === 'added') {
|
|
4344
|
+
shown.sort((a, b) => String(b.addedAt || '').localeCompare(String(a.addedAt || '')));
|
|
4345
|
+
}
|
|
4346
|
+
|
|
4347
|
+
const count = $('#marketCount');
|
|
4348
|
+
if (count) count.textContent = `显示 ${shown.length} / ${MARKET.all.length}`;
|
|
4349
|
+
|
|
4153
4350
|
if (!shown.length) {
|
|
4154
4351
|
list.innerHTML = `<div class="empty">没有匹配的插件${
|
|
4155
4352
|
MARKET.q || MARKET.tag ? ',换个关键词或分类试试' : ''
|
|
@@ -4163,31 +4360,45 @@ function renderMarketRows() {
|
|
|
4163
4360
|
const u = upd[p.id];
|
|
4164
4361
|
return `
|
|
4165
4362
|
<div class="plugin">
|
|
4166
|
-
<div class="
|
|
4167
|
-
<div class="
|
|
4168
|
-
<
|
|
4169
|
-
|
|
4363
|
+
<div class="plugin-body">
|
|
4364
|
+
<div class="plugin-head">
|
|
4365
|
+
<div class="plugin-title">
|
|
4366
|
+
<span class="name">${escapeHtml(p.name || p.id)}</span>
|
|
4367
|
+
<span class="hint plugin-ver">v${escapeHtml(p.version || '0.0.0')}</span>
|
|
4368
|
+
${u ? `<span class="upd-tag">可更新 ${escapeHtml(u.current)} → ${escapeHtml(u.latest)}</span>` : ''}
|
|
4369
|
+
</div>
|
|
4370
|
+
${
|
|
4371
|
+
p.installed
|
|
4372
|
+
? '<span class="badge on plugin-status">已安装</span>'
|
|
4373
|
+
: ''
|
|
4374
|
+
}
|
|
4170
4375
|
</div>
|
|
4171
|
-
|
|
4172
|
-
|
|
4376
|
+
${p.description ? `<div class="plugin-desc">${escapeHtml(p.description)}</div>` : ''}
|
|
4377
|
+
<div class="plugin-meta">
|
|
4378
|
+
<span class="plugin-id">${escapeHtml(p.id)}</span>
|
|
4173
4379
|
<span class="kind-tag ${p.runtime === 'node' ? 'int' : 'ext'}">
|
|
4174
4380
|
${p.runtime === 'node' ? '内建 · node' : '外部 · ' + escapeHtml(p.runtime || '')}
|
|
4175
4381
|
</span>
|
|
4176
4382
|
${verifyTag(p)}
|
|
4177
|
-
${p.author ? `<span class="hint">by ${escapeHtml(p.author)}</span>` : ''}
|
|
4178
|
-
${
|
|
4179
|
-
${p.description ? '<br>' + escapeHtml(p.description) : ''}
|
|
4180
|
-
${(p.tags || []).length ? '<br><span class="hint">' + p.tags.map(escapeHtml).join(' / ') + '</span>' : ''}
|
|
4181
|
-
${u ? `<br><span class="hint">本地版本 v${escapeHtml(u.current)}</span>` : ''}
|
|
4383
|
+
${p.author ? `<span class="hint plugin-author" title="${escapeHtml('作者 ' + p.author)}">by ${escapeHtml(p.author)}</span>` : ''}
|
|
4384
|
+
${u ? `<span class="hint">本地 v${escapeHtml(u.current)}</span>` : ''}
|
|
4182
4385
|
</div>
|
|
4386
|
+
${
|
|
4387
|
+
p.credits
|
|
4388
|
+
? `<div class="plugin-credits" title="${escapeHtml(p.credits)}">${escapeHtml(p.credits)}</div>`
|
|
4389
|
+
: ''
|
|
4390
|
+
}
|
|
4391
|
+
${(p.tags || []).length ? `<div class="plugin-tags">${p.tags.map((t) => `<span class="tag">${escapeHtml(t)}</span>`).join('')}</div>` : ''}
|
|
4392
|
+
</div>
|
|
4393
|
+
<div class="plugin-actions">
|
|
4394
|
+
${
|
|
4395
|
+
u
|
|
4396
|
+
? `<button class="btn small primary" data-update="${escapeHtml(p.id)}" data-name="${escapeHtml(p.name || p.id)}" data-from="${escapeHtml(u.current)}" data-to="${escapeHtml(u.latest)}">更新到 ${escapeHtml(u.latest)}</button>`
|
|
4397
|
+
: p.installed
|
|
4398
|
+
? ''
|
|
4399
|
+
: `<button class="btn small primary" data-install="${escapeHtml(p.id)}">安装</button>`
|
|
4400
|
+
}
|
|
4183
4401
|
</div>
|
|
4184
|
-
${
|
|
4185
|
-
u
|
|
4186
|
-
? `<button class="btn small primary" data-update="${escapeHtml(p.id)}" data-name="${escapeHtml(p.name || p.id)}" data-from="${escapeHtml(u.current)}" data-to="${escapeHtml(u.latest)}">更新到 ${escapeHtml(u.latest)}</button>`
|
|
4187
|
-
: p.installed
|
|
4188
|
-
? '<span class="badge on">已安装</span>'
|
|
4189
|
-
: `<button class="btn small primary" data-install="${escapeHtml(p.id)}">安装</button>`
|
|
4190
|
-
}
|
|
4191
4402
|
</div>`;
|
|
4192
4403
|
},
|
|
4193
4404
|
)
|
|
@@ -4244,7 +4455,7 @@ async function updateAllFromMarket() {
|
|
|
4244
4455
|
alert(lines.join('\n'));
|
|
4245
4456
|
await loadPlugins();
|
|
4246
4457
|
// 市场页开着才刷,关着就别白等一次索引
|
|
4247
|
-
if (
|
|
4458
|
+
if (activeSubtab === 'market') await loadMarket({ force: true });
|
|
4248
4459
|
} catch (e) {
|
|
4249
4460
|
alert('更新失败: ' + e.message);
|
|
4250
4461
|
}
|
|
@@ -4288,7 +4499,7 @@ async function updateFromMarket(id, btn) {
|
|
|
4288
4499
|
if (btn) btn.textContent = '已更新';
|
|
4289
4500
|
await loadPlugins();
|
|
4290
4501
|
// 市场页开着才刷:它=又一次索引拉取,关着就是白等
|
|
4291
|
-
if (
|
|
4502
|
+
if (activeSubtab === 'market') await loadMarket({ force: true });
|
|
4292
4503
|
if (r.error) alert(r.error);
|
|
4293
4504
|
} catch (e) {
|
|
4294
4505
|
alert('更新失败: ' + e.message);
|
|
@@ -4314,7 +4525,8 @@ async function installFromMarket(id, btn) {
|
|
|
4314
4525
|
try {
|
|
4315
4526
|
const r = await api('/api/market/install', { method: 'POST', body: JSON.stringify({ id }) });
|
|
4316
4527
|
await loadPlugins();
|
|
4317
|
-
|
|
4528
|
+
// 装完要刷新市场数据(「已安装」标记会变),但别强行把用户切到市场页签
|
|
4529
|
+
await loadMarket({ reveal: false, force: true });
|
|
4318
4530
|
await confirmDialog({
|
|
4319
4531
|
title: '安装完成',
|
|
4320
4532
|
message: r.added?.length
|
|
@@ -4345,17 +4557,39 @@ $('#btnExportStats')?.addEventListener('click', () => {
|
|
|
4345
4557
|
window.location.href = `/api/stats/export?${q}`;
|
|
4346
4558
|
});
|
|
4347
4559
|
$('#statsDays')?.addEventListener('change', loadStats);
|
|
4348
|
-
$('#btnRefreshMarket')?.addEventListener('click', loadMarket);
|
|
4560
|
+
$('#btnRefreshMarket')?.addEventListener('click', () => loadMarket({ force: true }));
|
|
4349
4561
|
|
|
4350
|
-
//
|
|
4351
|
-
|
|
4352
|
-
|
|
4353
|
-
renderMarketRows();
|
|
4354
|
-
});
|
|
4562
|
+
// 市场筛选:纯前端过滤,输入防抖,避免每次按键都重画整个网格
|
|
4563
|
+
const onMarketSearch = debounce((v) => { MARKET.q = v; renderMarketRows(); });
|
|
4564
|
+
$('#marketSearch')?.addEventListener('input', (e) => onMarketSearch(e.target.value));
|
|
4355
4565
|
$('#marketCat')?.addEventListener('change', (e) => {
|
|
4356
4566
|
MARKET.tag = e.target.value;
|
|
4357
4567
|
renderMarketRows();
|
|
4358
4568
|
});
|
|
4569
|
+
$('#marketSort')?.addEventListener('change', (e) => {
|
|
4570
|
+
MARKET.sort = e.target.value;
|
|
4571
|
+
renderMarketRows();
|
|
4572
|
+
});
|
|
4573
|
+
|
|
4574
|
+
// 已安装列表筛选:都是从缓存重渲染,不碰后端
|
|
4575
|
+
const onPluginSearch = debounce((v) => { PLUGINS.q = v; renderPluginRows(); });
|
|
4576
|
+
$('#pluginSearch')?.addEventListener('input', (e) => onPluginSearch(e.target.value));
|
|
4577
|
+
$('#pluginStatus')?.addEventListener('change', (e) => {
|
|
4578
|
+
PLUGINS.status = e.target.value;
|
|
4579
|
+
renderPluginRows();
|
|
4580
|
+
});
|
|
4581
|
+
$('#pluginSort')?.addEventListener('change', (e) => {
|
|
4582
|
+
PLUGINS.sort = e.target.value;
|
|
4583
|
+
renderPluginRows();
|
|
4584
|
+
});
|
|
4585
|
+
|
|
4586
|
+
// 视图切换:两份控件共用一份偏好(已安装 / 市场同步切换)
|
|
4587
|
+
document.addEventListener('click', (e) => {
|
|
4588
|
+
const btn = e.target.closest?.('.view-btn');
|
|
4589
|
+
if (!btn || !PLUGIN_VIEWS.includes(btn.dataset.view)) return;
|
|
4590
|
+
try { localStorage.setItem(PLUGIN_VIEW_KEY, btn.dataset.view); } catch {}
|
|
4591
|
+
applyPluginView();
|
|
4592
|
+
});
|
|
4359
4593
|
// 分类 chip 与下拉是同一份状态:点 chip 也把下拉同步过去,键盘用户不受影响
|
|
4360
4594
|
document.addEventListener('click', (e) => {
|
|
4361
4595
|
const chip = e.target.closest?.('.cat-chip');
|
|
@@ -4401,10 +4635,13 @@ $('#proxySelect')?.addEventListener('change', (e) => {
|
|
|
4401
4635
|
saveMirror();
|
|
4402
4636
|
});
|
|
4403
4637
|
$('#proxyCustom')?.addEventListener('change', saveMirror);
|
|
4404
|
-
|
|
4405
|
-
|
|
4406
|
-
|
|
4407
|
-
|
|
4638
|
+
/* 子页签:切到市场时确保数据拉过;切回已安装只切显隐,不产生请求 */
|
|
4639
|
+
$$('[data-plugin-subtab]').forEach((btn) => {
|
|
4640
|
+
btn.addEventListener('click', () => {
|
|
4641
|
+
const to = btn.dataset.pluginSubtab;
|
|
4642
|
+
setPluginSubtab(to);
|
|
4643
|
+
if (to === 'market') safeLoad('market', () => loadMarket({ reveal: true }));
|
|
4644
|
+
});
|
|
4408
4645
|
});
|
|
4409
4646
|
|
|
4410
4647
|
/* ---------------- 插件设置面板 ---------------- */
|
|
@@ -7148,6 +7385,7 @@ function initThemeColor() {
|
|
|
7148
7385
|
|
|
7149
7386
|
// 初始化:先恢复偏好,再补一次外观同步(无 hash 时 activateTab 不会被调用)
|
|
7150
7387
|
applyNavPrefs();
|
|
7388
|
+
applyPluginView();
|
|
7151
7389
|
if (toggle) {
|
|
7152
7390
|
toggle.title = document.body.classList.contains('nav-compact') ? '展开侧栏' : '收起侧栏';
|
|
7153
7391
|
}
|
|
@@ -7179,10 +7417,21 @@ const EMPTY_ACTIONS = {
|
|
|
7179
7417
|
'clear-market': () => {
|
|
7180
7418
|
const q = $('#marketSearch');
|
|
7181
7419
|
const cat = $('#marketCat');
|
|
7420
|
+
const sort = $('#marketSort');
|
|
7182
7421
|
if (q) { q.value = ''; MARKET.q = ''; }
|
|
7183
7422
|
if (cat) { cat.value = ''; MARKET.tag = ''; }
|
|
7423
|
+
if (sort) { sort.value = ''; MARKET.sort = ''; }
|
|
7184
7424
|
renderMarketRows();
|
|
7185
7425
|
},
|
|
7426
|
+
'clear-plugins': () => {
|
|
7427
|
+
const q = $('#pluginSearch');
|
|
7428
|
+
const status = $('#pluginStatus');
|
|
7429
|
+
const sort = $('#pluginSort');
|
|
7430
|
+
if (q) { q.value = ''; PLUGINS.q = ''; }
|
|
7431
|
+
if (status) { status.value = ''; PLUGINS.status = ''; }
|
|
7432
|
+
if (sort) { sort.value = ''; PLUGINS.sort = ''; }
|
|
7433
|
+
renderPluginRows();
|
|
7434
|
+
},
|
|
7186
7435
|
'go-config': () => {
|
|
7187
7436
|
activateTab('config');
|
|
7188
7437
|
history.replaceState(null, '', '#config');
|
|
@@ -9053,3 +9302,372 @@ async function devSave(patch, what) {
|
|
|
9053
9302
|
lastLoggedIn = logged;
|
|
9054
9303
|
}, 4000)?.unref?.();
|
|
9055
9304
|
})();
|
|
9305
|
+
|
|
9306
|
+
/* ---------------- 素材页(机器人级 Markdown 图床) ----------------
|
|
9307
|
+
* 三条接口都在后端做了鉴权 / CSRF / 归属校验,前端只负责选文件、base64 编码
|
|
9308
|
+
* 和展示。用 var 是刻意的:activateTab 可能在脚本执行到这里之前就被 hash 触发,
|
|
9309
|
+
* let/const 会撞 TDZ,safeLoad 把异常吞成「页面加载失败」很难查。
|
|
9310
|
+
*/
|
|
9311
|
+
var MEDIA_STATE = null;
|
|
9312
|
+
function mediaState() {
|
|
9313
|
+
if (!MEDIA_STATE) {
|
|
9314
|
+
MEDIA_STATE = { images: [], cursor: '', total: 0, quota: 100, loading: false, files: [], selected: new Set(), batchMode: false };
|
|
9315
|
+
}
|
|
9316
|
+
return MEDIA_STATE;
|
|
9317
|
+
}
|
|
9318
|
+
|
|
9319
|
+
function mediaTime(sec) {
|
|
9320
|
+
const n = Number(sec) || 0;
|
|
9321
|
+
return n ? new Date(n * 1000).toLocaleString('zh-CN', { hour12: false }) : '—';
|
|
9322
|
+
}
|
|
9323
|
+
|
|
9324
|
+
function mediaLeft(sec) {
|
|
9325
|
+
const n = Number(sec) || 0;
|
|
9326
|
+
if (!n) return '—';
|
|
9327
|
+
const days = Math.ceil((n * 1000 - Date.now()) / 86400000);
|
|
9328
|
+
return days <= 0 ? '已过期' : `剩 ${days} 天`;
|
|
9329
|
+
}
|
|
9330
|
+
|
|
9331
|
+
async function loadMedia({ more = false } = {}) {
|
|
9332
|
+
const st = mediaState();
|
|
9333
|
+
if (st.loading) return;
|
|
9334
|
+
st.loading = true;
|
|
9335
|
+
const listEl = $('#mediaList');
|
|
9336
|
+
const moreBtn = $('#mediaMore');
|
|
9337
|
+
const msgEl = $('#mediaListMsg');
|
|
9338
|
+
if (msgEl) msgEl.textContent = '加载中…';
|
|
9339
|
+
if (moreBtn) moreBtn.disabled = true;
|
|
9340
|
+
try {
|
|
9341
|
+
const q = !more || !st.cursor ? '' : `?cursor=${encodeURIComponent(st.cursor)}`;
|
|
9342
|
+
const r = await api(`/api/media-images${q}`);
|
|
9343
|
+
st.images = more ? [...st.images, ...(r?.images || [])] : (r?.images || []);
|
|
9344
|
+
st.cursor = String(r?.nextCursor || '');
|
|
9345
|
+
st.total = Number(r?.total) || 0;
|
|
9346
|
+
st.quota = Number(r?.quota) || 100;
|
|
9347
|
+
renderMedia();
|
|
9348
|
+
if (msgEl) msgEl.textContent = '';
|
|
9349
|
+
} catch (e) {
|
|
9350
|
+
if (listEl) listEl.innerHTML = `<div class="empty">${escapeHtml(errText(e))}</div>`;
|
|
9351
|
+
if (msgEl) msgEl.textContent = '';
|
|
9352
|
+
} finally {
|
|
9353
|
+
st.loading = false;
|
|
9354
|
+
if (moreBtn) {
|
|
9355
|
+
moreBtn.disabled = false;
|
|
9356
|
+
moreBtn.hidden = !st.cursor;
|
|
9357
|
+
}
|
|
9358
|
+
}
|
|
9359
|
+
}
|
|
9360
|
+
|
|
9361
|
+
/**
|
|
9362
|
+
* 删除成功后立即把卡片从本地摘掉,不等 list 接口重拉:
|
|
9363
|
+
* · loadMedia() 有 loading 闸门,删除时若有别的加载在跑会被直接跳过;
|
|
9364
|
+
* · 官方 list 有同步延迟,马上重拉可能把刚删的又带回来。
|
|
9365
|
+
* 只移除 deleted / missing,failed 的留在列表上让用户看到并重试。
|
|
9366
|
+
*/
|
|
9367
|
+
function dropMediaCards(ids) {
|
|
9368
|
+
const st = mediaState();
|
|
9369
|
+
const gone = new Set(ids);
|
|
9370
|
+
if (!gone.size) return;
|
|
9371
|
+
st.images = st.images.filter((x) => !gone.has(x.imageId));
|
|
9372
|
+
st.total = Math.max(0, st.total - gone.size);
|
|
9373
|
+
for (const id of gone) st.selected.delete(id);
|
|
9374
|
+
renderMedia();
|
|
9375
|
+
}
|
|
9376
|
+
|
|
9377
|
+
function updateMediaSelection() {
|
|
9378
|
+
const st = mediaState();
|
|
9379
|
+
const n = st.selected.size;
|
|
9380
|
+
const batch = st.batchMode;
|
|
9381
|
+
const batchBtn = $('#mediaBatchBtn');
|
|
9382
|
+
const all = $('#mediaSelectAll');
|
|
9383
|
+
const del = $('#mediaDeleteSelected');
|
|
9384
|
+
const cnt = $('#mediaSelCount');
|
|
9385
|
+
const hint = $('#mediaListHint');
|
|
9386
|
+
if (batchBtn) batchBtn.textContent = batch ? '退出批量' : '批量管理';
|
|
9387
|
+
if (all) {
|
|
9388
|
+
all.hidden = !batch;
|
|
9389
|
+
all.textContent = (n && n === st.images.length) ? '取消全选' : '全选';
|
|
9390
|
+
}
|
|
9391
|
+
if (del) {
|
|
9392
|
+
del.hidden = !batch;
|
|
9393
|
+
del.disabled = !n;
|
|
9394
|
+
del.textContent = n ? `删除所选 (${n})` : '删除所选';
|
|
9395
|
+
}
|
|
9396
|
+
if (cnt) { cnt.hidden = !batch; cnt.textContent = `已选 ${n} 张`; }
|
|
9397
|
+
if (hint) {
|
|
9398
|
+
hint.textContent = batch
|
|
9399
|
+
? '批量模式:直接点卡片选中或取消,再点「删除所选」;点「退出批量」返回普通模式。'
|
|
9400
|
+
: '缩略图是平台给的短时效预览链接,只在这里看一眼,不会存到本机。';
|
|
9401
|
+
}
|
|
9402
|
+
}
|
|
9403
|
+
|
|
9404
|
+
function renderMedia() {
|
|
9405
|
+
const list = $('#mediaList');
|
|
9406
|
+
if (!list) return;
|
|
9407
|
+
const st = mediaState();
|
|
9408
|
+
const quota = $('#mediaQuota');
|
|
9409
|
+
if (quota) quota.textContent = `已用 ${st.total} / ${st.quota} 张`;
|
|
9410
|
+
// 切机器人 / 刷新后清掉已不在列表里的选中项
|
|
9411
|
+
const ids = new Set(st.images.map((x) => x.imageId));
|
|
9412
|
+
for (const id of st.selected) if (!ids.has(id)) st.selected.delete(id);
|
|
9413
|
+
list.classList.toggle('batch', st.batchMode);
|
|
9414
|
+
updateMediaSelection();
|
|
9415
|
+
if (!st.images.length) {
|
|
9416
|
+
list.innerHTML = '<div class="empty">还没有素材,先在上面传一张</div>';
|
|
9417
|
+
return;
|
|
9418
|
+
}
|
|
9419
|
+
list.innerHTML = st.images.map((it) => {
|
|
9420
|
+
const id = escapeHtml(it.imageId);
|
|
9421
|
+
const name = escapeHtml(it.fileName || '未命名');
|
|
9422
|
+
const url = escapeHtml(it.url);
|
|
9423
|
+
const md = escapeHtml(``);
|
|
9424
|
+
const picked = st.selected.has(it.imageId) ? ' selected' : '';
|
|
9425
|
+
const thumb = it.previewUrl
|
|
9426
|
+
? `<img class="media-thumb" src="${escapeHtml(it.previewUrl)}" alt="" loading="lazy">`
|
|
9427
|
+
: '<div class="media-thumb"></div>';
|
|
9428
|
+
// 批量模式下不生成卡片按钮:点卡片任意位置都是选中,不会误触到复制 / 单张删除
|
|
9429
|
+
const actions = st.batchMode ? '' : `<div class="media-actions">
|
|
9430
|
+
<button type="button" class="btn small" data-media-copy="url" data-id="${id}">复制引用</button>
|
|
9431
|
+
<button type="button" class="btn small" data-media-copy="md" data-value="${md}">复制 Markdown</button>
|
|
9432
|
+
<button type="button" class="btn small danger" data-media-del="${id}">删除</button>
|
|
9433
|
+
</div>`;
|
|
9434
|
+
return `<div class="media-item${picked}" data-media-id="${id}">
|
|
9435
|
+
${thumb}
|
|
9436
|
+
<div class="media-meta">
|
|
9437
|
+
<div class="media-name">${name}</div>
|
|
9438
|
+
<span class="hint">${mediaTime(it.createdAt)} · ${escapeHtml(mediaLeft(it.expiresAt))}</span>
|
|
9439
|
+
${actions}
|
|
9440
|
+
</div>
|
|
9441
|
+
</div>`;
|
|
9442
|
+
}).join('');
|
|
9443
|
+
// 预览链接是短时效的,过期就留空方块,不要一页碎图标
|
|
9444
|
+
list.querySelectorAll('.media-thumb').forEach((img) => {
|
|
9445
|
+
img.addEventListener('error', () => { img.style.visibility = 'hidden'; });
|
|
9446
|
+
});
|
|
9447
|
+
}
|
|
9448
|
+
|
|
9449
|
+
async function copyMediaText(text) {
|
|
9450
|
+
try {
|
|
9451
|
+
await navigator.clipboard.writeText(text);
|
|
9452
|
+
toast('已复制', 'ok');
|
|
9453
|
+
} catch {
|
|
9454
|
+
const ta = document.createElement('textarea');
|
|
9455
|
+
ta.value = text;
|
|
9456
|
+
ta.style.position = 'absolute';
|
|
9457
|
+
ta.style.left = '-9999px';
|
|
9458
|
+
document.body.appendChild(ta);
|
|
9459
|
+
ta.select();
|
|
9460
|
+
const ok = document.execCommand('copy');
|
|
9461
|
+
ta.remove();
|
|
9462
|
+
toast(ok ? '已复制' : '复制失败,请手动选择', ok ? 'ok' : 'error');
|
|
9463
|
+
}
|
|
9464
|
+
}
|
|
9465
|
+
|
|
9466
|
+
function pickMediaFiles(files) {
|
|
9467
|
+
const st = mediaState();
|
|
9468
|
+
const pick = $('#mediaPick');
|
|
9469
|
+
const btn = $('#mediaUploadBtn');
|
|
9470
|
+
const msg = $('#mediaMsg');
|
|
9471
|
+
st.files = [];
|
|
9472
|
+
if (btn) btn.disabled = true;
|
|
9473
|
+
if (msg) msg.textContent = '';
|
|
9474
|
+
if (!files || !files.length) {
|
|
9475
|
+
if (pick) pick.textContent = '还没选文件';
|
|
9476
|
+
return;
|
|
9477
|
+
}
|
|
9478
|
+
const skipped = [];
|
|
9479
|
+
for (const file of Array.from(files)) {
|
|
9480
|
+
if (!/^image\/(png|jpeg|gif|webp)$/.test(file.type)) {
|
|
9481
|
+
skipped.push(`${file.name}(格式不支持)`);
|
|
9482
|
+
} else if (file.size > 10 * 1024 * 1024) {
|
|
9483
|
+
skipped.push(`${file.name}(${(file.size / 1024 / 1024).toFixed(1)}MB 超限)`);
|
|
9484
|
+
} else {
|
|
9485
|
+
st.files.push(file);
|
|
9486
|
+
}
|
|
9487
|
+
}
|
|
9488
|
+
if (btn) btn.disabled = !st.files.length;
|
|
9489
|
+
if (!st.files.length) {
|
|
9490
|
+
if (pick) pick.textContent = skipped.join('、') || '没有可用图片';
|
|
9491
|
+
return;
|
|
9492
|
+
}
|
|
9493
|
+
const names = st.files.slice(0, 3).map((f) => f.name).join('、');
|
|
9494
|
+
const more = st.files.length > 3 ? ` 等 ${st.files.length} 张` : '';
|
|
9495
|
+
const skipTip = skipped.length ? `;跳过 ${skipped.length} 张:${skipped.slice(0, 2).join('、')}` : '';
|
|
9496
|
+
if (pick) pick.textContent = `已选 ${st.files.length} 张:${names}${more}${skipTip}`;
|
|
9497
|
+
}
|
|
9498
|
+
|
|
9499
|
+
async function uploadMediaFiles() {
|
|
9500
|
+
const st = mediaState();
|
|
9501
|
+
const files = st.files.slice();
|
|
9502
|
+
if (!files.length) return;
|
|
9503
|
+
const btn = $('#mediaUploadBtn');
|
|
9504
|
+
const msg = $('#mediaMsg');
|
|
9505
|
+
if (btn) btn.disabled = true;
|
|
9506
|
+
const done = [];
|
|
9507
|
+
const failed = [];
|
|
9508
|
+
try {
|
|
9509
|
+
for (let i = 0; i < files.length; i++) {
|
|
9510
|
+
const file = files[i];
|
|
9511
|
+
if (msg) msg.textContent = `正在上传 ${i + 1}/${files.length}:${file.name}(申请 → 直传 → 完成)…`;
|
|
9512
|
+
try {
|
|
9513
|
+
const dataUrl = await new Promise((resolve, reject) => {
|
|
9514
|
+
const fr = new FileReader();
|
|
9515
|
+
fr.onload = () => resolve(String(fr.result || ''));
|
|
9516
|
+
fr.onerror = () => reject(new Error('读取图片失败'));
|
|
9517
|
+
fr.readAsDataURL(file);
|
|
9518
|
+
});
|
|
9519
|
+
const data = dataUrl.slice(dataUrl.indexOf(',') + 1);
|
|
9520
|
+
await api('/api/media-images/upload', {
|
|
9521
|
+
method: 'POST',
|
|
9522
|
+
body: JSON.stringify({ name: file.name, data }),
|
|
9523
|
+
});
|
|
9524
|
+
done.push(file.name);
|
|
9525
|
+
} catch (e) {
|
|
9526
|
+
failed.push(`${file.name}:${errText(e)}`);
|
|
9527
|
+
}
|
|
9528
|
+
}
|
|
9529
|
+
st.files = [];
|
|
9530
|
+
const input = $('#mediaFile');
|
|
9531
|
+
if (input) input.value = '';
|
|
9532
|
+
if (msg) msg.textContent = failed.length ? failed.join(';') : '';
|
|
9533
|
+
if ($('#mediaPick')) {
|
|
9534
|
+
$('#mediaPick').textContent = failed.length
|
|
9535
|
+
? `本次 ${done.length} 张成功、${failed.length} 张失败,可重新选择`
|
|
9536
|
+
: `已上传 ${done.length} 张,可以继续选图`;
|
|
9537
|
+
}
|
|
9538
|
+
if (failed.length) {
|
|
9539
|
+
toast(`成功 ${done.length} 张,失败 ${failed.length} 张`, 'error');
|
|
9540
|
+
} else {
|
|
9541
|
+
toast(`已上传 ${done.length} 张`, 'ok');
|
|
9542
|
+
}
|
|
9543
|
+
await loadMedia();
|
|
9544
|
+
} finally {
|
|
9545
|
+
if (btn) btn.disabled = !st.files.length;
|
|
9546
|
+
}
|
|
9547
|
+
}
|
|
9548
|
+
|
|
9549
|
+
async function deleteSelectedMedia() {
|
|
9550
|
+
const st = mediaState();
|
|
9551
|
+
const ids = [...st.selected];
|
|
9552
|
+
if (!ids.length) return;
|
|
9553
|
+
const ok = await confirmDialog({
|
|
9554
|
+
title: '批量删除素材',
|
|
9555
|
+
message: `确定删除选中的 <b>${ids.length}</b> 张吗?删除后 <b>qqbot-media://</b> 引用立即失效;已经发出去的消息不受影响。`,
|
|
9556
|
+
okText: `删除 ${ids.length} 张`,
|
|
9557
|
+
danger: true,
|
|
9558
|
+
});
|
|
9559
|
+
if (!ok) return;
|
|
9560
|
+
const btn = $('#mediaDeleteSelected');
|
|
9561
|
+
const msg = $('#mediaListMsg');
|
|
9562
|
+
if (btn) btn.disabled = true;
|
|
9563
|
+
try {
|
|
9564
|
+
if (msg) msg.textContent = `正在删除 ${ids.length} 张…`;
|
|
9565
|
+
const r = await api('/api/media-images/delete', {
|
|
9566
|
+
method: 'POST',
|
|
9567
|
+
body: JSON.stringify({ imageIds: ids, confirm: true }),
|
|
9568
|
+
});
|
|
9569
|
+
const gone = [
|
|
9570
|
+
...(r?.deleted || []).map((x) => x.imageId),
|
|
9571
|
+
...(r?.missing || []),
|
|
9572
|
+
];
|
|
9573
|
+
st.selected.clear();
|
|
9574
|
+
dropMediaCards(gone); // 先本地摘卡片,立即见效,不等 list 重拉
|
|
9575
|
+
const d = r?.deleted?.length || 0;
|
|
9576
|
+
const m = r?.missing?.length || 0;
|
|
9577
|
+
const f = r?.failed?.length || 0;
|
|
9578
|
+
const tip = [d && `成功 ${d} 张`, m && `${m} 张已不存在`, f && `${f} 张失败`].filter(Boolean).join(',');
|
|
9579
|
+
if (msg) msg.textContent = r?.error || tip;
|
|
9580
|
+
toast(tip || '已删除', (f || m) ? 'error' : 'ok');
|
|
9581
|
+
} catch (e) {
|
|
9582
|
+
if (msg) msg.textContent = errText(e);
|
|
9583
|
+
toast(errText(e), 'error');
|
|
9584
|
+
} finally {
|
|
9585
|
+
updateMediaSelection();
|
|
9586
|
+
}
|
|
9587
|
+
}
|
|
9588
|
+
|
|
9589
|
+
document.addEventListener('click', async (e) => {
|
|
9590
|
+
if (e.target?.closest?.('#mediaBatchBtn')) {
|
|
9591
|
+
const st = mediaState();
|
|
9592
|
+
st.batchMode = !st.batchMode;
|
|
9593
|
+
if (!st.batchMode) st.selected.clear();
|
|
9594
|
+
renderMedia();
|
|
9595
|
+
return;
|
|
9596
|
+
}
|
|
9597
|
+
if (e.target?.closest?.('#mediaSelectAll')) {
|
|
9598
|
+
const st = mediaState();
|
|
9599
|
+
if (st.selected.size === st.images.length) st.selected.clear();
|
|
9600
|
+
else st.images.forEach((x) => st.selected.add(x.imageId));
|
|
9601
|
+
renderMedia();
|
|
9602
|
+
return;
|
|
9603
|
+
}
|
|
9604
|
+
if (e.target?.closest?.('#mediaDeleteSelected')) return deleteSelectedMedia();
|
|
9605
|
+
if (mediaState().batchMode) {
|
|
9606
|
+
const item = e.target?.closest?.('.media-item');
|
|
9607
|
+
if (item) {
|
|
9608
|
+
const st = mediaState();
|
|
9609
|
+
const id = item.dataset.mediaId;
|
|
9610
|
+
if (st.selected.has(id)) st.selected.delete(id);
|
|
9611
|
+
else st.selected.add(id);
|
|
9612
|
+
item.classList.toggle('selected', st.selected.has(id));
|
|
9613
|
+
updateMediaSelection();
|
|
9614
|
+
return;
|
|
9615
|
+
}
|
|
9616
|
+
}
|
|
9617
|
+
const copyBtn = e.target?.closest?.('[data-media-copy]');
|
|
9618
|
+
if (copyBtn) {
|
|
9619
|
+
const kind = copyBtn.dataset.mediaCopy;
|
|
9620
|
+
if (kind === 'md') return copyMediaText(copyBtn.dataset.value || '');
|
|
9621
|
+
const it = mediaState().images.find((x) => x.imageId === copyBtn.dataset.id);
|
|
9622
|
+
if (it?.url) await copyMediaText(it.url);
|
|
9623
|
+
return;
|
|
9624
|
+
}
|
|
9625
|
+
const delBtn = e.target?.closest?.('[data-media-del]');
|
|
9626
|
+
if (delBtn) {
|
|
9627
|
+
const id = delBtn.dataset.mediaDel;
|
|
9628
|
+
const ok = await confirmDialog({
|
|
9629
|
+
title: '删除素材',
|
|
9630
|
+
message: '删除后 <b>qqbot-media://</b> 引用立即失效;已经发出去的消息不受影响。',
|
|
9631
|
+
okText: '删除',
|
|
9632
|
+
danger: true,
|
|
9633
|
+
});
|
|
9634
|
+
if (!ok) return;
|
|
9635
|
+
try {
|
|
9636
|
+
await api('/api/media-images/delete', {
|
|
9637
|
+
method: 'POST',
|
|
9638
|
+
body: JSON.stringify({ imageId: id, confirm: true }),
|
|
9639
|
+
});
|
|
9640
|
+
dropMediaCards([id]);
|
|
9641
|
+
toast('已删除', 'ok');
|
|
9642
|
+
} catch (err) {
|
|
9643
|
+
toast(errText(err), 'error');
|
|
9644
|
+
}
|
|
9645
|
+
return;
|
|
9646
|
+
}
|
|
9647
|
+
if (e.target?.closest?.('#mediaUploadBtn')) return uploadMediaFiles();
|
|
9648
|
+
if (e.target?.closest?.('#mediaRefresh')) return loadMedia();
|
|
9649
|
+
if (e.target?.closest?.('#mediaMore')) return loadMedia({ more: true });
|
|
9650
|
+
});
|
|
9651
|
+
|
|
9652
|
+
const mediaDrop = $('#mediaDrop');
|
|
9653
|
+
if (mediaDrop) {
|
|
9654
|
+
mediaDrop.addEventListener('click', (e) => {
|
|
9655
|
+
// label 自己会触发 file input,别再点一次,否则弹两个选择框
|
|
9656
|
+
if (e.target?.closest?.('label') || e.target?.closest?.('input')) return;
|
|
9657
|
+
$('#mediaFile')?.click();
|
|
9658
|
+
});
|
|
9659
|
+
mediaDrop.addEventListener('dragover', (e) => {
|
|
9660
|
+
e.preventDefault();
|
|
9661
|
+
mediaDrop.classList.add('drag');
|
|
9662
|
+
});
|
|
9663
|
+
mediaDrop.addEventListener('dragleave', () => mediaDrop.classList.remove('drag'));
|
|
9664
|
+
mediaDrop.addEventListener('drop', (e) => {
|
|
9665
|
+
e.preventDefault();
|
|
9666
|
+
mediaDrop.classList.remove('drag');
|
|
9667
|
+
pickMediaFiles(e.dataTransfer?.files);
|
|
9668
|
+
});
|
|
9669
|
+
}
|
|
9670
|
+
$('#mediaFile')?.addEventListener('change', (e) => pickMediaFiles(e.target.files));
|
|
9671
|
+
|
|
9672
|
+
// hash 直达 #media 时,activateTab 可能在本块执行前就跑过了,补一次
|
|
9673
|
+
if (location.hash.slice(1) === 'media') activateTab('media');
|