v1.0.4修复优化
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
<div class="config-container">
|
||||
<div class="config-header">
|
||||
<h1>
|
||||
<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align:-4px;margin-right:8px;"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83-2.83l.06-.06A1.65 1.65 0 004.68 15a1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 012.83-2.83l.06.06A1.65 1.65 0 009 4.68a1.65 1.65 0 001-1.51V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 2.83l-.06.06A1.65 1.65 0 0019.4 9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
|
||||
<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon-inline-md"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 010 2.83 2 2 0 01-2.83 0l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 01-4 0v-.09A1.65 1.65 0 009 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 01-2.83-2.83l.06-.06A1.65 1.65 0 004.68 15a1.65 1.65 0 00-1.51-1H3a2 2 0 010-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 012.83-2.83l.06.06A1.65 1.65 0 009 4.68a1.65 1.65 0 001-1.51V3a2 2 0 014 0v.09a1.65 1.65 0 001 1.51 1.65 1.65 0 001.82-.33l.06-.06a2 2 0 012.83 2.83l-.06.06A1.65 1.65 0 0019.4 9a1.65 1.65 0 001.51 1H21a2 2 0 010 4h-.09a1.65 1.65 0 00-1.51 1z"/></svg>
|
||||
系统配置
|
||||
</h1>
|
||||
<a href="/chat.php" class="btn btn-secondary">
|
||||
<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="vertical-align:-2px;margin-right:4px;"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
|
||||
<svg viewBox="0 0 24 24" width="14" height="14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon-inline"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
|
||||
返回聊天
|
||||
</a>
|
||||
</div>
|
||||
@@ -16,7 +16,7 @@
|
||||
<div id="providerList">
|
||||
<!-- 由 JS 动态渲染 -->
|
||||
</div>
|
||||
<button class="btn btn-secondary" onclick="ConfigPage.addProvider()" style="margin-top:12px;">+ 添加供应商</button>
|
||||
<button class="btn btn-secondary config-add-btn" onclick="ConfigPage.addProvider()">+ 添加供应商</button>
|
||||
</div>
|
||||
|
||||
<!-- 人格管理 -->
|
||||
@@ -25,7 +25,7 @@
|
||||
<div id="personalityList">
|
||||
<!-- 由 JS 动态渲染 -->
|
||||
</div>
|
||||
<h3 style="margin-top:20px;">添加自定义人格</h3>
|
||||
<h3>添加自定义人格</h3>
|
||||
<div class="form-group">
|
||||
<label>名称</label>
|
||||
<input type="text" id="newPersonalityName" placeholder="人格名称">
|
||||
@@ -48,208 +48,4 @@
|
||||
|
||||
<div id="configMessage"></div>
|
||||
|
||||
<script>
|
||||
const ConfigPage = {
|
||||
providers: [],
|
||||
personalities: [],
|
||||
|
||||
async init() {
|
||||
// 检查认证和管理员权限
|
||||
const token = Storage.getToken();
|
||||
if (!token) {
|
||||
window.location.href = '/login.php';
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 验证管理员权限
|
||||
const userRes = await api.get('/auth/me');
|
||||
if (userRes.data.role !== 'admin') {
|
||||
window.location.href = '/chat.php';
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
window.location.href = '/login.php';
|
||||
return;
|
||||
}
|
||||
|
||||
await this.loadProviders();
|
||||
await this.loadPersonalities();
|
||||
},
|
||||
|
||||
async loadProviders() {
|
||||
try {
|
||||
const res = await api.get('/config');
|
||||
this.providers = res.data.providers || [];
|
||||
this.renderProviders();
|
||||
} catch (err) {
|
||||
this.showMessage('加载供应商配置失败: ' + err.message, 'error');
|
||||
}
|
||||
},
|
||||
|
||||
renderProviders() {
|
||||
const list = document.getElementById('providerList');
|
||||
if (this.providers.length === 0) {
|
||||
list.innerHTML = '<p style="color:var(--text-secondary)">暂无供应商配置</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
list.innerHTML = this.providers.map((p, i) => `
|
||||
<div class="provider-item" style="background:var(--bg-secondary);padding:16px;border-radius:var(--radius);margin-bottom:12px;">
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:12px;">
|
||||
<strong>${this.escapeHtml(p.name)}</strong>
|
||||
<div>
|
||||
<label class="toggle-switch">
|
||||
<input type="checkbox" ${p.enabled ? 'checked' : ''} onchange="ConfigPage.toggleProvider(${i}, this.checked)">
|
||||
<span class="slider"></span>
|
||||
</label>
|
||||
<button class="btn btn-danger btn-sm" onclick="ConfigPage.deleteProvider(${i})" style="margin-left:8px;">删除</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>API URL</label>
|
||||
<input type="text" value="${this.escapeHtml(p.apiUrl || '')}" onchange="ConfigPage.updateProvider(${i}, 'apiUrl', this.value)">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>API Key</label>
|
||||
<input type="password" value="${this.escapeHtml(p.apiKey || '')}" onchange="ConfigPage.updateProvider(${i}, 'apiKey', this.value)" placeholder="API 密钥">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>可用模型(逗号分隔)</label>
|
||||
<input type="text" value="${this.escapeHtml((p.models || []).join(', '))}" onchange="ConfigPage.updateProviderModels(${i}, this.value)">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>供应商类型</label>
|
||||
<select onchange="ConfigPage.updateProvider(${i}, 'type', this.value)">
|
||||
<option value="newapi" ${p.type === 'newapi' ? 'selected' : ''}>OpenAI 兼容</option>
|
||||
<option value="openai" ${p.type === 'openai' ? 'selected' : ''}>OpenAI 官方</option>
|
||||
<option value="claude" ${p.type === 'claude' ? 'selected' : ''}>Claude (Anthropic)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
`).join('');
|
||||
},
|
||||
|
||||
addProvider() {
|
||||
this.providers.push({
|
||||
name: '新供应商',
|
||||
apiUrl: '',
|
||||
apiKey: '',
|
||||
models: [],
|
||||
type: 'newapi',
|
||||
enabled: true
|
||||
});
|
||||
this.renderProviders();
|
||||
this.saveProviders();
|
||||
},
|
||||
|
||||
updateProvider(index, field, value) {
|
||||
this.providers[index][field] = value;
|
||||
this.saveProviders();
|
||||
},
|
||||
|
||||
updateProviderModels(index, value) {
|
||||
this.providers[index].models = value.split(',').map(m => m.trim()).filter(m => m);
|
||||
this.saveProviders();
|
||||
},
|
||||
|
||||
toggleProvider(index, enabled) {
|
||||
this.providers[index].enabled = enabled;
|
||||
this.saveProviders();
|
||||
},
|
||||
|
||||
deleteProvider(index) {
|
||||
if (!confirm('确定要删除供应商 "' + this.providers[index].name + '" 吗?')) return;
|
||||
this.providers.splice(index, 1);
|
||||
this.renderProviders();
|
||||
this.saveProviders();
|
||||
},
|
||||
|
||||
async saveProviders() {
|
||||
try {
|
||||
await api.put('/config', { providers: this.providers });
|
||||
this.showMessage('供应商配置已保存', 'success');
|
||||
} catch (err) {
|
||||
this.showMessage('保存失败: ' + err.message, 'error');
|
||||
}
|
||||
},
|
||||
|
||||
async loadPersonalities() {
|
||||
try {
|
||||
const res = await api.get('/personalities');
|
||||
this.personalities = res.data || [];
|
||||
this.renderPersonalities();
|
||||
} catch (err) {
|
||||
this.showMessage('加载人格列表失败: ' + err.message, 'error');
|
||||
}
|
||||
},
|
||||
|
||||
renderPersonalities() {
|
||||
const list = document.getElementById('personalityList');
|
||||
if (this.personalities.length === 0) {
|
||||
list.innerHTML = '<p style="color:var(--text-secondary)">暂无人格</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
list.innerHTML = this.personalities.map(p => `
|
||||
<div style="display:flex;justify-content:space-between;align-items:center;padding:12px;background:var(--bg-secondary);border-radius:var(--radius);margin-bottom:8px;">
|
||||
<div>
|
||||
<span>${p.icon ? '<span style="margin-right:4px;">' + this.escapeHtml(p.icon) + '</span>' : ''}${this.escapeHtml(p.name)}</span>
|
||||
${p.is_preset ? '<span style="color:var(--warning);font-size:12px;margin-left:8px;">预设</span>' : '<span style="color:var(--success);font-size:12px;margin-left:8px;">自定义</span>'}
|
||||
</div>
|
||||
${!p.is_preset ? '<button class="btn btn-danger btn-sm" onclick="ConfigPage.deletePersonality(' + p.id + ')">删除</button>' : ''}
|
||||
</div>
|
||||
`).join('');
|
||||
},
|
||||
|
||||
async createPersonality() {
|
||||
const name = document.getElementById('newPersonalityName').value.trim();
|
||||
const prompt = document.getElementById('newPersonalityPrompt').value.trim();
|
||||
const icon = document.getElementById('newPersonalityIcon').value.trim();
|
||||
const description = document.getElementById('newPersonalityDesc').value.trim();
|
||||
|
||||
if (!name || !prompt) {
|
||||
this.showMessage('名称和提示词不能为空', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await api.post('/personalities', { name, prompt, icon, description });
|
||||
this.showMessage('人格创建成功', 'success');
|
||||
// 清空表单
|
||||
document.getElementById('newPersonalityName').value = '';
|
||||
document.getElementById('newPersonalityPrompt').value = '';
|
||||
document.getElementById('newPersonalityIcon').value = '';
|
||||
document.getElementById('newPersonalityDesc').value = '';
|
||||
await this.loadPersonalities();
|
||||
} catch (err) {
|
||||
this.showMessage('创建失败: ' + err.message, 'error');
|
||||
}
|
||||
},
|
||||
|
||||
async deletePersonality(id) {
|
||||
if (!confirm('确定要删除这个人格吗?')) return;
|
||||
try {
|
||||
await api.delete('/personalities/' + id);
|
||||
this.showMessage('人格已删除', 'success');
|
||||
await this.loadPersonalities();
|
||||
} catch (err) {
|
||||
this.showMessage('删除失败: ' + err.message, 'error');
|
||||
}
|
||||
},
|
||||
|
||||
showMessage(text, type) {
|
||||
const el = document.getElementById('configMessage');
|
||||
el.innerHTML = `<div class="alert alert-${type === 'error' ? 'error' : 'success'}" style="position:fixed;bottom:20px;right:20px;z-index:1000;min-width:200px;">${this.escapeHtml(text)}</div>`;
|
||||
setTimeout(() => { el.innerHTML = ''; }, 3000);
|
||||
},
|
||||
|
||||
escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
};
|
||||
|
||||
ConfigPage.init();
|
||||
</script>
|
||||
<script src="/assets/js/config.js"></script>
|
||||
|
||||
Reference in New Issue
Block a user