673 lines
35 KiB
HTML
673 lines
35 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<title>封面 AI 助手 · 生图 / 修图</title>
|
||
<style>
|
||
:root{--bg:#0b0f17;--panel:#121a26;--line:#223047;--txt:#e6edf7;--mut:#8fa3bd;--acc:#38bdf8;--acc2:#a78bfa;--ok:#34d399;--warn:#fbbf24;--err:#f87171}
|
||
*{box-sizing:border-box}
|
||
body{margin:0;background:var(--bg);color:var(--txt);font:15px/1.6 -apple-system,"Noto Sans SC","Microsoft YaHei",system-ui,sans-serif}
|
||
header{padding:18px 22px;border-bottom:1px solid var(--line);display:flex;align-items:center;gap:14px;flex-wrap:wrap;background:linear-gradient(90deg,#0b0f17,#101a2b)}
|
||
h1{font-size:18px;margin:0;font-weight:700}
|
||
.tag{font-size:12px;color:var(--mut);border:1px solid var(--line);border-radius:999px;padding:3px 10px}
|
||
.tag.on{color:var(--ok);border-color:rgba(52,211,153,.4)}
|
||
.tag.off{color:var(--warn);border-color:rgba(251,191,36,.4)}
|
||
.wrap{max-width:1240px;margin:0 auto;padding:20px}
|
||
.tabs{display:flex;gap:8px;margin-bottom:18px;flex-wrap:wrap}
|
||
.tabs button{background:var(--panel);color:var(--mut);border:1px solid var(--line);border-radius:10px;padding:9px 16px;cursor:pointer;font-size:14px}
|
||
.tabs button.on{color:#08131f;background:linear-gradient(90deg,var(--acc),var(--acc2));border-color:transparent;font-weight:700}
|
||
.grid{display:grid;grid-template-columns:1.05fr 1fr;gap:18px}
|
||
@media(max-width:900px){.grid{grid-template-columns:1fr}}
|
||
.card{background:var(--panel);border:1px solid var(--line);border-radius:14px;padding:16px}
|
||
label{display:block;font-size:13px;color:var(--mut);margin:12px 0 6px}
|
||
textarea,select,input[type=text],input[type=password]{width:100%;background:#0e1622;color:var(--txt);border:1px solid var(--line);border-radius:10px;padding:11px 12px;font:inherit}
|
||
textarea{min-height:96px;resize:vertical}
|
||
.chips{display:flex;flex-wrap:wrap;gap:7px;margin-top:7px}
|
||
.chips span{font-size:12.5px;color:var(--mut);border:1px dashed var(--line);border-radius:999px;padding:4px 11px;cursor:pointer}
|
||
.chips span:hover{color:var(--acc);border-color:var(--acc)}
|
||
.btn{display:inline-flex;align-items:center;gap:8px;background:linear-gradient(90deg,var(--acc),var(--acc2));color:#08131f;font-weight:800;border:0;border-radius:10px;padding:12px 22px;cursor:pointer;font-size:15px}
|
||
.btn:disabled{opacity:.55;cursor:not-allowed}
|
||
.btn.ghost{background:transparent;color:var(--txt);border:1px solid var(--line);font-weight:600}
|
||
.btn.sm{padding:9px 14px;font-size:13.5px}
|
||
.row{display:flex;gap:10px;flex-wrap:wrap;align-items:center}
|
||
.hint{font-size:12.5px;color:var(--mut)}
|
||
.status{margin-top:12px;font-size:13.5px;min-height:22px}
|
||
.status.err{color:var(--err)}.status.ok{color:var(--ok)}.status.warn{color:var(--warn)}
|
||
.banner{display:flex;gap:12px;align-items:center;flex-wrap:wrap;background:rgba(251,191,36,.08);border:1px solid rgba(251,191,36,.35);border-radius:12px;padding:12px 14px;margin-bottom:14px;font-size:14px;color:#fde68a}
|
||
.banner a{color:var(--acc);font-weight:700}
|
||
.settings{margin-bottom:18px}
|
||
.settings .sgrid{display:grid;grid-template-columns:1.2fr 1.2fr 1fr;gap:12px}
|
||
@media(max-width:900px){.settings .sgrid{grid-template-columns:1fr}}
|
||
.settings label{margin-top:0}
|
||
.results{display:grid;grid-template-columns:repeat(auto-fill,minmax(220px,1fr));gap:12px;margin-top:14px}
|
||
.shot{background:#0e1622;border:1px solid var(--line);border-radius:12px;overflow:hidden;display:flex;flex-direction:column}
|
||
.shot img{width:100%;display:block;background:#0a0f18}
|
||
.shot .bar{display:flex;gap:6px;padding:8px;flex-wrap:wrap}
|
||
.shot .bar a,.shot .bar button{font-size:12px;color:var(--mut);background:#0b1220;border:1px solid var(--line);border-radius:8px;padding:5px 9px;cursor:pointer;text-decoration:none}
|
||
.shot .bar a:hover,.shot .bar button:hover{color:var(--acc);border-color:var(--acc)}
|
||
.drop{border:2px dashed var(--line);border-radius:12px;padding:26px;text-align:center;color:var(--mut);cursor:pointer}
|
||
.drop.on{border-color:var(--acc);color:var(--acc)}
|
||
.cmp{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-top:12px}
|
||
.cmp figure{margin:0}
|
||
.cmp figcaption{font-size:12px;color:var(--mut);margin-bottom:5px}
|
||
.cmp img{width:100%;border-radius:10px;border:1px solid var(--line)}
|
||
.mini{font-size:12.5px;color:var(--mut);background:#0b1220;border:1px solid var(--line);border-radius:8px;padding:6px 11px;cursor:pointer;text-decoration:none}
|
||
.mini:hover{color:var(--acc);border-color:var(--acc)}
|
||
/* ---- 授权中心:右上角版本 / 公告 ---- */
|
||
.hright{margin-left:auto;display:flex;align-items:center;gap:8px}
|
||
.bell{position:relative;background:#0e1622;border:1px solid var(--line);border-radius:10px;padding:7px 11px;cursor:pointer;font-size:15px;color:var(--txt)}
|
||
.bell:hover{border-color:var(--acc)}
|
||
.bell .dot{position:absolute;top:4px;right:5px;width:7px;height:7px;border-radius:50%;background:var(--warn);display:none}
|
||
.bell .dot.on{display:block}
|
||
.vchip{background:#0e1622;border:1px solid var(--line);border-radius:999px;padding:5px 12px;cursor:pointer;font-size:12.5px;color:var(--mut);font-variant-numeric:tabular-nums}
|
||
.vchip:hover{border-color:var(--acc);color:var(--acc)}
|
||
.repolink{background:#0e1622;border:1px solid var(--line);border-radius:999px;padding:5px 12px;font-size:12.5px;color:var(--mut);text-decoration:none;display:inline-flex;align-items:center}
|
||
.repolink:hover{border-color:var(--acc);color:var(--acc)}
|
||
.vchip.upd{color:var(--warn);border-color:rgba(251,191,36,.55);font-weight:700}
|
||
.annpanel{position:fixed;top:64px;right:16px;width:340px;max-width:calc(100vw - 32px);max-height:60vh;overflow:auto;background:var(--panel);border:1px solid var(--line);border-radius:14px;padding:14px;z-index:60;box-shadow:0 18px 50px rgba(0,0,0,.5)}
|
||
.annpanel .it{border-top:1px solid var(--line);padding:10px 0;font-size:13.5px}
|
||
.annpanel .it:first-of-type{border-top:0}
|
||
.annpanel .it .t{font-weight:700;color:var(--txt)}
|
||
.annpanel .it .c{color:var(--mut);margin-top:4px;white-space:pre-wrap}
|
||
.annpanel .it .m{font-size:11.5px;color:#65788f;margin-top:5px}
|
||
.annpanel .empty{color:var(--mut);font-size:13px;padding:12px 0}
|
||
.modal{position:fixed;inset:0;background:rgba(4,8,14,.66);display:flex;align-items:center;justify-content:center;z-index:70;padding:20px}
|
||
.mbox{background:var(--panel);border:1px solid var(--line);border-radius:14px;padding:18px;max-width:460px;width:100%;max-height:74vh;overflow:auto;box-shadow:0 22px 60px rgba(0,0,0,.55)}
|
||
.mbox .c{margin-top:10px;color:var(--txt);white-space:pre-wrap;font-size:14px}
|
||
.loading{display:inline-block;width:14px;height:14px;border:2px solid #33465f;border-top-color:var(--acc);border-radius:50%;animation:sp .8s linear infinite}
|
||
@keyframes sp{to{transform:rotate(360deg)}}
|
||
footer{padding:16px 22px;color:var(--mut);font-size:12.5px;border-top:1px solid var(--line);margin-top:26px}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header>
|
||
<h1>封面 AI 助手</h1>
|
||
<span class="tag off" id="cfgTag">未配置 API Key</span>
|
||
<span class="tag" id="modelTag">mimo-v2.6-flash</span>
|
||
<span class="tag">生成的图可直接在「米图设计」的素材面板里点用</span>
|
||
<div class="hright">
|
||
<a class="repolink" href="https://gitee.com/jinghui111/cover-ai" target="_blank" rel="noopener" title="查看开源代码 · Gitee">开源</a>
|
||
<button class="bell" id="annBtn" title="公告">🔔<i class="dot" id="annDot"></i></button>
|
||
<button class="vchip" id="verChip" title="版本信息">v<span id="verNum">…</span></button>
|
||
</div>
|
||
</header>
|
||
|
||
<!-- 公告面板 -->
|
||
<div class="annpanel" id="annPanel" style="display:none">
|
||
<div class="row" style="justify-content:space-between;margin-bottom:6px">
|
||
<strong>公告</strong>
|
||
<button class="mini" id="annClose" type="button">关闭</button>
|
||
</div>
|
||
<div id="annList"><div class="empty">加载中…</div></div>
|
||
</div>
|
||
|
||
<!-- 公告弹窗(只弹一次,按公告 id 记忆) -->
|
||
<div class="modal" id="annModal" style="display:none">
|
||
<div class="mbox">
|
||
<div class="row" style="justify-content:space-between">
|
||
<strong id="mTitle">公告</strong>
|
||
<button class="mini" id="mClose" type="button">知道了</button>
|
||
</div>
|
||
<div class="c" id="mBody"></div>
|
||
<a class="mini" id="mLink" style="display:none;margin-top:12px" target="_blank" rel="noopener">查看详情</a>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="wrap">
|
||
<!-- ============ 新版本提示 ============ -->
|
||
<div class="banner" id="updBanner" style="display:none">
|
||
<strong>发现新版本 <span id="updLatest"></span></strong>
|
||
<span id="updLog" style="flex:1;min-width:180px"></span>
|
||
<a id="updLink" href="#" target="_blank" rel="noopener" style="display:none">查看 / 下载</a>
|
||
<button class="mini" id="updHide" type="button">知道了</button>
|
||
</div>
|
||
|
||
<!-- ============ API 设置(没 Key 只能用基础功能) ============ -->
|
||
<div class="card settings">
|
||
<div class="row" style="justify-content:space-between">
|
||
<label style="margin:0;font-size:15px;color:var(--txt);font-weight:700">API 设置</label>
|
||
<button class="btn ghost sm" id="setToggle">收起</button>
|
||
</div>
|
||
<div class="hint" style="margin-top:6px">AI 生图 / 修图需要自己的 API Key(服务端只转发,不保存 Key)。没 Key 只能用基础功能:素材库、已生成图片的下载 / 裁剪 / 复制。</div>
|
||
|
||
<div id="setBody" style="margin-top:14px">
|
||
<div class="sgrid">
|
||
<div>
|
||
<label>接口地址(OpenAI 兼容)</label>
|
||
<input type="text" id="cfgBase" spellcheck="false" placeholder="https://ai.88531.cn/v1" />
|
||
</div>
|
||
<div>
|
||
<label>API Key(必填才能用 AI)</label>
|
||
<div class="row" style="gap:6px">
|
||
<input type="password" id="cfgKey" spellcheck="false" placeholder="sk-…" style="flex:1;min-width:150px" />
|
||
<button class="mini" id="keyEye" type="button">显示</button>
|
||
</div>
|
||
</div>
|
||
<div>
|
||
<label>模型</label>
|
||
<div class="row" style="gap:6px">
|
||
<input type="text" id="cfgModel" spellcheck="false" placeholder="mimo-v2.6-flash" style="flex:1;min-width:120px" />
|
||
<button class="mini" id="fetchModels" type="button">拉取列表</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="row" style="margin-top:14px">
|
||
<button class="btn sm" id="saveCfg">保存并启用 AI</button>
|
||
<button class="btn ghost sm" id="clearCfg">清除配置</button>
|
||
<span class="hint" id="cfgHint"></span>
|
||
</div>
|
||
|
||
<div class="banner" id="keyBanner" style="display:none;margin-top:14px;margin-bottom:0">
|
||
<span>还没有 API Key:AI 生图 / 修图无法使用。免费注册即可领取 →</span>
|
||
<a id="regLink" href="#" target="_blank" rel="noopener">去免费注册获取 Key</a>
|
||
<span class="hint">(注册后复制 Key 粘到上面的输入框,点「保存并启用 AI」)</span>
|
||
</div>
|
||
|
||
<div class="status" id="cfgStatus"></div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="tabs">
|
||
<button class="on" data-tab="gen">AI 生图</button>
|
||
<button data-tab="edit">AI 修图</button>
|
||
<button data-tab="lib">素材库</button>
|
||
</div>
|
||
|
||
<!-- ============ 生图 ============ -->
|
||
<section id="tab-gen">
|
||
<div class="banner" id="genBanner" style="display:none">
|
||
<span>AI 生图需要 API Key。</span>
|
||
<a href="#" data-open-set>去上方「API 设置」填写</a>
|
||
<a id="regLink2" href="#" target="_blank" rel="noopener">没有 Key?免费注册获取</a>
|
||
</div>
|
||
<div class="grid">
|
||
<div class="card">
|
||
<label>画面描述(中文或英文都行)</label>
|
||
<textarea id="prompt" placeholder="例:扁平风科技插画,深蓝渐变背景,发光的笔记本电脑和悬浮 App 图标,左侧留出标题空间,不要文字"></textarea>
|
||
<div class="chips" id="styleChips"></div>
|
||
|
||
<div class="row" style="margin-top:14px">
|
||
<div style="flex:1;min-width:210px">
|
||
<label style="margin-top:0">出图比例 / 尺寸</label>
|
||
<select id="size"></select>
|
||
</div>
|
||
<div style="flex:1;min-width:210px">
|
||
<label style="margin-top:0">模型(在「API 设置」里可改 / 拉取列表)</label>
|
||
<select id="model"></select>
|
||
</div>
|
||
</div>
|
||
<div class="row" style="margin-top:16px">
|
||
<button class="btn" id="genBtn">生成图片</button>
|
||
<span class="hint" id="genTime"></span>
|
||
</div>
|
||
<div class="status" id="genStatus"></div>
|
||
</div>
|
||
<div class="card">
|
||
<label style="margin-top:0">结果</label>
|
||
<div class="hint" id="genEmpty">生成后会显示在这里,可下载或按平台尺寸裁剪。</div>
|
||
<div class="results" id="genResults"></div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<!-- ============ 修图 ============ -->
|
||
<section id="tab-edit" style="display:none">
|
||
<div class="banner" id="editBanner" style="display:none">
|
||
<span>AI 修图需要 API Key。</span>
|
||
<a href="#" data-open-set>去上方「API 设置」填写</a>
|
||
<a id="regLink3" href="#" target="_blank" rel="noopener">没有 Key?免费注册获取</a>
|
||
</div>
|
||
<div class="grid">
|
||
<div class="card">
|
||
<label style="margin-top:0">上传要改的图(封面、截图、照片都行)</label>
|
||
<div class="drop" id="drop">点击选择图片,或把图片拖到这里 / 直接 Ctrl+V 粘贴</div>
|
||
<input type="file" id="file" accept="image/*" hidden />
|
||
|
||
<label>修改要求</label>
|
||
<textarea id="editPrompt" placeholder="例:把背景换成浅灰渐变,去掉所有文字,主体换成扁平插画风格"></textarea>
|
||
<div class="chips" id="editChips"></div>
|
||
|
||
<label>修图方式</label>
|
||
<div class="row" style="gap:16px">
|
||
<label class="row" style="margin:0;color:var(--txt);font-size:13.5px">
|
||
<input type="radio" name="mode" value="redraw" checked> AI 重绘(看图后按要求重画一版)
|
||
</label>
|
||
<label class="row" style="margin:0;color:var(--txt);font-size:13.5px">
|
||
<input type="radio" name="mode" value="edit"> 精细修图(保持原图像素,需接口支持 /images/edits)
|
||
</label>
|
||
</div>
|
||
<div class="row" style="margin-top:14px">
|
||
<div style="flex:1;min-width:200px">
|
||
<label style="margin-top:0">输出尺寸</label>
|
||
<select id="editSize"></select>
|
||
</div>
|
||
</div>
|
||
<div class="row" style="margin-top:16px">
|
||
<button class="btn" id="editBtn">开始修图</button>
|
||
</div>
|
||
<div class="status" id="editStatus"></div>
|
||
</div>
|
||
<div class="card">
|
||
<label style="margin-top:0">对比</label>
|
||
<div class="cmp">
|
||
<figure><figcaption>原图</figcaption><img id="oriImg" alt="原图" style="display:none" /></figure>
|
||
<figure><figcaption>结果</figcaption><img id="outImg" alt="结果" style="display:none" /></figure>
|
||
</div>
|
||
<div class="hint" id="editHint" style="margin-top:10px"></div>
|
||
<div class="results" id="editResults"></div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
<!-- ============ 素材库 ============ -->
|
||
<section id="tab-lib" style="display:none">
|
||
<div class="card">
|
||
<div class="row" style="justify-content:space-between">
|
||
<label style="margin:0">已生成的图(<span id="libCount">0</span> 张)</label>
|
||
<button class="btn ghost" id="libRefresh">刷新</button>
|
||
</div>
|
||
<div class="hint">基础功能,不需要 Key。「米图设计 → 素材」面板里会出现这些图,点一下就能放进画布。</div>
|
||
<div class="results" id="libResults"></div>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
|
||
<footer id="foot">AI 接口由你自己配置:默认 <span id="footBase"></span> · 模型 <span id="footModel"></span>。未配置 Key 时仅基础功能可用。<span style="float:right"><a href="https://nav.88531.cn/" target="_blank" rel="noopener" style="color:#38bdf8;text-decoration:none">我的导航站</a> · <a href="https://gitee.com/jinghui111/cover-ai" target="_blank" rel="noopener" style="color:#38bdf8;text-decoration:none">开源地址</a></span></footer>
|
||
|
||
<datalist id="modelList"></datalist>
|
||
|
||
<script>
|
||
const $ = (s) => document.querySelector(s);
|
||
const SIZES = {
|
||
'1280x720':'横版 1280×720(头条/视频封面)',
|
||
'900x383':'公众号封面 900×383',
|
||
'1080x1440':'小红书 1080×1440',
|
||
'1200x675':'知乎 1200×675',
|
||
'1146x717':'B站 1146×717',
|
||
'1024x1024':'方形 1024×1024',
|
||
'1080x1920':'竖版海报 1080×1920',
|
||
};
|
||
const STYLES = ['扁平矢量插画','3D 质感渲染','极简留白','深色科技渐变','柔和马卡龙配色','手绘水彩','中式水墨','赛博霓虹','毛玻璃拟物','杂志编辑风'];
|
||
const EDITS = ['把背景换成浅灰渐变','改成暖米色调','去掉图中所有文字','换成扁平插画风格','主体放大并留白给标题','换成纯色背景便于排版','增加柔和光效','改成深色科技风'];
|
||
const SAFE = '画面中不要出现任何文字、字母、数字或水印。';
|
||
|
||
const LS = { base:'coverai.base', key:'coverai.key', model:'coverai.model', models:'coverai.models' };
|
||
let DEF = { base:'https://ai.88531.cn/v1', model:'mimo-v2.6-flash', registerUrl:'https://ai.88531.cn/register' };
|
||
let currentImg = null;
|
||
|
||
/* ---------- 配置读写 ---------- */
|
||
const cfgGet = (k, d='') => localStorage.getItem(k) ?? d;
|
||
const cfgSet = (k, v) => localStorage.setItem(k, v);
|
||
const CFG = {
|
||
get base(){ return (cfgGet(LS.base) || DEF.base).replace(/\/+$/,''); },
|
||
get key(){ return cfgGet(LS.key).trim(); },
|
||
get model(){ return cfgGet(LS.model) || DEF.model; },
|
||
};
|
||
function setAll() {
|
||
$('#cfgBase').value = cfgGet(LS.base) || DEF.base;
|
||
$('#cfgKey').value = cfgGet(LS.key);
|
||
$('#cfgModel').value = cfgGet(LS.model) || DEF.model;
|
||
}
|
||
|
||
function hasKey(){ return !!cfgGet(LS.key).trim(); }
|
||
|
||
/* 无 Key:AI 按钮禁用 + 引导注册 */
|
||
function applyKeyState() {
|
||
const ok = hasKey();
|
||
$('#cfgTag').textContent = ok ? '已配置 API Key · AI 可用' : '未配置 API Key · 仅基础功能';
|
||
$('#cfgTag').className = 'tag ' + (ok ? 'on' : 'off');
|
||
$('#genBtn').disabled = !ok;
|
||
$('#editBtn').disabled = !ok;
|
||
$('#genBanner').style.display = ok ? 'none' : '';
|
||
$('#editBanner').style.display = ok ? 'none' : '';
|
||
$('#keyBanner').style.display = ok ? 'none' : '';
|
||
$('#regLink').href = DEF.registerUrl;
|
||
$('#regLink2').href = DEF.registerUrl;
|
||
$('#regLink3').href = DEF.registerUrl;
|
||
$('#cfgHint').textContent = ok ? '已保存到本浏览器,服务端不保存 Key。' : '未配置:AI 生图 / 修图已禁用。';
|
||
}
|
||
|
||
/* 把配置随请求头发给服务端(服务端只转发) */
|
||
async function ai(path, opts = {}) {
|
||
const r = await fetch(path, {
|
||
...opts,
|
||
headers: Object.assign({ 'Content-Type':'application/json',
|
||
'x-ai-base': CFG.base, 'x-ai-key': CFG.key, 'x-ai-model': CFG.model }, opts.headers || {}),
|
||
});
|
||
const d = await r.json().catch(() => ({ ok:false, error:'返回解析失败' }));
|
||
if (d && d.needKey) { openSettings(); }
|
||
return d;
|
||
}
|
||
async function api(path, opts = {}) {
|
||
const r = await fetch(path, { headers: { 'Content-Type':'application/json' }, ...opts });
|
||
return r.json().catch(() => ({ ok:false, error:'返回解析失败' }));
|
||
}
|
||
|
||
/* ---------- 模型列表 ---------- */
|
||
function modelOptions() {
|
||
let list = [];
|
||
try { list = JSON.parse(cfgGet(LS.models) || '[]'); } catch {}
|
||
if (!Array.isArray(list) || !list.length) list = [CFG.model];
|
||
if (!list.includes(CFG.model)) list.unshift(CFG.model);
|
||
return [...new Set(list)];
|
||
}
|
||
function renderModels() {
|
||
const sel = $('#model');
|
||
sel.innerHTML = modelOptions().map(m => `<option value="${m}"${m===CFG.model?' selected':''}>${m}</option>`).join('');
|
||
$('#modelList').innerHTML = modelOptions().map(m => `<option value="${m}">`).join('');
|
||
$('#modelTag').textContent = CFG.model;
|
||
}
|
||
$('#model').addEventListener('change', () => {
|
||
cfgSet(LS.model, $('#model').value);
|
||
$('#cfgModel').value = $('#model').value;
|
||
$('#modelTag').textContent = CFG.model;
|
||
});
|
||
$('#fetchModels').onclick = async () => {
|
||
if (!hasKey()) { setS('#cfgStatus', '先填写 API Key 再拉取模型列表。', 'err'); openSettings(); return; }
|
||
saveCfg(false);
|
||
setS('#cfgStatus', '<span class="loading"></span> 正在从接口拉取模型…');
|
||
const r = await ai('/api/models');
|
||
if (!r.ok) { setS('#cfgStatus', r.error || '拉取失败', 'err'); return; }
|
||
cfgSet(LS.models, JSON.stringify(r.models.slice(0, 300)));
|
||
renderModels();
|
||
setS('#cfgStatus', `已拉取 ${r.count} 个模型,下拉框可选(当前 ${CFG.model})。`, 'ok');
|
||
};
|
||
|
||
/* ---------- 设置区交互 ---------- */
|
||
function openSettings(){ $('#setBody').style.display = ''; $('#setToggle').textContent = '收起'; window.scrollTo({top:0,behavior:'smooth'}); }
|
||
$('#setToggle').onclick = () => {
|
||
const hide = $('#setBody').style.display !== 'none';
|
||
$('#setBody').style.display = hide ? 'none' : '';
|
||
$('#setToggle').textContent = hide ? '展开' : '收起';
|
||
};
|
||
document.querySelectorAll('[data-open-set]').forEach(a => a.onclick = e => { e.preventDefault(); openSettings(); });
|
||
$('#keyEye').onclick = () => {
|
||
const k = $('#cfgKey');
|
||
k.type = k.type === 'password' ? 'text' : 'password';
|
||
$('#keyEye').textContent = k.type === 'password' ? '显示' : '隐藏';
|
||
};
|
||
function saveCfg(notify = true) {
|
||
cfgSet(LS.base, ($('#cfgBase').value || DEF.base).trim());
|
||
cfgSet(LS.key, $('#cfgKey').value.trim());
|
||
cfgSet(LS.model, ($('#cfgModel').value || DEF.model).trim());
|
||
applyKeyState(); renderModels();
|
||
if (notify) setS('#cfgStatus', hasKey() ? '已保存,AI 功能已启用。' : '已保存,但还没填 Key,AI 功能仍禁用。', hasKey() ? 'ok' : 'warn');
|
||
}
|
||
$('#saveCfg').onclick = saveCfg;
|
||
$('#clearCfg').onclick = () => {
|
||
[LS.base, LS.key, LS.model, LS.models].forEach(k => localStorage.removeItem(k));
|
||
setAll(); applyKeyState(); renderModels();
|
||
setS('#cfgStatus', '已清除,AI 功能回到未配置状态(基础功能仍可用)。', 'warn');
|
||
};
|
||
['#cfgBase','#cfgKey','#cfgModel'].forEach(s => $(s).addEventListener('keydown', e => { if (e.key === 'Enter') saveCfg(); }));
|
||
|
||
/* ---------- 授权中心:版本 / 更新提示 / 公告 / 心跳 ---------- */
|
||
const HUB = { version: '…', update: {}, announcements: [], popup: [] };
|
||
const esc = (s) => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&', '<':'<', '>':'>', '"':'"' }[c]));
|
||
const annSeen = () => { try { return JSON.parse(localStorage.getItem('coverai.annSeen') || '[]'); } catch { return []; } };
|
||
|
||
function renderUpdate() {
|
||
const u = HUB.update || {};
|
||
const has = !!u.update_available && !!u.latest_version;
|
||
const dismissed = localStorage.getItem('coverai.updHide') === String(u.latest_version);
|
||
const chip = $('#verChip');
|
||
chip.classList.toggle('upd', has && !dismissed);
|
||
chip.title = has ? `当前 v${HUB.version} · 发现新版本 v${u.latest_version}` : `当前版本 v${HUB.version}`;
|
||
if (has && !dismissed) {
|
||
$('#updLatest').textContent = 'v' + u.latest_version;
|
||
$('#updLog').textContent = u.changelog || '(更新说明见下载页)';
|
||
const a = $('#updLink');
|
||
if (u.download_url) { a.href = u.download_url; a.style.display = ''; } else a.style.display = 'none';
|
||
$('#updBanner').style.display = '';
|
||
} else {
|
||
$('#updBanner').style.display = 'none';
|
||
}
|
||
}
|
||
|
||
function renderAnn() {
|
||
const list = HUB.announcements || [];
|
||
$('#annDot').classList.toggle('on', list.length > 0);
|
||
$('#annBtn').title = list.length ? `公告(${list.length} 条)` : '暂无公告';
|
||
$('#annList').innerHTML = list.length ? list.map(a => {
|
||
const link = a.link || a.url || '';
|
||
return `<div class="it">
|
||
<div class="t">${esc(a.title || a.subject || '公告')}</div>
|
||
<div class="c">${esc(a.content || a.body || a.text || '')}</div>
|
||
${link ? `<a class="mini" style="margin-top:7px;display:inline-block" href="${esc(link)}" target="_blank" rel="noopener">查看详情</a>` : ''}
|
||
<div class="m">${esc(a.created_at || a.publish_at || '')}</div>
|
||
</div>`;
|
||
}).join('') : '<div class="empty">暂无公告</div>';
|
||
if (!list.length) $('#annPanel').style.display = 'none'; // 公告被清空 → 面板跟着关
|
||
const seen = annSeen();
|
||
const pop = (HUB.popup || []).filter(p => !seen.includes(String(p.id)));
|
||
if (pop.length) showModal({ id: pop[0].id, title: pop[0].title || pop[0].subject || '公告', content: pop[0].content || pop[0].body || pop[0].text || '', link: pop[0].link || pop[0].url || '' });
|
||
}
|
||
|
||
function showModal({ id = null, title, content, link = '' }) {
|
||
$('#mTitle').textContent = title || '公告';
|
||
$('#mBody').textContent = content || '(无内容)';
|
||
const l = $('#mLink');
|
||
if (link) { l.href = link; l.style.display = ''; } else l.style.display = 'none';
|
||
$('#annModal').style.display = '';
|
||
$('#mClose').onclick = () => {
|
||
$('#annModal').style.display = 'none';
|
||
if (id !== null) {
|
||
const seen = annSeen(); seen.push(String(id));
|
||
localStorage.setItem('coverai.annSeen', JSON.stringify(seen.slice(-200)));
|
||
}
|
||
};
|
||
}
|
||
|
||
async function hubLoad() {
|
||
try {
|
||
const d = await api('/api/hub/status');
|
||
if (!d || !d.ok) return;
|
||
HUB.version = d.version || HUB.version;
|
||
HUB.update = d.update || {};
|
||
HUB.announcements = Array.isArray(d.announcements) ? d.announcements : [];
|
||
HUB.popup = Array.isArray(d.popup) ? d.popup : [];
|
||
$('#verNum').textContent = HUB.version;
|
||
renderUpdate();
|
||
renderAnn();
|
||
} catch {}
|
||
}
|
||
function hubBeat() { fetch('/api/hub/heartbeat', { method: 'POST' }).catch(() => {}); }
|
||
|
||
$('#annBtn').onclick = () => { const p = $('#annPanel'); p.style.display = p.style.display === 'none' ? '' : 'none'; };
|
||
$('#annClose').onclick = () => { $('#annPanel').style.display = 'none'; };
|
||
$('#updHide').onclick = () => {
|
||
localStorage.setItem('coverai.updHide', String((HUB.update || {}).latest_version || ''));
|
||
renderUpdate();
|
||
};
|
||
$('#verChip').onclick = () => {
|
||
const u = HUB.update || {};
|
||
if (u.update_available && localStorage.getItem('coverai.updHide') !== String(u.latest_version)) {
|
||
$('#updBanner').style.display = '';
|
||
$('#updBanner').scrollIntoView({ behavior: 'smooth', block: 'center' });
|
||
} else {
|
||
showModal({ title: '版本信息', content: `当前版本:v${HUB.version}\n最新版本:${u.latest_version ? 'v' + u.latest_version : '暂无'}\n${u.update_available ? '有新版本可用,点右上角版本号查看。' : '已是最新版本。'}` });
|
||
}
|
||
};
|
||
|
||
/* ---------- 初始化 ---------- */
|
||
(async function init() {
|
||
try {
|
||
const c = await api('/api/ai-config');
|
||
if (c && c.base) DEF = c;
|
||
} catch {}
|
||
setAll();
|
||
$('#footBase').textContent = DEF.base;
|
||
$('#footModel').textContent = DEF.model;
|
||
$('#modelTag').textContent = CFG.model;
|
||
|
||
const sizeSel = $('#size'), editSel = $('#editSize');
|
||
for (const [k, v] of Object.entries(SIZES)) {
|
||
sizeSel.insertAdjacentHTML('beforeend', `<option value="${k}"${k==='1280x720'?' selected':''}>${v}</option>`);
|
||
editSel.insertAdjacentHTML('beforeend', `<option value="${k}"${k==='1280x720'?' selected':''}>${v}</option>`);
|
||
}
|
||
renderModels();
|
||
applyKeyState();
|
||
if (!hasKey()) { setS('#cfgStatus', '未配置 API Key:先填 Key 才能用 AI(素材库等基础功能不受影响)。', 'warn'); }
|
||
|
||
/* 授权中心:开机一次 + 每 60 秒刷公告/版本,每 30 分钟一次心跳 */
|
||
hubLoad();
|
||
hubBeat();
|
||
setInterval(hubLoad, 60000);
|
||
setInterval(hubBeat, 30 * 60000);
|
||
|
||
$('#styleChips').innerHTML = STYLES.map(s => `<span>${s}</span>`).join('');
|
||
$('#editChips').innerHTML = EDITS.map(s => `<span>${s}</span>`).join('');
|
||
document.querySelectorAll('#styleChips span').forEach(el => el.onclick = () => {
|
||
const t = $('#prompt'); t.value = (t.value ? t.value.replace(/[,,]\s*$/, '') + ',' : '') + el.textContent; t.focus();
|
||
});
|
||
document.querySelectorAll('#editChips span').forEach(el => el.onclick = () => {
|
||
const t = $('#editPrompt'); t.value = (t.value ? t.value.replace(/[,,]\s*$/, '') + ',' : '') + el.textContent; t.focus();
|
||
});
|
||
$('#libCount').textContent = (await api('/api/gallery')).items?.length || 0;
|
||
})();
|
||
|
||
/* ---------- Tab ---------- */
|
||
document.querySelectorAll('.tabs button').forEach(b => b.onclick = () => {
|
||
document.querySelectorAll('.tabs button').forEach(x => x.classList.toggle('on', x === b));
|
||
['gen','edit','lib'].forEach(t => $('#tab-' + t).style.display = (t === b.dataset.tab ? '' : 'none'));
|
||
if (b.dataset.tab === 'lib') loadLib();
|
||
});
|
||
|
||
/* ---------- 生图 ---------- */
|
||
$('#genBtn').onclick = async () => {
|
||
if (!hasKey()) { openSettings(); setS('#genStatus', '先在「API 设置」里配置 API Key。', 'err'); return; }
|
||
const prompt = ($('#prompt').value || '').trim();
|
||
if (!prompt) { setS('#genStatus', '先写点画面描述', 'err'); return; }
|
||
$('#genBtn').disabled = true;
|
||
setS('#genStatus', '<span class="loading"></span> 正在生成(约 10 秒)…');
|
||
const t0 = Date.now();
|
||
const r = await ai('/api/gen', { method: 'POST', body: JSON.stringify({
|
||
prompt: prompt + '。' + SAFE, size: $('#size').value, model: $('#model').value, n: 1 }) });
|
||
$('#genBtn').disabled = hasKey() ? false : true;
|
||
$('#genTime').textContent = ((Date.now() - t0) / 1000).toFixed(1) + ' 秒';
|
||
if (!r.ok) { setS('#genStatus', r.error || '生成失败', 'err'); return; }
|
||
setS('#genStatus', '生成成功,共 ' + r.images.length + ' 张', 'ok');
|
||
$('#genEmpty').style.display = 'none';
|
||
r.images.forEach(img => $('#genResults').insertAdjacentHTML('afterbegin', cardHtml(img, $('#size').value)));
|
||
bindCards('#genResults');
|
||
};
|
||
|
||
/* ---------- 修图 ---------- */
|
||
const drop = $('#drop'), fileInput = $('#file');
|
||
drop.onclick = () => fileInput.click();
|
||
drop.ondragover = e => { e.preventDefault(); drop.classList.add('on'); };
|
||
drop.ondragleave = () => drop.classList.remove('on');
|
||
drop.ondrop = e => { e.preventDefault(); drop.classList.remove('on'); if (e.dataTransfer.files[0]) readFile(e.dataTransfer.files[0]); };
|
||
fileInput.onchange = e => e.target.files[0] && readFile(e.target.files[0]);
|
||
window.addEventListener('paste', e => {
|
||
const it = [...(e.clipboardData?.items || [])].find(x => x.type.startsWith('image/'));
|
||
if (it) readFile(it.getAsFile());
|
||
});
|
||
function readFile(f) {
|
||
if (!f || !f.type.startsWith('image/')) return;
|
||
const fr = new FileReader();
|
||
fr.onload = () => {
|
||
currentImg = fr.result;
|
||
$('#oriImg').src = currentImg; $('#oriImg').style.display = '';
|
||
drop.textContent = '已选择:' + f.name + '(点这里可重选)';
|
||
setS('#editStatus', '图片已载入,填好修改要求后点「开始修图」');
|
||
};
|
||
fr.readAsDataURL(f);
|
||
}
|
||
|
||
$('#editBtn').onclick = async () => {
|
||
if (!hasKey()) { openSettings(); setS('#editStatus', '先在「API 设置」里配置 API Key。', 'err'); return; }
|
||
if (!currentImg) { setS('#editStatus', '先上传一张图', 'err'); return; }
|
||
const mode = document.querySelector('input[name=mode]:checked').value;
|
||
const prompt = ($('#editPrompt').value || '').trim();
|
||
if (!prompt) { setS('#editStatus', '写点修改要求(例如:换成浅灰背景)', 'err'); return; }
|
||
$('#editBtn').disabled = true;
|
||
setS('#editStatus', '<span class="loading"></span> ' + (mode === 'edit' ? '正在精细修图…' : '正在分析原图并重绘(约 25 秒)…'));
|
||
const path = mode === 'edit' ? '/api/edit' : '/api/redraw';
|
||
const body = { image: currentImg, prompt, size: $('#editSize').value, model: $('#model').value };
|
||
const r = await ai(path, { method: 'POST', body: JSON.stringify(body) });
|
||
$('#editBtn').disabled = hasKey() ? false : true;
|
||
if (!r.ok) {
|
||
setS('#editStatus', (r.error || '失败') + (mode === 'edit' ? ' → 可切到「AI 重绘」立刻出图' : ''), 'err');
|
||
return;
|
||
}
|
||
const img = r.images[0];
|
||
setS('#editStatus', '完成' + (r.described ? '(原图分析:' + r.described.slice(0, 60) + '…)' : '') + (r.describeWarn ? '(看图失败,已按你的要求直接重画)' : ''), 'ok');
|
||
$('#outImg').src = img.url; $('#outImg').style.display = '';
|
||
const sz = $('#editSize').value;
|
||
$('#editResults').innerHTML = `<div style="display:flex;gap:8px;flex-wrap:wrap;margin-top:12px">
|
||
<a class="mini" href="${img.url}" download>下载结果</a>
|
||
<button class="mini" data-url="${img.url}" data-size="${sz}" data-act="crop">裁剪到 ${sz.replace('x','×')}</button>
|
||
<button class="mini" data-url="${img.url}" data-act="copy">复制地址</button>
|
||
</div>`;
|
||
$('#editResults').querySelectorAll('button').forEach(b => b.onclick = async () => {
|
||
if (b.dataset.act === 'copy') { navigator.clipboard?.writeText(b.dataset.url); b.textContent = '已复制'; setTimeout(() => b.textContent = '复制地址', 1200); }
|
||
if (b.dataset.act === 'crop') await cropDownload(b.dataset.url, b.dataset.size);
|
||
});
|
||
};
|
||
|
||
/* ---------- 素材库 ---------- */
|
||
async function loadLib() {
|
||
const r = await api('/api/gallery');
|
||
const items = r.items || [];
|
||
$('#libCount').textContent = items.length;
|
||
$('#libResults').innerHTML = items.map(i => cardHtml(i, i.size, true)).join('');
|
||
bindCards('#libResults');
|
||
}
|
||
$('#libRefresh').onclick = loadLib;
|
||
|
||
/* ---------- 卡片 ---------- */
|
||
function cardHtml(img, size, withDel = true) {
|
||
return `<div class="shot" data-name="${img.name}" data-url="${img.url}" data-size="${size || ''}">
|
||
<img src="${img.url}" loading="lazy" alt="" />
|
||
<div class="bar">
|
||
<a href="${img.url}" download>下载</a>
|
||
<button data-act="crop">裁剪到 ${(size || '').replace('x','×')}</button>
|
||
<button data-act="copy">复制地址</button>
|
||
${withDel ? '<button data-act="del">删除</button>' : ''}
|
||
</div>
|
||
</div>`;
|
||
}
|
||
|
||
function bindCards(sel) {
|
||
document.querySelectorAll(sel + ' .shot').forEach(card => {
|
||
card.querySelectorAll('button').forEach(btn => btn.onclick = async () => {
|
||
const act = btn.dataset.act, url = card.dataset.url, size = card.dataset.size;
|
||
if (act === 'copy') { navigator.clipboard?.writeText(url); btn.textContent = '已复制'; setTimeout(() => btn.textContent = '复制地址', 1200); }
|
||
if (act === 'del') {
|
||
await fetch('/api/gallery/' + card.dataset.name, { method: 'DELETE' });
|
||
card.remove(); loadLib();
|
||
}
|
||
if (act === 'crop') await cropDownload(url, size);
|
||
});
|
||
});
|
||
}
|
||
|
||
/* 按平台尺寸居中裁剪(浏览器本地完成) */
|
||
async function cropDownload(url, size) {
|
||
if (!size) return;
|
||
const [tw, th] = size.split('x').map(Number);
|
||
const img = new Image(); img.crossOrigin = 'anonymous'; img.src = url;
|
||
await img.decode();
|
||
const scale = Math.max(tw / img.width, th / img.height);
|
||
const sw = tw / scale, sh = th / scale;
|
||
const sx = (img.width - sw) / 2, sy = (img.height - sh) / 2;
|
||
const c = document.createElement('canvas'); c.width = tw; c.height = th;
|
||
c.getContext('2d').drawImage(img, sx, sy, sw, sh, 0, 0, tw, th);
|
||
c.toBlob(b => {
|
||
const a = document.createElement('a');
|
||
a.href = URL.createObjectURL(b);
|
||
a.download = `cover-${tw}x${th}-${Date.now()}.png`;
|
||
a.click();
|
||
}, 'image/png');
|
||
}
|
||
|
||
function setS(sel, html, cls = '') {
|
||
const el = $(sel); el.className = 'status ' + cls; el.innerHTML = html;
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|