import { App, PluginSettingTab, Setting, Notice } from "obsidian"; import DiscuzPublisher from "../main"; import { SiteConfig, createDefaultSite, parseAliases, } from "./plugin_settings"; import { HttpUtils } from "../utils/request"; export class DiscuzSettingTab extends PluginSettingTab { plugin: DiscuzPublisher; constructor(app: App, plugin: DiscuzPublisher) { super(app, plugin); this.plugin = plugin; } display(): void { const { containerEl } = this; containerEl.empty(); containerEl.createEl("h2", { text: "Discuz 发布设置", attr: { style: "padding-left: 0; margin-left: 0;" }, }); const settings = this.plugin.settings; containerEl.createEl("h3", { text: "站点列表", attr: { style: "padding-left: 0; margin-left: 0;" }, }); settings.sites.forEach((site, index) => { const siteBox = containerEl.createDiv({ attr: { style: "border: 1px solid var(--background-modifier-border); padding: 12px; border-radius: 6px; margin-bottom: 12px;", }, }); // ===== 标题行 ===== const header = siteBox.createDiv({ attr: { style: "display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 8px;", }, }); const titleWrap = header.createDiv({ attr: { style: "display: flex; align-items: center; gap: 8px; flex: 1;" }, }); titleWrap.createEl("strong", { text: site.name || `站点 ${index + 1}`, attr: { style: "font-size: 14px;" }, }); if (site.userInfo?.username) { titleWrap.createEl("span", { text: `👤 ${site.userInfo.username}`, attr: { style: "font-size: 12px; color: var(--text-muted);" }, }); if (site.isAdmin) { titleWrap.createEl("span", { text: "超管", attr: { style: "font-size: 11px; color: var(--text-success); padding: 0; border: none;", }, }); } } const btnGroup = header.createDiv({ attr: { style: "display: flex; gap: 6px; align-items: center;" }, }); const isActive = settings.activeSiteId === site.id; const activeBtn = btnGroup.createEl("button", { text: isActive ? "✓ 当前" : "设为当前", }); if (isActive) activeBtn.disabled = true; activeBtn.addEventListener("click", async () => { settings.activeSiteId = site.id; await this.plugin.saveSettings(); this.display(); }); if (site.userInfo?.username) { const logoutBtn = btnGroup.createEl("button", { text: "退出" }); logoutBtn.addEventListener("click", async () => { HttpUtils.setSite(site); HttpUtils.logout(); delete site.userInfo; delete site.isAdmin; delete site.token; delete site.tokenExpiry; await this.plugin.saveSettings(); new Notice("已退出登录"); this.display(); }); } else { const loginBtn = btnGroup.createEl("button", { text: "登录" }); loginBtn.addEventListener("click", async () => { try { HttpUtils.setSite(site); await HttpUtils.login(); await this.plugin.saveSettings(); new Notice("✅ 登录成功:" + (site.userInfo?.username || "")); this.display(); } catch (e) { new Notice("❌ " + (e instanceof Error ? e.message : String(e))); } }); } const delBtn = btnGroup.createEl("button", { text: "删除" }); delBtn.addEventListener("click", async () => { if (!confirm(`确定删除站点 "${site.name}"?`)) return; settings.sites.splice(index, 1); if (settings.activeSiteId === site.id) { settings.activeSiteId = settings.sites[0]?.id || ""; } await this.plugin.saveSettings(); this.display(); }); // 折叠按钮 const toggleBtn = btnGroup.createEl("button", { text: "▼", attr: { style: "width: 28px; padding: 0; font-size: 11px;", title: "展开/收起设置", }, }); // ===== 可折叠的设置内容 ===== const detailWrap = siteBox.createDiv({ attr: { style: "display: none; margin-top: 10px;" }, }); toggleBtn.addEventListener("click", () => { const expanded = detailWrap.style.display !== "none"; detailWrap.style.display = expanded ? "none" : "block"; toggleBtn.setText(expanded ? "▼" : "▲"); }); // ===== 基础字段 ===== const fields: [keyof SiteConfig, string, boolean][] = [ ["name", "站点名称", false], ["apiBase", "API 地址(末尾带 /)", false], ["siteUrl", "站点地址", false], ["appid", "AppID", false], ["secret", "Secret", true], ["username", "用户名", false], ["password", "密码", true], ]; for (const [key, label, isPwd] of fields) { new Setting(detailWrap).setName(label).addText((t) => { if (isPwd) t.inputEl.type = "password"; t.setValue(String(site[key] ?? "")).onChange(async (v) => { (site as any)[key] = v; await this.plugin.saveSettings(); }); }); } new Setting(detailWrap) .setName("默认版块 ID") .addText((t) => t.setValue(String(site.defaultFid)).onChange(async (v) => { site.defaultFid = parseInt(v) || 2; await this.plugin.saveSettings(); }) ); new Setting(detailWrap) .setName("测试连接") .addButton((b) => b.setButtonText("测试").onClick(async () => { try { HttpUtils.setSite(site); const r = await HttpUtils.testToken(); new Notice("✅ Token 获取成功: " + r.token); } catch (e) { new Notice("❌ " + (e instanceof Error ? e.message : String(e))); } }) ); // ===== 马甲设置:仅超管可见 ===== if (site.isAdmin) { detailWrap.createEl("hr", { attr: { style: "margin: 12px 0;" } }); detailWrap.createEl("div", { text: "马甲设置", attr: { style: "font-weight: bold; font-size: 13px; margin-bottom: 6px; padding-left: 0; margin-left: 0;", }, }); new Setting(detailWrap) .setName("马甲列表") .setDesc("格式:用户名:密码,用户名:密码(英文逗号分隔)") .addTextArea((t) => { t.setValue(site.aliases || "").onChange(async (v) => { site.aliases = v; await this.plugin.saveSettings(); }); t.inputEl.style.width = "100%"; t.inputEl.style.minHeight = "60px"; }); new Setting(detailWrap) .setName("测试马甲登录") .setDesc("逐个测试马甲账号是否能登录(实时读取当前输入)") .addButton((b) => b.setButtonText("测试全部").onClick(async () => { const currentAliases = parseAliases(site.aliases); console.log("[Discuz] 测试马甲,当前 aliases:", currentAliases); if (currentAliases.length === 0) { new Notice("未配置马甲"); return; } HttpUtils.setSite(site); try { if (!site.token) await HttpUtils.testToken(); } catch (e) { new Notice("❌ Token 获取失败,请先测试连接"); return; } const results: string[] = []; for (const a of currentAliases) { try { await HttpUtils.loginAs(a.username, a.password); results.push(`✅ ${a.username}`); } catch (e) { console.error(`马甲 ${a.username} 登录失败:`, e); results.push(`❌ ${a.username}`); } } // 测试完恢复主账号 try { await HttpUtils.loginAs(site.username, site.password); } catch (e) { console.error("[Discuz] 恢复主账号失败:", e); } new Notice("马甲测试结果:\n" + results.join("\n"), 8000); }) ); } }); new Setting(containerEl).addButton((b) => b.setButtonText("+ 添加站点").onClick(async () => { const newSite = createDefaultSite(`站点 ${settings.sites.length + 1}`); settings.sites.push(newSite); if (!settings.activeSiteId) settings.activeSiteId = newSite.id; await this.plugin.saveSettings(); this.display(); }) ); } }