101 lines
3.9 KiB
JavaScript
101 lines
3.9 KiB
JavaScript
// admin-portfolio.js - 作品集管理页面功能
|
|
|
|
function openAddPortfolioModal() {
|
|
document.getElementById('portfolioId').value = '';
|
|
document.getElementById('portfolioName').value = '';
|
|
document.getElementById('portfolioDescription').value = '';
|
|
document.getElementById('portfolioTechStack').value = '';
|
|
document.getElementById('portfolioURL').value = '';
|
|
document.getElementById('portfolioStartDate').value = '';
|
|
document.getElementById('portfolioEndDate').value = '';
|
|
document.getElementById('portfolioModalTitle').textContent = '添加项目';
|
|
document.getElementById('portfolioModal').style.display = 'flex';
|
|
}
|
|
|
|
function closePortfolioModal() {
|
|
document.getElementById('portfolioModal').style.display = 'none';
|
|
}
|
|
|
|
function editPortfolio(id) {
|
|
fetch(`/api/admin/portfolio/${id}`)
|
|
.then(response => response.json())
|
|
.then(item => {
|
|
document.getElementById('portfolioId').value = item.id;
|
|
document.getElementById('portfolioName').value = item.name;
|
|
document.getElementById('portfolioDescription').value = item.description;
|
|
document.getElementById('portfolioTechStack').value = item.tech_stack ? item.tech_stack.join(', ') : '';
|
|
document.getElementById('portfolioURL').value = item.url || '';
|
|
document.getElementById('portfolioStartDate').value = item.start_date || '';
|
|
document.getElementById('portfolioEndDate').value = item.end_date || '';
|
|
document.getElementById('portfolioModalTitle').textContent = '编辑项目';
|
|
document.getElementById('portfolioModal').style.display = 'flex';
|
|
})
|
|
.catch(error => {
|
|
showCustomAlert('error', '获取失败', '获取项目信息失败');
|
|
});
|
|
}
|
|
|
|
function savePortfolio(event) {
|
|
event.preventDefault();
|
|
|
|
const portfolioId = document.getElementById('portfolioId').value;
|
|
const userId = document.getElementById('portfolioUserId').value;
|
|
const techStack = document.getElementById('portfolioTechStack').value
|
|
.split(',')
|
|
.map(s => s.trim())
|
|
.filter(s => s);
|
|
|
|
const data = {
|
|
user_id: userId,
|
|
name: document.getElementById('portfolioName').value,
|
|
description: document.getElementById('portfolioDescription').value,
|
|
tech_stack: techStack,
|
|
url: document.getElementById('portfolioURL').value,
|
|
start_date: document.getElementById('portfolioStartDate').value,
|
|
end_date: document.getElementById('portfolioEndDate').value
|
|
};
|
|
|
|
const url = portfolioId ? `/api/admin/portfolio/${portfolioId}` : '/api/admin/portfolio';
|
|
const method = portfolioId ? 'PUT' : 'POST';
|
|
|
|
fetch(url, {
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(data)
|
|
})
|
|
.then(response => response.json())
|
|
.then(result => {
|
|
if (result.success) {
|
|
showCustomAlert('success', '保存成功', portfolioId ? '项目更新成功' : '项目添加成功');
|
|
closePortfolioModal();
|
|
window.location.reload();
|
|
} else {
|
|
showCustomAlert('error', '保存失败', result.message || '操作失败');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
showCustomAlert('error', '保存失败', '网络请求失败');
|
|
});
|
|
}
|
|
|
|
function deletePortfolio(id) {
|
|
if (!confirm('确定要删除该项目吗?')) return;
|
|
|
|
fetch(`/api/admin/portfolio/${id}`, {
|
|
method: 'DELETE'
|
|
})
|
|
.then(response => response.json())
|
|
.then(result => {
|
|
if (result.success) {
|
|
showCustomAlert('success', '删除成功', '项目删除成功');
|
|
window.location.reload();
|
|
} else {
|
|
showCustomAlert('error', '删除失败', result.message || '操作失败');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
showCustomAlert('error', '删除失败', '网络请求失败');
|
|
});
|
|
} |