优化项目结构
This commit is contained in:
0
public/assets/css/.gitkeep
Normal file
0
public/assets/css/.gitkeep
Normal file
345
public/assets/css/chat.css
Normal file
345
public/assets/css/chat.css
Normal file
@@ -0,0 +1,345 @@
|
||||
/* 聊天布局 */
|
||||
.chat-layout {
|
||||
display: flex;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* 侧边栏 */
|
||||
.sidebar {
|
||||
width: var(--sidebar-width);
|
||||
background: var(--bg-secondary);
|
||||
border-right: 1px solid var(--border-color);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
transition: transform 0.3s;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.sidebar.collapsed {
|
||||
transform: translateX(-100%);
|
||||
width: 0;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.sidebar-header {
|
||||
padding: 16px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.sidebar-header .btn {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.session-list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.session-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 12px;
|
||||
border-radius: var(--radius);
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.session-item:hover {
|
||||
background: var(--bg-card);
|
||||
}
|
||||
|
||||
.session-item.active {
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.session-item .session-name {
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.session-item .session-delete {
|
||||
opacity: 0;
|
||||
background: none;
|
||||
border: none;
|
||||
color: inherit;
|
||||
cursor: pointer;
|
||||
padding: 4px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.session-item:hover .session-delete {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
/* 主区域 */
|
||||
.chat-main {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
/* 工具栏 */
|
||||
.toolbar {
|
||||
height: var(--toolbar-height);
|
||||
background: var(--bg-secondary);
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0 16px;
|
||||
gap: 12px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.toolbar select {
|
||||
padding: 6px 10px;
|
||||
background: var(--bg-input);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius);
|
||||
color: var(--text-primary);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.toolbar .toggle-sidebar {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
cursor: pointer;
|
||||
font-size: 20px;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
/* 思考模式开关 */
|
||||
.toggle-switch {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
width: 40px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.toggle-switch input {
|
||||
opacity: 0;
|
||||
width: 0;
|
||||
height: 0;
|
||||
}
|
||||
|
||||
.toggle-switch .slider {
|
||||
position: absolute;
|
||||
cursor: pointer;
|
||||
top: 0; left: 0; right: 0; bottom: 0;
|
||||
background-color: var(--border-color);
|
||||
border-radius: 20px;
|
||||
transition: 0.3s;
|
||||
}
|
||||
|
||||
.toggle-switch .slider:before {
|
||||
position: absolute;
|
||||
content: "";
|
||||
height: 16px;
|
||||
width: 16px;
|
||||
left: 2px;
|
||||
bottom: 2px;
|
||||
background-color: white;
|
||||
border-radius: 50%;
|
||||
transition: 0.3s;
|
||||
}
|
||||
|
||||
.toggle-switch input:checked + .slider {
|
||||
background-color: var(--primary);
|
||||
}
|
||||
|
||||
.toggle-switch input:checked + .slider:before {
|
||||
transform: translateX(20px);
|
||||
}
|
||||
|
||||
/* 消息区域 */
|
||||
.messages-container {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 20px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.message {
|
||||
max-width: 80%;
|
||||
padding: 12px 16px;
|
||||
border-radius: var(--radius);
|
||||
line-height: 1.6;
|
||||
font-size: 14px;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.message.user {
|
||||
align-self: flex-end;
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
border-bottom-right-radius: 2px;
|
||||
}
|
||||
|
||||
.message.assistant {
|
||||
align-self: flex-start;
|
||||
background: var(--bg-card);
|
||||
border-bottom-left-radius: 2px;
|
||||
}
|
||||
|
||||
.message .message-role {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
/* 思考内容折叠 */
|
||||
.thinking-toggle {
|
||||
font-size: 12px;
|
||||
color: var(--primary);
|
||||
cursor: pointer;
|
||||
margin-top: 8px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.thinking-content {
|
||||
display: none;
|
||||
margin-top: 8px;
|
||||
padding: 12px;
|
||||
background: rgba(0,0,0,0.2);
|
||||
border-radius: var(--radius);
|
||||
font-size: 13px;
|
||||
color: var(--text-secondary);
|
||||
border-left: 3px solid var(--primary);
|
||||
}
|
||||
|
||||
.thinking-content.expanded {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* 打字机光标 */
|
||||
.typing-cursor::after {
|
||||
content: '▊';
|
||||
animation: blink 1s step-end infinite;
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
@keyframes blink {
|
||||
50% { opacity: 0; }
|
||||
}
|
||||
|
||||
/* 输入区域 */
|
||||
.input-area {
|
||||
padding: 16px;
|
||||
background: var(--bg-secondary);
|
||||
border-top: 1px solid var(--border-color);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.input-wrapper {
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
gap: 8px;
|
||||
background: var(--bg-input);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius);
|
||||
padding: 8px 12px;
|
||||
}
|
||||
|
||||
.input-wrapper textarea {
|
||||
flex: 1;
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-primary);
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
resize: none;
|
||||
max-height: 120px;
|
||||
min-height: 24px;
|
||||
font-family: var(--font-family);
|
||||
}
|
||||
|
||||
.input-wrapper textarea:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.input-wrapper button {
|
||||
background: var(--primary);
|
||||
border: none;
|
||||
color: white;
|
||||
padding: 8px 12px;
|
||||
border-radius: var(--radius);
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.input-wrapper button:hover {
|
||||
background: var(--primary-hover);
|
||||
}
|
||||
|
||||
.input-wrapper button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* 文件预览 */
|
||||
.file-preview {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
padding: 8px 0;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.file-preview-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 4px 8px;
|
||||
background: var(--bg-card);
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.file-preview-item .remove-file {
|
||||
cursor: pointer;
|
||||
color: var(--danger);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
/* 空状态 */
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
color: var(--text-secondary);
|
||||
padding: 60px 20px;
|
||||
}
|
||||
|
||||
.empty-state h3 {
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
/* 响应式 */
|
||||
@media (max-width: 768px) {
|
||||
.sidebar {
|
||||
position: fixed;
|
||||
z-index: 100;
|
||||
height: 100vh;
|
||||
}
|
||||
.message {
|
||||
max-width: 95%;
|
||||
}
|
||||
.toolbar {
|
||||
flex-wrap: wrap;
|
||||
height: auto;
|
||||
padding: 8px 12px;
|
||||
}
|
||||
}
|
||||
127
public/assets/css/markdown.css
Normal file
127
public/assets/css/markdown.css
Normal file
@@ -0,0 +1,127 @@
|
||||
/* 消息中的 Markdown 内容 */
|
||||
.message.assistant .markdown-body {
|
||||
font-size: 14px;
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
.markdown-body p {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.markdown-body p:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.markdown-body h1, .markdown-body h2, .markdown-body h3,
|
||||
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
|
||||
margin-top: 16px;
|
||||
margin-bottom: 8px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.markdown-body h1 { font-size: 1.4em; }
|
||||
.markdown-body h2 { font-size: 1.3em; }
|
||||
.markdown-body h3 { font-size: 1.2em; }
|
||||
|
||||
.markdown-body ul, .markdown-body ol {
|
||||
margin: 8px 0;
|
||||
padding-left: 24px;
|
||||
}
|
||||
|
||||
.markdown-body li {
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.markdown-body code {
|
||||
background: rgba(255,255,255,0.1);
|
||||
padding: 2px 6px;
|
||||
border-radius: 3px;
|
||||
font-size: 0.9em;
|
||||
font-family: 'Consolas', 'Monaco', monospace;
|
||||
}
|
||||
|
||||
.markdown-body pre {
|
||||
background: #1e1e2e;
|
||||
border-radius: var(--radius);
|
||||
padding: 0;
|
||||
margin: 12px 0;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.markdown-body pre code {
|
||||
display: block;
|
||||
padding: 16px;
|
||||
background: none;
|
||||
overflow-x: auto;
|
||||
font-size: 13px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.markdown-body pre .copy-btn {
|
||||
position: absolute;
|
||||
top: 8px;
|
||||
right: 8px;
|
||||
background: rgba(255,255,255,0.1);
|
||||
border: none;
|
||||
color: var(--text-secondary);
|
||||
padding: 4px 8px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.markdown-body pre .copy-btn:hover {
|
||||
background: rgba(255,255,255,0.2);
|
||||
}
|
||||
|
||||
.markdown-body blockquote {
|
||||
border-left: 4px solid var(--primary);
|
||||
margin: 12px 0;
|
||||
padding: 8px 16px;
|
||||
background: rgba(74, 144, 217, 0.05);
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.markdown-body table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin: 12px 0;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.markdown-body th, .markdown-body td {
|
||||
border: 1px solid var(--border-color);
|
||||
padding: 8px 12px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.markdown-body th {
|
||||
background: var(--bg-secondary);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.markdown-body tr:nth-child(even) {
|
||||
background: rgba(255,255,255,0.02);
|
||||
}
|
||||
|
||||
.markdown-body a {
|
||||
color: var(--primary);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.markdown-body a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.markdown-body hr {
|
||||
border: none;
|
||||
border-top: 1px solid var(--border-color);
|
||||
margin: 16px 0;
|
||||
}
|
||||
|
||||
.markdown-body img {
|
||||
max-width: 100%;
|
||||
border-radius: var(--radius);
|
||||
}
|
||||
773
public/assets/css/style.css
Normal file
773
public/assets/css/style.css
Normal file
@@ -0,0 +1,773 @@
|
||||
/* ========================================
|
||||
CSS 变量
|
||||
======================================== */
|
||||
:root {
|
||||
--primary: #4a90d9;
|
||||
--primary-hover: #357abd;
|
||||
--primary-light: rgba(74, 144, 217, 0.12);
|
||||
--primary-glow: rgba(74, 144, 217, 0.25);
|
||||
--bg-primary: #1a1a2e;
|
||||
--bg-secondary: #16213e;
|
||||
--bg-card: #1e2a45;
|
||||
--bg-input: #0f1b33;
|
||||
--text-primary: #e0e0e0;
|
||||
--text-secondary: #8892a8;
|
||||
--text-hint: #5c6a85;
|
||||
--border-color: #2a3a5c;
|
||||
--border-light: rgba(42, 58, 92, 0.6);
|
||||
--success: #4caf50;
|
||||
--success-bg: rgba(76, 175, 80, 0.1);
|
||||
--danger: #f44336;
|
||||
--danger-bg: rgba(244, 67, 54, 0.1);
|
||||
--warning: #ff9800;
|
||||
--sidebar-width: 260px;
|
||||
--toolbar-height: 50px;
|
||||
--input-height: 120px;
|
||||
--radius: 8px;
|
||||
--radius-lg: 12px;
|
||||
--font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
|
||||
--shadow-sm: 0 2px 8px rgba(0,0,0,0.15);
|
||||
--shadow-md: 0 4px 16px rgba(0,0,0,0.2);
|
||||
--shadow-lg: 0 8px 32px rgba(0,0,0,0.3);
|
||||
--transition: 0.2s ease;
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
Reset
|
||||
======================================== */
|
||||
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
|
||||
body {
|
||||
font-family: var(--font-family);
|
||||
background: var(--bg-primary);
|
||||
color: var(--text-primary);
|
||||
line-height: 1.6;
|
||||
min-height: 100vh;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
登录页
|
||||
======================================== */
|
||||
.login-container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
padding: 20px;
|
||||
background:
|
||||
radial-gradient(ellipse at 20% 50%, rgba(74, 144, 217, 0.08) 0%, transparent 50%),
|
||||
radial-gradient(ellipse at 80% 20%, rgba(74, 144, 217, 0.05) 0%, transparent 50%),
|
||||
linear-gradient(160deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
|
||||
}
|
||||
|
||||
.login-card {
|
||||
background: var(--bg-card);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 48px 40px;
|
||||
width: 100%;
|
||||
max-width: 420px;
|
||||
box-shadow: var(--shadow-lg);
|
||||
border: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.login-logo {
|
||||
text-align: center;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
.login-logo .logo-icon {
|
||||
width: 56px;
|
||||
height: 56px;
|
||||
color: var(--primary);
|
||||
margin-bottom: 16px;
|
||||
filter: drop-shadow(0 2px 8px rgba(74, 144, 217, 0.3));
|
||||
}
|
||||
|
||||
.login-logo h1 {
|
||||
font-size: 26px;
|
||||
color: var(--primary);
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.5px;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.login-subtitle {
|
||||
font-size: 14px;
|
||||
color: var(--text-secondary);
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
安装向导页
|
||||
======================================== */
|
||||
.install-container {
|
||||
max-width: 720px;
|
||||
margin: 0 auto;
|
||||
padding: 48px 24px 32px;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.install-header {
|
||||
text-align: center;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
.install-logo {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.install-logo svg {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
color: var(--primary);
|
||||
filter: drop-shadow(0 2px 8px rgba(74, 144, 217, 0.3));
|
||||
}
|
||||
|
||||
.install-header h1 {
|
||||
font-size: 26px;
|
||||
font-weight: 700;
|
||||
color: var(--text-primary);
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.install-subtitle {
|
||||
font-size: 14px;
|
||||
color: var(--text-secondary);
|
||||
max-width: 400px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.install-footer {
|
||||
text-align: center;
|
||||
padding: 24px 0 8px;
|
||||
margin-top: auto;
|
||||
font-size: 12px;
|
||||
color: var(--text-hint);
|
||||
}
|
||||
|
||||
/* 步骤指示器 */
|
||||
.step-indicator {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 36px;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.step-indicator .step {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 12px 4px 0;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.step-indicator .step::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
height: 3px;
|
||||
background: var(--border-color);
|
||||
border-radius: 2px;
|
||||
transition: background var(--transition);
|
||||
}
|
||||
|
||||
.step-indicator .step.active::after {
|
||||
background: var(--primary);
|
||||
}
|
||||
|
||||
.step-indicator .step.completed::after {
|
||||
background: var(--success);
|
||||
}
|
||||
|
||||
.step-num {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 50%;
|
||||
background: var(--border-color);
|
||||
color: var(--text-secondary);
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
transition: all var(--transition);
|
||||
}
|
||||
|
||||
.step.active .step-num {
|
||||
background: var(--primary);
|
||||
color: #fff;
|
||||
box-shadow: 0 2px 8px rgba(74, 144, 217, 0.35);
|
||||
}
|
||||
|
||||
.step.completed .step-num {
|
||||
background: var(--success);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.step-label {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
white-space: nowrap;
|
||||
transition: color var(--transition);
|
||||
}
|
||||
|
||||
.step.active .step-label {
|
||||
color: var(--primary);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.step.completed .step-label {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
/* 步骤内容卡片 */
|
||||
.step-content {
|
||||
display: none;
|
||||
background: var(--bg-card);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 32px;
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: var(--shadow-sm);
|
||||
animation: stepFadeIn 0.3s ease;
|
||||
}
|
||||
|
||||
.step-content.active {
|
||||
display: block;
|
||||
}
|
||||
|
||||
@keyframes stepFadeIn {
|
||||
from { opacity: 0; transform: translateY(8px); }
|
||||
to { opacity: 1; transform: translateY(0); }
|
||||
}
|
||||
|
||||
.step-header {
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.step-header h2 {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.step-desc {
|
||||
font-size: 13px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
/* 环境检查列表 */
|
||||
.check-list {
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.check-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 12px 16px;
|
||||
border-radius: var(--radius);
|
||||
margin-bottom: 6px;
|
||||
background: var(--bg-secondary);
|
||||
transition: background var(--transition);
|
||||
}
|
||||
|
||||
.check-item:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.check-name {
|
||||
font-size: 14px;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.check-status {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.check-status svg {
|
||||
vertical-align: -2px;
|
||||
}
|
||||
|
||||
.check-item.pass .check-status {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.check-item.fail .check-status {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.check-item.fail {
|
||||
background: var(--danger-bg);
|
||||
border: 1px solid rgba(244, 67, 54, 0.2);
|
||||
}
|
||||
|
||||
.check-item.pass {
|
||||
border: 1px solid transparent;
|
||||
}
|
||||
|
||||
/* 供应商配置卡片 */
|
||||
.provider-item {
|
||||
background: var(--bg-secondary);
|
||||
padding: 20px;
|
||||
border-radius: var(--radius);
|
||||
margin-bottom: 12px;
|
||||
border: 1px solid var(--border-light);
|
||||
}
|
||||
|
||||
.provider-item-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 16px;
|
||||
padding-bottom: 12px;
|
||||
border-bottom: 1px solid var(--border-light);
|
||||
}
|
||||
|
||||
.provider-item-title {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.provider-item .form-group {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.provider-item .form-group:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
通用表单
|
||||
======================================== */
|
||||
.form-group {
|
||||
margin-bottom: 18px;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 6px;
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.form-group input,
|
||||
.form-group select,
|
||||
.form-group textarea {
|
||||
width: 100%;
|
||||
padding: 10px 14px;
|
||||
background: var(--bg-input);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: var(--radius);
|
||||
color: var(--text-primary);
|
||||
font-size: 14px;
|
||||
font-family: inherit;
|
||||
transition: all var(--transition);
|
||||
}
|
||||
|
||||
.form-group input::placeholder,
|
||||
.form-group textarea::placeholder {
|
||||
color: var(--text-hint);
|
||||
}
|
||||
|
||||
.form-group input:focus,
|
||||
.form-group select:focus,
|
||||
.form-group textarea:focus {
|
||||
outline: none;
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 3px var(--primary-light);
|
||||
}
|
||||
|
||||
/* 表单行(并排布局) */
|
||||
.form-row {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.form-row {
|
||||
flex-direction: column;
|
||||
gap: 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* 输入框+按钮行 */
|
||||
.input-action-row {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.input-action-row input {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
/* 表单提示文字 */
|
||||
.form-hint {
|
||||
display: block;
|
||||
margin-top: 4px;
|
||||
font-size: 12px;
|
||||
color: var(--text-hint);
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
按钮
|
||||
======================================== */
|
||||
.btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 10px 20px;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
font-family: inherit;
|
||||
cursor: pointer;
|
||||
transition: all var(--transition);
|
||||
text-decoration: none;
|
||||
line-height: 1.4;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: var(--primary);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background: var(--primary-hover);
|
||||
transform: translateY(-1px);
|
||||
box-shadow: 0 4px 12px rgba(74, 144, 217, 0.35);
|
||||
}
|
||||
|
||||
.btn-primary:active {
|
||||
transform: translateY(0);
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
transform: none;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: var(--bg-secondary);
|
||||
color: var(--text-primary);
|
||||
border: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
border-color: var(--primary);
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.btn-outline {
|
||||
background: transparent;
|
||||
color: var(--primary);
|
||||
border: 1px solid var(--primary);
|
||||
}
|
||||
|
||||
.btn-outline:hover {
|
||||
background: var(--primary-light);
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: var(--danger);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.btn-sm {
|
||||
padding: 6px 14px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.btn-lg {
|
||||
padding: 12px 32px;
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.btn-block {
|
||||
display: flex;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.btn-group {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
margin-top: 28px;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
消息提示
|
||||
======================================== */
|
||||
.alert {
|
||||
padding: 12px 16px;
|
||||
border-radius: var(--radius);
|
||||
margin-bottom: 16px;
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.alert-error {
|
||||
background: var(--danger-bg);
|
||||
border: 1px solid rgba(244, 67, 54, 0.3);
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.alert-success {
|
||||
background: var(--success-bg);
|
||||
border: 1px solid rgba(76, 175, 80, 0.3);
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
配置页
|
||||
======================================== */
|
||||
.config-container {
|
||||
max-width: 900px;
|
||||
margin: 40px auto;
|
||||
padding: 0 20px;
|
||||
}
|
||||
|
||||
.config-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 32px;
|
||||
}
|
||||
|
||||
.config-header h1 {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
font-size: 22px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.config-header h1 svg {
|
||||
color: var(--primary);
|
||||
}
|
||||
|
||||
.config-section {
|
||||
background: var(--bg-card);
|
||||
border-radius: var(--radius-lg);
|
||||
padding: 28px;
|
||||
margin-bottom: 24px;
|
||||
border: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.config-section h2 {
|
||||
margin-bottom: 20px;
|
||||
font-size: 17px;
|
||||
font-weight: 600;
|
||||
padding-bottom: 12px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
响应式
|
||||
======================================== */
|
||||
@media (max-width: 768px) {
|
||||
.login-card {
|
||||
padding: 32px 24px;
|
||||
}
|
||||
|
||||
.install-container {
|
||||
padding: 24px 16px 16px;
|
||||
}
|
||||
|
||||
.step-content {
|
||||
padding: 24px 20px;
|
||||
}
|
||||
|
||||
.step-label {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.step-indicator {
|
||||
gap: 0;
|
||||
}
|
||||
|
||||
.install-header h1 {
|
||||
font-size: 22px;
|
||||
}
|
||||
|
||||
.install-subtitle {
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.config-container {
|
||||
margin: 20px auto;
|
||||
padding: 0 12px;
|
||||
}
|
||||
|
||||
.config-header {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 12px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.login-card {
|
||||
padding: 24px 20px;
|
||||
border-radius: var(--radius);
|
||||
}
|
||||
|
||||
.login-logo .logo-icon {
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
}
|
||||
|
||||
.login-logo h1 {
|
||||
font-size: 22px;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
工具类
|
||||
======================================== */
|
||||
.hidden { display: none !important; }
|
||||
.ml-auto { margin-left: auto; }
|
||||
.mt-sm { margin-top: 12px; }
|
||||
.mt-md { margin-top: 16px; }
|
||||
.mt-lg { margin-top: 24px; }
|
||||
.full-width { width: 100%; }
|
||||
.text-muted { color: var(--text-secondary); }
|
||||
.text-xs { font-size: 12px; }
|
||||
.text-center { text-align: center; }
|
||||
.col-flex-2 { flex: 2; }
|
||||
.col-flex-1 { flex: 1; }
|
||||
|
||||
/* 内联 SVG 图标对齐 */
|
||||
.icon-inline {
|
||||
vertical-align: -2px;
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
.icon-inline-md {
|
||||
vertical-align: -4px;
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
聊天页工具栏组件
|
||||
======================================== */
|
||||
.toolbar-think-label {
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.upload-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-secondary);
|
||||
cursor: pointer;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
.settings-link {
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
配置页组件
|
||||
======================================== */
|
||||
.config-add-btn {
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
.config-section h3 {
|
||||
margin-top: 20px;
|
||||
margin-bottom: 16px;
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.config-empty {
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.config-provider-card {
|
||||
background: var(--bg-secondary);
|
||||
padding: 16px;
|
||||
border-radius: var(--radius);
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.config-provider-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.config-provider-header strong {
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.config-provider-header .toggle-switch {
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
.personality-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 12px 16px;
|
||||
background: var(--bg-secondary);
|
||||
border-radius: var(--radius);
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.personality-icon {
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
.personality-badge {
|
||||
font-size: 12px;
|
||||
margin-left: 8px;
|
||||
}
|
||||
|
||||
.personality-badge.preset {
|
||||
color: var(--warning);
|
||||
}
|
||||
|
||||
.personality-badge.custom {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
/* 固定定位消息提示(配置页用) */
|
||||
.toast-message {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
right: 20px;
|
||||
z-index: 1000;
|
||||
min-width: 200px;
|
||||
}
|
||||
0
public/assets/img/.gitkeep
Normal file
0
public/assets/img/.gitkeep
Normal file
0
public/assets/js/.gitkeep
Normal file
0
public/assets/js/.gitkeep
Normal file
74
public/assets/js/api.js
Normal file
74
public/assets/js/api.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const api = {
|
||||
async request(url, options = {}) {
|
||||
const token = Storage.getToken();
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
...(token ? { 'Authorization': 'Bearer ' + token } : {}),
|
||||
...options.headers
|
||||
};
|
||||
|
||||
const response = await fetch('/api' + url, {
|
||||
...options,
|
||||
headers
|
||||
});
|
||||
|
||||
// 401 自动跳转登录
|
||||
if (response.status === 401) {
|
||||
Storage.clearToken();
|
||||
window.location.href = '/login.php';
|
||||
throw new Error('请先登录');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || '请求失败');
|
||||
}
|
||||
return data;
|
||||
},
|
||||
|
||||
get(url) {
|
||||
return this.request(url, { method: 'GET' });
|
||||
},
|
||||
|
||||
post(url, data) {
|
||||
return this.request(url, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
},
|
||||
|
||||
put(url, data) {
|
||||
return this.request(url, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
},
|
||||
|
||||
delete(url) {
|
||||
return this.request(url, { method: 'DELETE' });
|
||||
},
|
||||
|
||||
// 文件上传(不用 JSON Content-Type)
|
||||
async upload(url, formData) {
|
||||
const token = Storage.getToken();
|
||||
const headers = token ? { 'Authorization': 'Bearer ' + token } : {};
|
||||
|
||||
const response = await fetch('/api' + url, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
Storage.clearToken();
|
||||
window.location.href = '/login.php';
|
||||
throw new Error('请先登录');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
if (!response.ok) {
|
||||
throw new Error(data.message || '上传失败');
|
||||
}
|
||||
return data;
|
||||
}
|
||||
};
|
||||
414
public/assets/js/chat.js
Normal file
414
public/assets/js/chat.js
Normal file
@@ -0,0 +1,414 @@
|
||||
const ChatManager = {
|
||||
isStreaming: false,
|
||||
messages: [],
|
||||
|
||||
init() {
|
||||
const input = document.getElementById('messageInput');
|
||||
if (input) {
|
||||
input.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter' && e.ctrlKey) {
|
||||
e.preventDefault();
|
||||
this.sendMessage();
|
||||
}
|
||||
});
|
||||
// 自适应高度
|
||||
input.addEventListener('input', () => {
|
||||
input.style.height = 'auto';
|
||||
input.style.height = Math.min(input.scrollHeight, 120) + 'px';
|
||||
});
|
||||
}
|
||||
|
||||
const sendBtn = document.getElementById('sendBtn');
|
||||
if (sendBtn) {
|
||||
sendBtn.addEventListener('click', () => this.sendMessage());
|
||||
}
|
||||
|
||||
UploadManager.init();
|
||||
},
|
||||
|
||||
async loadMessages(sessionId) {
|
||||
try {
|
||||
const res = await api.get('/sessions/' + sessionId + '/messages');
|
||||
this.messages = res.data || [];
|
||||
this.renderMessages();
|
||||
Storage.setCachedMessages(sessionId, this.messages);
|
||||
} catch (err) {
|
||||
// 尝试从缓存加载
|
||||
const cached = Storage.getCachedMessages(sessionId);
|
||||
if (cached) {
|
||||
this.messages = cached;
|
||||
this.renderMessages();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
renderMessages() {
|
||||
const container = document.getElementById('messagesContainer');
|
||||
if (!container) return;
|
||||
|
||||
if (this.messages.length === 0) {
|
||||
container.innerHTML = '<div class="empty-state"><h3>开始新的对话</h3><p>输入消息开始聊天</p></div>';
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = this.messages.map(msg => {
|
||||
if (msg.role === 'user') {
|
||||
return `<div class="message user">${this.escapeHtml(msg.content)}</div>`;
|
||||
} else {
|
||||
let html = `<div class="message assistant">`;
|
||||
if (msg.thinking_content) {
|
||||
html += `<div class="thinking-toggle" onclick="this.nextElementSibling.classList.toggle('expanded')">思考过程 ▾</div>`;
|
||||
html += `<div class="thinking-content">${MarkdownRenderer.render(msg.thinking_content)}</div>`;
|
||||
}
|
||||
html += `<div class="markdown-body">${MarkdownRenderer.render(msg.content)}</div>`;
|
||||
html += `</div>`;
|
||||
return html;
|
||||
}
|
||||
}).join('');
|
||||
|
||||
container.scrollTop = container.scrollHeight;
|
||||
},
|
||||
|
||||
async sendMessage() {
|
||||
if (this.isStreaming) return;
|
||||
|
||||
const input = document.getElementById('messageInput');
|
||||
const content = input.value.trim();
|
||||
if (!content) return;
|
||||
|
||||
if (!SessionManager.currentSessionId) {
|
||||
await SessionManager.createSession();
|
||||
}
|
||||
|
||||
// 清空输入
|
||||
input.value = '';
|
||||
input.style.height = 'auto';
|
||||
|
||||
// 构建消息
|
||||
const userMessage = { role: 'user', content };
|
||||
if (UploadManager.getFiles().length > 0) {
|
||||
userMessage.file_info = UploadManager.getFiles();
|
||||
}
|
||||
this.messages.push(userMessage);
|
||||
this.renderMessages();
|
||||
|
||||
// 保存用户消息到数据库
|
||||
api.post('/sessions/' + SessionManager.currentSessionId + '/messages', userMessage).catch(console.error);
|
||||
|
||||
// 清除文件
|
||||
UploadManager.clearFiles();
|
||||
|
||||
// 获取当前配置
|
||||
const provider = document.getElementById('providerSelect')?.value || 'newapi';
|
||||
const model = document.getElementById('modelSelect')?.value || 'gpt-3.5-turbo';
|
||||
const thinkingMode = document.getElementById('thinkingMode')?.checked || false;
|
||||
|
||||
// SSE 流式请求
|
||||
this.isStreaming = true;
|
||||
this.updateSendButton();
|
||||
|
||||
try {
|
||||
await this.streamChat(provider, model, thinkingMode);
|
||||
} catch (err) {
|
||||
this.addErrorMessage(err.message);
|
||||
} finally {
|
||||
this.isStreaming = false;
|
||||
this.updateSendButton();
|
||||
}
|
||||
},
|
||||
|
||||
async streamChat(provider, model, thinkingMode) {
|
||||
const token = Storage.getToken();
|
||||
|
||||
// 构建消息历史(只取 role 和 content)
|
||||
const messages = this.messages.map(m => ({
|
||||
role: m.role,
|
||||
content: m.content
|
||||
}));
|
||||
|
||||
// 添加 AI 消息占位
|
||||
const assistantEl = this.addAssistantPlaceholder();
|
||||
let fullContent = '';
|
||||
let thinkingContent = '';
|
||||
|
||||
const response = await fetch('/api/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + token
|
||||
},
|
||||
body: JSON.stringify({
|
||||
provider, model, messages, stream: true, thinkingMode
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const err = await response.json();
|
||||
throw new Error(err.message || 'AI 请求失败');
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const lines = buffer.split('\n');
|
||||
buffer = lines.pop() || '';
|
||||
|
||||
for (const line of lines) {
|
||||
if (!line.startsWith('data: ')) continue;
|
||||
const data = line.slice(6).trim();
|
||||
|
||||
if (data === '[DONE]') {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
|
||||
if (parsed.type === 'error') {
|
||||
throw new Error(parsed.message);
|
||||
}
|
||||
|
||||
if (parsed.thinking) {
|
||||
thinkingContent += parsed.thinking;
|
||||
this.updateThinkingContent(assistantEl, thinkingContent);
|
||||
}
|
||||
|
||||
if (parsed.content) {
|
||||
fullContent += parsed.content;
|
||||
this.updateAssistantContent(assistantEl, fullContent);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.message && !e.message.includes('JSON')) throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 流结束,保存 AI 消息
|
||||
const assistantMessage = {
|
||||
role: 'assistant',
|
||||
content: fullContent,
|
||||
thinking_content: thinkingContent || null
|
||||
};
|
||||
this.messages.push(assistantMessage);
|
||||
|
||||
// 保存到数据库
|
||||
api.post('/sessions/' + SessionManager.currentSessionId + '/messages', assistantMessage).catch(console.error);
|
||||
|
||||
// 缓存到 localStorage
|
||||
Storage.setCachedMessages(SessionManager.currentSessionId, this.messages);
|
||||
|
||||
// 移除打字机光标
|
||||
this.removeTypingCursor(assistantEl);
|
||||
},
|
||||
|
||||
addAssistantPlaceholder() {
|
||||
const container = document.getElementById('messagesContainer');
|
||||
const empty = container.querySelector('.empty-state');
|
||||
if (empty) empty.remove();
|
||||
|
||||
const el = document.createElement('div');
|
||||
el.className = 'message assistant';
|
||||
el.innerHTML = '<span class="typing-cursor"></span>';
|
||||
container.appendChild(el);
|
||||
container.scrollTop = container.scrollHeight;
|
||||
return el;
|
||||
},
|
||||
|
||||
updateAssistantContent(el, content) {
|
||||
const cursor = el.querySelector('.typing-cursor');
|
||||
const body = el.querySelector('.markdown-body');
|
||||
|
||||
if (body) {
|
||||
body.innerHTML = MarkdownRenderer.render(content);
|
||||
if (cursor) body.appendChild(cursor);
|
||||
} else {
|
||||
const thinking = el.querySelector('.thinking-content, .thinking-toggle');
|
||||
const md = document.createElement('div');
|
||||
md.className = 'markdown-body';
|
||||
md.innerHTML = MarkdownRenderer.render(content);
|
||||
if (cursor) md.appendChild(cursor);
|
||||
|
||||
if (thinking) {
|
||||
el.appendChild(md);
|
||||
} else {
|
||||
el.innerHTML = '';
|
||||
el.appendChild(md);
|
||||
}
|
||||
}
|
||||
|
||||
el.parentElement.scrollTop = el.parentElement.scrollHeight;
|
||||
},
|
||||
|
||||
updateThinkingContent(el, content) {
|
||||
let toggle = el.querySelector('.thinking-toggle');
|
||||
let container = el.querySelector('.thinking-content');
|
||||
|
||||
if (!toggle) {
|
||||
toggle = document.createElement('div');
|
||||
toggle.className = 'thinking-toggle expanded';
|
||||
toggle.textContent = '思考过程 ▾';
|
||||
toggle.onclick = function() {
|
||||
container.classList.toggle('expanded');
|
||||
this.textContent = container.classList.contains('expanded') ? '思考过程 ▴' : '思考过程 ▾';
|
||||
};
|
||||
el.insertBefore(toggle, el.firstChild);
|
||||
}
|
||||
|
||||
if (!container) {
|
||||
container = document.createElement('div');
|
||||
container.className = 'thinking-content expanded';
|
||||
el.insertBefore(container, toggle.nextSibling);
|
||||
}
|
||||
|
||||
container.innerHTML = MarkdownRenderer.render(content);
|
||||
el.parentElement.scrollTop = el.parentElement.scrollHeight;
|
||||
},
|
||||
|
||||
removeTypingCursor(el) {
|
||||
const cursor = el.querySelector('.typing-cursor');
|
||||
if (cursor) cursor.remove();
|
||||
},
|
||||
|
||||
addErrorMessage(message) {
|
||||
const container = document.getElementById('messagesContainer');
|
||||
const el = document.createElement('div');
|
||||
el.className = 'message assistant';
|
||||
el.innerHTML = `<div class="alert alert-error">错误: ${this.escapeHtml(message)} <button class="btn btn-sm btn-secondary" onclick="ChatManager.retryLast()">重试</button></div>`;
|
||||
container.appendChild(el);
|
||||
container.scrollTop = container.scrollHeight;
|
||||
},
|
||||
|
||||
clearMessages() {
|
||||
const container = document.getElementById('messagesContainer');
|
||||
if (container) {
|
||||
container.innerHTML = '<div class="empty-state"><h3>开始新的对话</h3><p>输入消息开始聊天</p></div>';
|
||||
}
|
||||
this.messages = [];
|
||||
},
|
||||
|
||||
updateSendButton() {
|
||||
const btn = document.getElementById('sendBtn');
|
||||
if (btn) {
|
||||
btn.disabled = this.isStreaming;
|
||||
btn.textContent = this.isStreaming ? '回复中...' : '发送';
|
||||
}
|
||||
},
|
||||
|
||||
escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
};
|
||||
|
||||
// ========================================
|
||||
// 聊天页面初始化
|
||||
// ========================================
|
||||
|
||||
// 切换侧边栏
|
||||
function toggleSidebar() {
|
||||
document.getElementById('sidebar').classList.toggle('collapsed');
|
||||
}
|
||||
|
||||
// 供应商/模型数据缓存
|
||||
let providersData = [];
|
||||
let personalitiesData = [];
|
||||
|
||||
// 加载供应商配置
|
||||
async function loadProviders() {
|
||||
try {
|
||||
const res = await api.get('/config');
|
||||
providersData = res.data.providers || [];
|
||||
const select = document.getElementById('providerSelect');
|
||||
select.innerHTML = '<option value="">选择供应商</option>';
|
||||
providersData.forEach((p, i) => {
|
||||
if (p.enabled) {
|
||||
const option = document.createElement('option');
|
||||
option.value = i;
|
||||
option.textContent = p.name;
|
||||
select.appendChild(option);
|
||||
}
|
||||
});
|
||||
// 如果只有一个供应商,自动选择
|
||||
if (providersData.filter(p => p.enabled).length === 1) {
|
||||
select.value = providersData.findIndex(p => p.enabled);
|
||||
onProviderChange();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('加载供应商配置失败:', err);
|
||||
}
|
||||
}
|
||||
|
||||
// 供应商切换时更新模型列表
|
||||
function onProviderChange() {
|
||||
const index = document.getElementById('providerSelect').value;
|
||||
const modelSelect = document.getElementById('modelSelect');
|
||||
modelSelect.innerHTML = '<option value="">选择模型</option>';
|
||||
|
||||
if (index !== '' && providersData[index]) {
|
||||
const provider = providersData[index];
|
||||
(provider.models || []).forEach(m => {
|
||||
const option = document.createElement('option');
|
||||
option.value = m;
|
||||
option.textContent = m;
|
||||
modelSelect.appendChild(option);
|
||||
});
|
||||
// 如果只有一个模型,自动选择
|
||||
if (provider.models && provider.models.length === 1) {
|
||||
modelSelect.value = provider.models[0];
|
||||
}
|
||||
// 如果有默认模型,自动选择
|
||||
if (provider.defaultModel) {
|
||||
modelSelect.value = provider.defaultModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 加载人格列表
|
||||
async function loadPersonalities() {
|
||||
try {
|
||||
const res = await api.get('/personalities');
|
||||
personalitiesData = res.data || [];
|
||||
const select = document.getElementById('personalitySelect');
|
||||
select.innerHTML = '<option value="">默认人格</option>';
|
||||
personalitiesData.forEach(p => {
|
||||
const option = document.createElement('option');
|
||||
option.value = p.id;
|
||||
option.textContent = (p.icon ? p.icon + ' ' : '') + p.name;
|
||||
select.appendChild(option);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('加载人格列表失败:', err);
|
||||
}
|
||||
}
|
||||
|
||||
// 页面初始化
|
||||
(async function() {
|
||||
// 检查认证
|
||||
const token = Storage.getToken();
|
||||
if (!token) {
|
||||
window.location.href = '/login.php';
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化聊天管理器
|
||||
ChatManager.init();
|
||||
|
||||
// 加载数据
|
||||
await Promise.all([
|
||||
loadProviders(),
|
||||
loadPersonalities(),
|
||||
SessionManager.loadSessions()
|
||||
]);
|
||||
|
||||
// 如果有会话,自动选择第一个
|
||||
if (SessionManager.sessions.length > 0) {
|
||||
await SessionManager.switchSession(SessionManager.sessions[0].id);
|
||||
}
|
||||
})();
|
||||
203
public/assets/js/config.js
Normal file
203
public/assets/js/config.js
Normal file
@@ -0,0 +1,203 @@
|
||||
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 class="config-empty">暂无供应商配置</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
list.innerHTML = this.providers.map((p, i) => `
|
||||
<div class="config-provider-card">
|
||||
<div class="config-provider-header">
|
||||
<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})">删除</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 class="config-empty">暂无人格</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
list.innerHTML = this.personalities.map(p => `
|
||||
<div class="personality-item">
|
||||
<div>
|
||||
<span>${p.icon ? '<span class="personality-icon">' + this.escapeHtml(p.icon) + '</span>' : ''}${this.escapeHtml(p.name)}</span>
|
||||
${p.is_preset ? '<span class="personality-badge preset">预设</span>' : '<span class="personality-badge custom">自定义</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'} toast-message">${this.escapeHtml(text)}</div>`;
|
||||
setTimeout(() => { el.innerHTML = ''; }, 3000);
|
||||
},
|
||||
|
||||
escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
};
|
||||
|
||||
ConfigPage.init();
|
||||
201
public/assets/js/install.js
Normal file
201
public/assets/js/install.js
Normal file
@@ -0,0 +1,201 @@
|
||||
const InstallWizard = {
|
||||
currentStep: 1,
|
||||
totalSteps: 5,
|
||||
|
||||
init() {
|
||||
this.checkEnv();
|
||||
this.addProvider(); // 默认添加一个供应商表单
|
||||
},
|
||||
|
||||
checkEnv() {
|
||||
const checks = document.getElementById('envCheckData');
|
||||
if (!checks) return;
|
||||
|
||||
const data = JSON.parse(checks.textContent);
|
||||
|
||||
const container = document.getElementById('envChecks');
|
||||
container.innerHTML = data.map(c => `
|
||||
<div class="check-item ${c.pass ? 'pass' : 'fail'}">
|
||||
<span class="check-name">${c.name}</span>
|
||||
<span class="check-status">
|
||||
${c.pass
|
||||
? '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"/></svg> 通过'
|
||||
: '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6L6 18M6 6l12 12"/></svg> 未通过'
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
`).join('');
|
||||
},
|
||||
|
||||
nextStep() {
|
||||
if (this.currentStep === 4) {
|
||||
// 验证管理员密码
|
||||
const pwd = document.getElementById('adminPassword').value;
|
||||
const confirm = document.getElementById('adminPasswordConfirm').value;
|
||||
const username = document.getElementById('adminUsername').value;
|
||||
if (!username) { alert('请输入管理员用户名'); return; }
|
||||
if (pwd.length < 6) { alert('密码至少6位'); return; }
|
||||
if (pwd !== confirm) { alert('两次密码不一致'); return; }
|
||||
}
|
||||
if (this.currentStep < this.totalSteps) {
|
||||
this.currentStep++;
|
||||
this.updateSteps();
|
||||
}
|
||||
},
|
||||
|
||||
prevStep() {
|
||||
if (this.currentStep > 1) {
|
||||
this.currentStep--;
|
||||
this.updateSteps();
|
||||
}
|
||||
},
|
||||
|
||||
updateSteps() {
|
||||
document.querySelectorAll('.step-content').forEach((el, i) => {
|
||||
el.classList.toggle('active', i + 1 === this.currentStep);
|
||||
});
|
||||
document.querySelectorAll('.step-indicator .step').forEach((el, i) => {
|
||||
el.classList.remove('active', 'completed');
|
||||
if (i + 1 === this.currentStep) el.classList.add('active');
|
||||
if (i + 1 < this.currentStep) el.classList.add('completed');
|
||||
});
|
||||
},
|
||||
|
||||
async testDb() {
|
||||
const result = document.getElementById('dbTestResult');
|
||||
try {
|
||||
const response = await fetch('/api/install/test-db', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
host: document.getElementById('dbHost').value,
|
||||
port: parseInt(document.getElementById('dbPort').value),
|
||||
user: document.getElementById('dbUser').value,
|
||||
password: document.getElementById('dbPassword').value,
|
||||
database: document.getElementById('dbName').value
|
||||
})
|
||||
});
|
||||
const data = await response.json();
|
||||
result.innerHTML = `<div class="alert ${data.success ? 'alert-success' : 'alert-error'}">${data.message}</div>`;
|
||||
} catch (err) {
|
||||
result.innerHTML = '<div class="alert alert-error">连接失败: ' + err.message + '</div>';
|
||||
}
|
||||
},
|
||||
|
||||
addProvider() {
|
||||
const list = document.getElementById('providerList');
|
||||
const index = list.children.length;
|
||||
const html = `
|
||||
<div class="provider-item" data-index="${index}">
|
||||
<div class="provider-item-header">
|
||||
<span class="provider-item-title">供应商 #${index + 1}</span>
|
||||
${index > 0 ? '<button class="btn btn-danger btn-sm" onclick="this.closest(\'.provider-item\').remove()">删除</button>' : ''}
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<div class="form-group col-flex-1">
|
||||
<label>供应商名称</label>
|
||||
<input type="text" class="provider-name" placeholder="如:OpenAI、DeepSeek">
|
||||
</div>
|
||||
<div class="form-group col-flex-1">
|
||||
<label>供应商类型</label>
|
||||
<select class="provider-type">
|
||||
<option value="newapi">OpenAI 兼容</option>
|
||||
<option value="openai">OpenAI 官方</option>
|
||||
<option value="claude">Claude (Anthropic)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>API URL</label>
|
||||
<input type="text" class="provider-url" placeholder="如:https://api.openai.com">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>API Key</label>
|
||||
<input type="password" class="provider-key" placeholder="API 密钥">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>可用模型(逗号分隔)</label>
|
||||
<input type="text" class="provider-models" placeholder="如:gpt-3.5-turbo, gpt-4">
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
list.insertAdjacentHTML('beforeend', html);
|
||||
},
|
||||
|
||||
generateSecret() {
|
||||
const chars = '0123456789abcdef';
|
||||
let result = '';
|
||||
for (let i = 0; i < 64; i++) {
|
||||
result += chars[Math.floor(Math.random() * chars.length)];
|
||||
}
|
||||
return result;
|
||||
},
|
||||
|
||||
async runInstall() {
|
||||
const btn = document.getElementById('installBtn');
|
||||
const result = document.getElementById('installResult');
|
||||
|
||||
// 收集供应商数据
|
||||
const providers = [];
|
||||
document.querySelectorAll('.provider-item').forEach(item => {
|
||||
const models = item.querySelector('.provider-models').value.split(',').map(m => m.trim()).filter(m => m);
|
||||
providers.push({
|
||||
name: item.querySelector('.provider-name').value,
|
||||
apiUrl: item.querySelector('.provider-url').value,
|
||||
apiKey: item.querySelector('.provider-key').value,
|
||||
models: models,
|
||||
type: item.querySelector('.provider-type').value,
|
||||
enabled: true
|
||||
});
|
||||
});
|
||||
|
||||
if (providers.length === 0 || !providers[0].name || !providers[0].apiKey) {
|
||||
alert('请至少配置一个完整的供应商');
|
||||
return;
|
||||
}
|
||||
|
||||
btn.disabled = true;
|
||||
btn.textContent = '安装中...';
|
||||
|
||||
const setupData = {
|
||||
username: document.getElementById('adminUsername').value,
|
||||
password: document.getElementById('adminPassword').value,
|
||||
dbConfig: {
|
||||
host: document.getElementById('dbHost').value,
|
||||
port: parseInt(document.getElementById('dbPort').value),
|
||||
user: document.getElementById('dbUser').value,
|
||||
password: document.getElementById('dbPassword').value,
|
||||
database: document.getElementById('dbName').value
|
||||
},
|
||||
appConfig: {
|
||||
jwtSecret: document.getElementById('jwtSecret').value || undefined,
|
||||
jwtExpiry: parseInt(document.getElementById('jwtExpiry').value) || 86400
|
||||
},
|
||||
providers: providers
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/install/setup', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(setupData)
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
result.innerHTML = '<div class="alert alert-success">安装成功!正在跳转到登录页...</div>';
|
||||
setTimeout(() => { window.location.href = '/login.php'; }, 2000);
|
||||
} else {
|
||||
result.innerHTML = '<div class="alert alert-error">安装失败: ' + data.message + '</div>';
|
||||
btn.disabled = false;
|
||||
btn.textContent = '完成安装';
|
||||
}
|
||||
} catch (err) {
|
||||
result.innerHTML = '<div class="alert alert-error">安装失败: ' + err.message + '</div>';
|
||||
btn.disabled = false;
|
||||
btn.textContent = '完成安装';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
InstallWizard.init();
|
||||
50
public/assets/js/login.js
Normal file
50
public/assets/js/login.js
Normal file
@@ -0,0 +1,50 @@
|
||||
// 页面加载时检查是否已登录
|
||||
(function() {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
window.location.href = '/chat.php';
|
||||
}
|
||||
})();
|
||||
|
||||
// 登录表单提交
|
||||
document.getElementById('loginForm').addEventListener('submit', async function(e) {
|
||||
e.preventDefault();
|
||||
const btn = document.getElementById('loginBtn');
|
||||
const errorEl = document.getElementById('loginError');
|
||||
const username = document.getElementById('username').value.trim();
|
||||
const password = document.getElementById('password').value;
|
||||
|
||||
if (!username || !password) {
|
||||
errorEl.textContent = '请输入用户名和密码';
|
||||
errorEl.classList.remove('hidden');
|
||||
return;
|
||||
}
|
||||
|
||||
btn.disabled = true;
|
||||
btn.textContent = '登录中...';
|
||||
errorEl.classList.add('hidden');
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/auth/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
localStorage.setItem('token', data.data.token);
|
||||
window.location.href = '/chat.php';
|
||||
} else {
|
||||
errorEl.textContent = data.message || '登录失败';
|
||||
errorEl.classList.remove('hidden');
|
||||
}
|
||||
} catch (err) {
|
||||
errorEl.textContent = '网络错误,请稍后重试';
|
||||
errorEl.classList.remove('hidden');
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
btn.textContent = '登录';
|
||||
}
|
||||
});
|
||||
34
public/assets/js/markdown.js
Normal file
34
public/assets/js/markdown.js
Normal file
@@ -0,0 +1,34 @@
|
||||
const MarkdownRenderer = {
|
||||
init() {
|
||||
// 配置 marked.js
|
||||
marked.setOptions({
|
||||
highlight: function(code, lang) {
|
||||
if (lang && hljs.getLanguage(lang)) {
|
||||
return hljs.highlight(code, { language: lang }).value;
|
||||
}
|
||||
return hljs.highlightAuto(code).value;
|
||||
},
|
||||
breaks: true,
|
||||
gfm: true
|
||||
});
|
||||
},
|
||||
|
||||
render(text) {
|
||||
if (!text) return '';
|
||||
let html = marked.parse(text);
|
||||
// 为代码块添加复制按钮
|
||||
html = html.replace(/<pre><code/g, '<pre><button class="copy-btn" onclick="MarkdownRenderer.copyCode(this)">复制</button><code');
|
||||
return html;
|
||||
},
|
||||
|
||||
copyCode(btn) {
|
||||
const code = btn.nextElementSibling;
|
||||
navigator.clipboard.writeText(code.textContent).then(() => {
|
||||
btn.textContent = '已复制';
|
||||
setTimeout(() => { btn.textContent = '复制'; }, 2000);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// 初始化
|
||||
MarkdownRenderer.init();
|
||||
72
public/assets/js/session.js
Normal file
72
public/assets/js/session.js
Normal file
@@ -0,0 +1,72 @@
|
||||
const SessionManager = {
|
||||
sessions: [],
|
||||
currentSessionId: null,
|
||||
|
||||
async loadSessions() {
|
||||
try {
|
||||
const res = await api.get('/sessions');
|
||||
this.sessions = res.data || [];
|
||||
this.renderSessionList();
|
||||
} catch (err) {
|
||||
console.error('加载会话失败:', err);
|
||||
}
|
||||
},
|
||||
|
||||
renderSessionList() {
|
||||
const list = document.getElementById('sessionList');
|
||||
if (!list) return;
|
||||
|
||||
list.innerHTML = this.sessions.map(s => `
|
||||
<div class="session-item ${s.id === this.currentSessionId ? 'active' : ''}"
|
||||
onclick="SessionManager.switchSession(${s.id})">
|
||||
<span class="session-name">${this.escapeHtml(s.name)}</span>
|
||||
<button class="session-delete" onclick="event.stopPropagation(); SessionManager.deleteSession(${s.id})" title="删除">×</button>
|
||||
</div>
|
||||
`).join('');
|
||||
},
|
||||
|
||||
async createSession() {
|
||||
try {
|
||||
const res = await api.post('/sessions', {});
|
||||
this.sessions.unshift(res.data);
|
||||
this.currentSessionId = res.data.id;
|
||||
this.renderSessionList();
|
||||
ChatManager.clearMessages();
|
||||
return res.data;
|
||||
} catch (err) {
|
||||
console.error('创建会话失败:', err);
|
||||
}
|
||||
},
|
||||
|
||||
async switchSession(id) {
|
||||
this.currentSessionId = id;
|
||||
this.renderSessionList();
|
||||
await ChatManager.loadMessages(id);
|
||||
},
|
||||
|
||||
async deleteSession(id) {
|
||||
if (!confirm('确定要删除这个会话吗?')) return;
|
||||
try {
|
||||
await api.delete('/sessions/' + id);
|
||||
this.sessions = this.sessions.filter(s => s.id !== id);
|
||||
Storage.clearCachedMessages(id);
|
||||
|
||||
if (this.currentSessionId === id) {
|
||||
this.currentSessionId = null;
|
||||
ChatManager.clearMessages();
|
||||
if (this.sessions.length > 0) {
|
||||
await this.switchSession(this.sessions[0].id);
|
||||
}
|
||||
}
|
||||
this.renderSessionList();
|
||||
} catch (err) {
|
||||
alert('删除会话失败: ' + err.message);
|
||||
}
|
||||
},
|
||||
|
||||
escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
};
|
||||
22
public/assets/js/storage.js
Normal file
22
public/assets/js/storage.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const Storage = {
|
||||
getToken() {
|
||||
return localStorage.getItem('token');
|
||||
},
|
||||
setToken(token) {
|
||||
localStorage.setItem('token', token);
|
||||
},
|
||||
clearToken() {
|
||||
localStorage.removeItem('token');
|
||||
},
|
||||
// 消息缓存
|
||||
getCachedMessages(sessionId) {
|
||||
const data = localStorage.getItem('messages_' + sessionId);
|
||||
return data ? JSON.parse(data) : null;
|
||||
},
|
||||
setCachedMessages(sessionId, messages) {
|
||||
localStorage.setItem('messages_' + sessionId, JSON.stringify(messages));
|
||||
},
|
||||
clearCachedMessages(sessionId) {
|
||||
localStorage.removeItem('messages_' + sessionId);
|
||||
}
|
||||
};
|
||||
67
public/assets/js/upload.js
Normal file
67
public/assets/js/upload.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const UploadManager = {
|
||||
files: [], // 待发送的文件列表
|
||||
allowedTypes: '.jpg,.jpeg,.png,.gif,.webp,.js,.ts,.py,.java,.cpp,.c,.html,.css,.json,.xml,.txt,.md,.go,.rs,.php,.rb,.sql,.yaml,.yml,.sh,.bat',
|
||||
|
||||
init() {
|
||||
const btn = document.getElementById('uploadBtn');
|
||||
if (btn) {
|
||||
btn.addEventListener('click', () => this.openFileSelector());
|
||||
}
|
||||
},
|
||||
|
||||
openFileSelector() {
|
||||
const input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = this.allowedTypes;
|
||||
input.multiple = true;
|
||||
input.onchange = (e) => this.handleFiles(e.target.files);
|
||||
input.click();
|
||||
},
|
||||
|
||||
async handleFiles(fileList) {
|
||||
for (const file of fileList) {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
try {
|
||||
const res = await api.upload('/upload', formData);
|
||||
this.files.push(res.data);
|
||||
this.renderPreview();
|
||||
} catch (err) {
|
||||
alert('文件上传失败: ' + err.message);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
renderPreview() {
|
||||
const preview = document.getElementById('filePreview');
|
||||
if (!preview) return;
|
||||
|
||||
preview.innerHTML = this.files.map((f, i) => `
|
||||
<div class="file-preview-item">
|
||||
<span>${this.escapeHtml(f.name)}</span>
|
||||
<span class="remove-file" onclick="UploadManager.removeFile(${i})">×</span>
|
||||
</div>
|
||||
`).join('');
|
||||
},
|
||||
|
||||
removeFile(index) {
|
||||
this.files.splice(index, 1);
|
||||
this.renderPreview();
|
||||
},
|
||||
|
||||
getFiles() {
|
||||
return this.files;
|
||||
},
|
||||
|
||||
clearFiles() {
|
||||
this.files = [];
|
||||
this.renderPreview();
|
||||
},
|
||||
|
||||
escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
};
|
||||
@@ -11,7 +11,7 @@ $envChecks = json_encode([
|
||||
['name' => 'PHP 版本 >= 8.0', 'pass' => version_compare(PHP_VERSION, '8.0.0', '>=')],
|
||||
['name' => 'PDO 扩展', 'pass' => extension_loaded('pdo')],
|
||||
['name' => 'cURL 扩展', 'pass' => extension_loaded('curl')],
|
||||
['name' => 'uploads/ 目录可写', 'pass' => is_writable(__DIR__ . '/../uploads')],
|
||||
['name' => 'uploads/ 目录可写', 'pass' => is_writable(__DIR__ . '/uploads')],
|
||||
['name' => 'config/ 目录可写', 'pass' => is_writable(__DIR__ . '/../config')],
|
||||
]);
|
||||
?>
|
||||
|
||||
0
public/uploads/.gitkeep
Normal file
0
public/uploads/.gitkeep
Normal file
Reference in New Issue
Block a user