合并 #71

Open
hzd123456 wants to merge 41 commits from likecheng22/gitlink_help_center:hezhendong-branch into master
2 changed files with 203 additions and 8 deletions
Showing only changes of commit 9873d915a2 - Show all commits

View File

@ -2,29 +2,107 @@ import React, { useState } from 'react';
import styles from './styles.module.css';
const MAX_LENGTH = 500;
const AI_API_ENDPOINT = 'https://api.moonshot.cn/v1/chat/completions';
const AI_API_KEY = 'sk-qIIXV0G8hLyXRku8848tL4n2bwx8rf1jrok4LFramQiZQOQ8';
// 处理文本格式,移除 Markdown 符号
const formatText = (text) => {
return text
.replace(/`/g, '') // 移除所有反引号
.replace(/^#+\s*/gm, '') // 移除标题符号
.replace(/\*\*(.*?)\*\*/g, '$1') // 移除加粗符号
.replace(/\*(.*?)\*/g, '$1') // 移除斜体符号
.replace(/\[(.*?)\]\((.*?)\)/g, '$1') // 移除链接符号
.replace(/^\s*[-*+]\s*/gm, '• ') // 将列表符号统一为圆点
.replace(/^\s*\d+\.\s*/gm, '• ') // 将数字列表转换为圆点
.replace(/\n{3,}/g, '\n\n') // 将多个空行替换为两个空行
.trim();
};
const FloatingButtons = () => {
const [showModal, setShowModal] = useState(false);
const [showAIModal, setShowAIModal] = useState(false);
const [feedback, setFeedback] = useState('');
const [aiQuestion, setAiQuestion] = useState('');
const [aiAnswer, setAiAnswer] = useState('');
const [isLoading, setIsLoading] = useState(false);
const handleFeedback = () => {
setShowModal(true);
};
const handleCustomerService = () => {
setShowAIModal(true);
};
const handleClose = () => {
setShowModal(false);
setFeedback('');
};
const handleAIClose = () => {
setShowAIModal(false);
setAiQuestion('');
setAiAnswer('');
};
const handleChange = (e) => {
if (e.target.value.length <= MAX_LENGTH) {
setFeedback(e.target.value);
}
};
const handleAIQuestionChange = (e) => {
setAiQuestion(e.target.value);
};
const handleAISubmit = async (e) => {
e.preventDefault();
if (!aiQuestion.trim()) return;
setIsLoading(true);
try {
const response = await fetch(AI_API_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${AI_API_KEY}`
},
body: JSON.stringify({
model: "moonshot-v1-8k",
messages: [
{
role: "system",
content: "你是一个专业的 GitLink 帮助助手。请用简洁专业的语言回答用户的问题,不要使用任何 Markdown 格式符号(如 #、*、` 等)。"
},
{
role: "user",
content: aiQuestion
}
],
temperature: 0.7,
max_tokens: 2000
})
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error?.message || `HTTP error! status: ${response.status}`);
}
const data = await response.json();
const answer = data.choices[0].message.content;
setAiAnswer(formatText(answer));
} catch (error) {
console.error('AI 问答出错:', error);
setAiAnswer('抱歉,处理您的问题时出现错误,请稍后重试。');
} finally {
setIsLoading(false);
}
};
const handleSubmit = (e) => {
e.preventDefault();
// localStorage 自动存储
const feedbacks = JSON.parse(localStorage.getItem('feedbacks') || '[]');
feedbacks.push({
content: feedback,
@ -47,6 +125,16 @@ const FloatingButtons = () => {
<path d="M4 19V5a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H7l-3 3z" stroke="#666" strokeWidth="2" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
</svg>
</button>
<button
className={styles.button}
onClick={handleCustomerService}
title="AI 助手"
aria-label="AI 助手"
>
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-1-13h2v6h-2zm0 8h2v2h-2z" fill="#666"/>
</svg>
</button>
{showModal && (
<div className={styles.modalOverlay}>
<div className={styles.modal}>
@ -69,6 +157,32 @@ const FloatingButtons = () => {
</div>
</div>
)}
{showAIModal && (
<div className={styles.modalOverlay}>
<div className={styles.modal}>
<button className={styles.closeBtn} onClick={handleAIClose} title="关闭">×</button>
<h2 className={styles.modalTitle}>AI 助手</h2>
{aiAnswer && (
<div className={styles.aiAnswerFixed}>
<h3>AI 回答</h3>
<p>{aiAnswer}</p>
</div>
)}
<form onSubmit={handleAISubmit} className={styles.formWithAIFixed}>
<textarea
className={styles.textarea}
placeholder="请输入您的问题AI 助手将为您解答"
value={aiQuestion}
onChange={handleAIQuestionChange}
rows={4}
/>
<button type="submit" className={styles.submitBtn} disabled={isLoading}>
{isLoading ? '思考中...' : '发送问题'}
</button>
</form>
</div>
</div>
)}
</div>
);
};

View File

@ -3,6 +3,9 @@
right: 20px;
bottom: 80px;
z-index: 1000;
display: flex;
flex-direction: row;
gap: 12px;
}
.button {
@ -45,6 +48,8 @@
display: flex;
align-items: center;
justify-content: center;
overflow-y: auto;
padding: 20px;
}
.modal {
@ -54,15 +59,17 @@
padding: 32px 24px 24px 24px;
min-width: 400px;
max-width: 90vw;
max-height: 90vh;
position: relative;
display: flex;
flex-direction: column;
align-items: stretch;
overflow-y: auto;
}
.closeBtn {
position: absolute;
top: 12px;
position: sticky;
top: 0;
right: 16px;
background: none;
border: none;
@ -70,14 +77,20 @@
color: #888;
cursor: pointer;
line-height: 1;
z-index: 100;
align-self: flex-end;
margin-bottom: -20px;
}
.modalTitle {
text-align: center;
font-size: 22px;
font-weight: 500;
margin-bottom: 18px;
color: #222;
margin: 0 0 20px;
font-size: 20px;
color: #333;
position: sticky;
top: 0;
background: white;
padding: 10px 0;
z-index: 1;
}
.form {
@ -130,4 +143,72 @@
.submitBtn:hover {
background: #3355cc;
}
.submitBtn:disabled {
background: #a0a0a0;
cursor: not-allowed;
}
.aiAnswer {
margin-top: 20px;
padding: 16px;
background: #f8f9fa;
border-radius: 6px;
border: 1px solid #e9ecef;
overflow-y: auto;
max-height: 400px;
}
.aiAnswer h3 {
margin: 0 0 10px;
font-size: 16px;
color: #333;
position: sticky;
top: 60px;
background: white;
padding: 10px 0;
z-index: 1;
}
.aiAnswer p {
margin: 0;
font-size: 15px;
line-height: 1.6;
color: #333;
white-space: pre-wrap;
}
.cursor {
display: inline-block;
width: 2px;
height: 1em;
background-color: #466aff;
margin-left: 2px;
animation: blink 1s infinite;
vertical-align: middle;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
.aiAnswerFixed {
position: sticky;
top: 80px;
background: #f8f9fa;
border-radius: 6px;
border: 1px solid #e9ecef;
margin-bottom: 16px;
padding: 16px;
z-index: 10;
}
.formWithAIFixed {
display: flex;
flex-direction: column;
gap: 12px;
overflow-y: auto;
max-height: 320px;
}