chore: update code 周一-2025-11 17:11

This commit is contained in:
赵杰 Jie Zhao (雄狮汽车科技)
2025-11-03 17:11:28 +08:00
parent 37fb88b042
commit 20bbda37e9
4 changed files with 129 additions and 99 deletions

View File

@@ -1,8 +1,8 @@
// 推荐功能脚本
// 推荐功能脚本
let currentUserId = null;
// DOM元素
// DOM元素
const loginSection = document.getElementById('loginSection');
const requestSection = document.getElementById('requestSection');
const recommendationsSection = document.getElementById('recommendationsSection');
@@ -14,27 +14,28 @@ const getRecommendationBtn = document.getElementById('getRecommendationBtn');
const mealTypeSelect = document.getElementById('mealType');
const recommendationsList = document.getElementById('recommendationsList');
// 显示消息
// 显示消息
function showMessage(message, type = 'info') {
messageArea.textContent = message;
messageArea.className = `message-area ${type}`;
messageArea.style.display = 'block';
setTimeout(() => {
messageArea.className = 'message-area';
messageArea.style.display = 'none';
}, 3000);
}
// 用户登录
// 用户登录
loginBtn.addEventListener('click', async () => {
const userId = userIdInput.value.trim();
if (!userId) {
showMessage('请输入用户ID', 'error');
showMessage('请输入用户ID', 'error');
return;
}
loginBtn.disabled = true;
loginBtn.textContent = '登录中...';
loginBtn.textContent = '登录中...';
try {
const response = await fetch('/api/user/login', {
@@ -53,25 +54,25 @@ loginBtn.addEventListener('click', async () => {
currentUserId = userId;
loginSection.style.display = 'none';
requestSection.style.display = 'block';
showMessage(`欢迎,${data.name || userId}`, 'success');
showMessage(`欢迎,${data.name || userId}`, 'success');
} else {
showMessage(data.message || '登录失败', 'error');
showMessage(data.message || '登录失败', 'error');
}
} catch (error) {
console.error('登录失败:', error);
showMessage('登录失败,请检查网络连接', 'error');
console.error('登录失败:', error);
showMessage('登录失败,请检查网络连接', 'error');
} finally {
loginBtn.disabled = false;
loginBtn.textContent = '登录';
loginBtn.textContent = '登录';
}
});
// 获取推荐
// 获取推荐
getRecommendationBtn.addEventListener('click', async () => {
const mealType = mealTypeSelect.value;
getRecommendationBtn.disabled = true;
getRecommendationBtn.textContent = '获取中...';
getRecommendationBtn.textContent = '获取中...';
try {
const response = await fetch('/api/recommendation/get', {
@@ -92,20 +93,20 @@ getRecommendationBtn.addEventListener('click', async () => {
if (data.success && data.recommendations && data.recommendations.length > 0) {
displayRecommendations(data.recommendations);
recommendationsSection.style.display = 'block';
showMessage('推荐获取成功!', 'success');
showMessage('推荐获取成功!', 'success');
} else {
showMessage(data.message || '暂无推荐,请先完善个人数据', 'info');
showMessage(data.message || '暂无推荐内容,请先完善个人数据', 'info');
}
} catch (error) {
console.error('获取推荐失败:', error);
showMessage('获取推荐失败,请检查网络连接', 'error');
console.error('获取推荐失败:', error);
showMessage('获取推荐失败,请检查网络连接', 'error');
} finally {
getRecommendationBtn.disabled = false;
getRecommendationBtn.textContent = '获取推荐';
getRecommendationBtn.textContent = '获取推荐';
}
});
// 显示推荐结果
// 显示推荐结果
function displayRecommendations(recommendations) {
recommendationsList.innerHTML = '';
@@ -120,16 +121,34 @@ function displayRecommendations(recommendations) {
foods = [rec.food];
}
const foodsHtml = foods.map(food =>
`<div class="food-item">${escapeHtml(food)}</div>`
).join('');
const confidenceHtml = rec.confidence
? `<div class="confidence">置信度: ${(rec.confidence * 100).toFixed(1)}%</div>`
: '';
const reasonHtml = rec.reason
? `<div style="margin-top: 10px; color: #666;">推荐理由: ${escapeHtml(rec.reason)}</div>`
: '';
card.innerHTML = `
<h3>推荐方案 ${index + 1}</h3>
<h3>推荐方案 ${index + 1}</h3>
<div class="food-list">
${foods.map(food => `<div class="food-item">${food}</div>`).join('')}
${foodsHtml}
</div>
${rec.confidence ? `<div class="confidence">置信度: ${(rec.confidence * 100).toFixed(1)}%</div>` : ''}
${rec.reason ? `<div style="margin-top: 10px; color: #666;">推荐理由: ${rec.reason}</div>` : ''}
${confidenceHtml}
${reasonHtml}
`;
recommendationsList.appendChild(card);
});
}
// HTML转义函数防止XSS攻击
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}