2025-11-03 17:11:28 +08:00
|
|
|
// 数据采集功能脚本
|
2025-11-02 22:23:10 +08:00
|
|
|
|
|
|
|
|
let currentUserId = null;
|
|
|
|
|
|
2025-11-03 17:11:28 +08:00
|
|
|
// DOM元素
|
2025-11-02 22:23:10 +08:00
|
|
|
const loginSection = document.getElementById('loginSection');
|
|
|
|
|
const basicQuestionnaire = document.getElementById('basicQuestionnaire');
|
|
|
|
|
const mealRecord = document.getElementById('mealRecord');
|
|
|
|
|
const messageArea = document.getElementById('messageArea');
|
|
|
|
|
|
|
|
|
|
const loginBtn = document.getElementById('loginBtn');
|
|
|
|
|
const userIdInput = document.getElementById('userId');
|
|
|
|
|
const userNameInput = document.getElementById('userName');
|
|
|
|
|
|
|
|
|
|
const submitBasicBtn = document.getElementById('submitBasicBtn');
|
|
|
|
|
const submitMealBtn = document.getElementById('submitMealBtn');
|
|
|
|
|
|
2025-11-03 17:11:28 +08:00
|
|
|
// 显示消息
|
2025-11-02 22:23:10 +08:00
|
|
|
function showMessage(message, type = 'info') {
|
|
|
|
|
messageArea.textContent = message;
|
|
|
|
|
messageArea.className = `message-area ${type}`;
|
2025-11-03 17:11:28 +08:00
|
|
|
messageArea.style.display = 'block';
|
2025-11-02 22:23:10 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
|
messageArea.className = 'message-area';
|
|
|
|
|
messageArea.style.display = 'none';
|
|
|
|
|
}, 3000);
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-03 17:11:28 +08:00
|
|
|
// 用户登录/注册
|
2025-11-02 22:23:10 +08:00
|
|
|
loginBtn.addEventListener('click', async () => {
|
|
|
|
|
const userId = userIdInput.value.trim();
|
|
|
|
|
const userName = userNameInput.value.trim();
|
|
|
|
|
|
|
|
|
|
if (!userId || !userName) {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage('请输入用户ID和姓名', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loginBtn.disabled = true;
|
2025-11-03 17:11:28 +08:00
|
|
|
loginBtn.textContent = '登录中...';
|
2025-11-02 22:23:10 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/user/register', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
user_id: userId,
|
|
|
|
|
name: userName
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
if (data.success) {
|
|
|
|
|
currentUserId = userId;
|
|
|
|
|
loginSection.style.display = 'none';
|
|
|
|
|
basicQuestionnaire.style.display = 'block';
|
|
|
|
|
mealRecord.style.display = 'block';
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage('注册成功!', 'success');
|
2025-11-02 22:23:10 +08:00
|
|
|
} else {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage(data.message || '注册失败', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-11-03 17:11:28 +08:00
|
|
|
console.error('注册失败:', error);
|
|
|
|
|
showMessage('注册失败,请检查网络连接', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
} finally {
|
|
|
|
|
loginBtn.disabled = false;
|
2025-11-03 17:11:28 +08:00
|
|
|
loginBtn.textContent = '登录/注册';
|
2025-11-02 22:23:10 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-03 17:11:28 +08:00
|
|
|
// 提交基础信息
|
2025-11-02 22:23:10 +08:00
|
|
|
submitBasicBtn.addEventListener('click', async () => {
|
|
|
|
|
const age = document.getElementById('age').value;
|
|
|
|
|
const gender = document.getElementById('gender').value;
|
|
|
|
|
const height = document.getElementById('height').value;
|
|
|
|
|
const weight = document.getElementById('weight').value;
|
|
|
|
|
const activityLevel = document.getElementById('activityLevel').value;
|
|
|
|
|
|
|
|
|
|
if (!age || !gender || !height || !weight || !activityLevel) {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage('请填写完整的基础信息', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
submitBasicBtn.disabled = true;
|
2025-11-03 17:11:28 +08:00
|
|
|
submitBasicBtn.textContent = '提交中...';
|
2025-11-02 22:23:10 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/questionnaire/submit', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
user_id: currentUserId,
|
|
|
|
|
type: 'basic',
|
|
|
|
|
answers: {
|
|
|
|
|
age: parseInt(age),
|
|
|
|
|
gender: gender,
|
|
|
|
|
height: parseFloat(height),
|
|
|
|
|
weight: parseFloat(weight),
|
|
|
|
|
activity_level: activityLevel
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
if (data.success) {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage('基础信息提交成功!', 'success');
|
2025-11-02 22:23:10 +08:00
|
|
|
} else {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage(data.message || '提交失败', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-11-03 17:11:28 +08:00
|
|
|
console.error('提交失败:', error);
|
|
|
|
|
showMessage('提交失败,请检查网络连接', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
} finally {
|
|
|
|
|
submitBasicBtn.disabled = false;
|
2025-11-03 17:11:28 +08:00
|
|
|
submitBasicBtn.textContent = '提交基础信息';
|
2025-11-02 22:23:10 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-03 17:11:28 +08:00
|
|
|
// 记录餐食
|
2025-11-02 22:23:10 +08:00
|
|
|
submitMealBtn.addEventListener('click', async () => {
|
|
|
|
|
const mealDate = document.getElementById('mealDate').value || new Date().toISOString().split('T')[0];
|
|
|
|
|
const mealType = document.getElementById('mealType').value;
|
|
|
|
|
const foodsText = document.getElementById('foods').value;
|
|
|
|
|
const calories = document.getElementById('calories').value;
|
|
|
|
|
const satisfaction = document.getElementById('satisfaction').value;
|
|
|
|
|
|
|
|
|
|
if (!foodsText || !calories) {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage('请填写完整的餐食信息', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-03 17:11:28 +08:00
|
|
|
// 解析食物列表
|
2025-11-02 22:23:10 +08:00
|
|
|
const foods = foodsText.split('\n').filter(line => line.trim());
|
|
|
|
|
const quantities = foods.map(f => {
|
|
|
|
|
const parts = f.trim().split(/\s+/);
|
2025-11-03 17:11:28 +08:00
|
|
|
return parts.length > 1 ? parts.slice(1).join(' ') : '适量';
|
2025-11-02 22:23:10 +08:00
|
|
|
});
|
|
|
|
|
const foodNames = foods.map(f => f.trim().split(/\s+/)[0]);
|
|
|
|
|
|
|
|
|
|
submitMealBtn.disabled = true;
|
2025-11-03 17:11:28 +08:00
|
|
|
submitMealBtn.textContent = '记录中...';
|
2025-11-02 22:23:10 +08:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch('/api/meal/record', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
user_id: currentUserId,
|
|
|
|
|
date: mealDate,
|
|
|
|
|
meal_type: mealType,
|
|
|
|
|
foods: foodNames,
|
|
|
|
|
quantities: quantities,
|
|
|
|
|
calories: parseFloat(calories),
|
|
|
|
|
satisfaction_score: parseInt(satisfaction),
|
|
|
|
|
notes: ''
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
|
|
if (data.success) {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage('餐食记录成功!', 'success');
|
|
|
|
|
// 清空表单
|
2025-11-02 22:23:10 +08:00
|
|
|
document.getElementById('foods').value = '';
|
|
|
|
|
document.getElementById('calories').value = '';
|
|
|
|
|
document.getElementById('satisfaction').value = '3';
|
|
|
|
|
} else {
|
2025-11-03 17:11:28 +08:00
|
|
|
showMessage(data.message || '记录失败', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-11-03 17:11:28 +08:00
|
|
|
console.error('记录失败:', error);
|
|
|
|
|
showMessage('记录失败,请检查网络连接', 'error');
|
2025-11-02 22:23:10 +08:00
|
|
|
} finally {
|
|
|
|
|
submitMealBtn.disabled = false;
|
2025-11-03 17:11:28 +08:00
|
|
|
submitMealBtn.textContent = '记录餐食';
|
2025-11-02 22:23:10 +08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-03 17:11:28 +08:00
|
|
|
// 设置默认日期为今天
|
2025-11-02 22:23:10 +08:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
|
const mealDateInput = document.getElementById('mealDate');
|
|
|
|
|
if (mealDateInput) {
|
|
|
|
|
mealDateInput.value = new Date().toISOString().split('T')[0];
|
|
|
|
|
}
|
|
|
|
|
});
|