加载中...
';
try {
let url = `${API_BASE}/posts?category=${currentCategory}`;
if (currentJobType !== 'all') {
url += `&jobType=${encodeURIComponent(currentJobType)}`;
}
if (currentLocation !== 'all') {
url += `&location=${encodeURIComponent(currentLocation)}`;
}
const res = await fetch(url);
const data = await res.json();
// 前端再次过滤确保只显示选中地区的帖子
let posts = data.posts || [];
if (currentLocation !== 'all') {
posts = posts.filter(p => p.location === currentLocation);
}
if (data.success && posts.length) {
list.innerHTML = posts.map(p => `
${escapeHtml(p.title)}
${escapeHtml(p.content)}
${p.images && p.images.length > 0 ? `
${p.images.slice(0,3).map(img => `

`).join('')}${p.images.length > 3 ? `
+${p.images.length-3}` : ''}
` : ''}
${p.phone ? `
📞 ${p.phone}
${p.allowCall ? `
☎️ 打电话` : ''}
${p.allowMessage ? `
💬 发信息` : ''}
` : ''}
`).join('');
} else {
list.innerHTML = '';
for (let i = 1; i <= 2; i++) {
const ad = activeAds.find(a => a.slot === i);
if (ad) {
// Get all images: ad.images array or single ad.image
let allImages = [];
if (ad.images && Array.isArray(ad.images) && ad.images.length > 0) {
allImages = ad.images;
} else if (ad.image) {
allImages = [ad.image];
}
// Filter videos and images
const videos = allImages.filter(img => img && (img.match(/\.(mp4|webm|mov)$/i) || img.startsWith('data:video')));
const images = allImages.filter(img => img && !img.match(/\.(mp4|webm|mov)$/i) && !img.startsWith('data:video'));
const adId = ad._id || `ad-${i}`;
let mediaHtml = '';
// Handle videos - autoplay muted loop
if (videos.length > 0) {
videos.forEach((video, idx) => {
mediaHtml += `
`;
});
}
// Handle images with carousel
if (images.length > 0) {
if (images.length === 1) {
// Single image
mediaHtml += `

`;
} else {
// Multiple images - carousel
const carouselId = `carousel-${adId}`;
mediaHtml += `
`;
// Images container
mediaHtml += `
`;
images.forEach((img, idx) => {
mediaHtml += `

`;
});
mediaHtml += `
`;
// Navigation dots
mediaHtml += `
`;
images.forEach((_, idx) => {
mediaHtml += ``;
});
mediaHtml += `
`;
// Auto-start carousel
mediaHtml += ``;
mediaHtml += `
`;
}
}
// Content and link (no title)
html += `
${mediaHtml}${ad.content ? `
${ad.content}
` : ''}${ad.link ? `
了解更多 →` : ''}
`;
} else {
html += `
广告位 ${i}
`;
}
}
html += '
';
container.innerHTML = html;
}
// Carousel navigation function
window.goToSlide = function(carouselId, index) {
const carousel = document.getElementById(carouselId);
if (!carousel) return;
const images = carousel.querySelectorAll('.carousel-img');
const dots = carousel.querySelectorAll('.carousel-dot');
const total = parseInt(carousel.dataset.total);
images.forEach((img, idx) => {
img.style.opacity = idx === index ? '1' : '0';
});
dots.forEach((dot, idx) => {
dot.style.background = idx === index ? '#1a73e8' : '#ccc';
dot.classList.toggle('active', idx === index);
});
// Reset auto-play
if (window.adCarousels && window.adCarousels[carouselId]) {
clearInterval(window.adCarousels[carouselId]);
window.adCarousels[carouselId] = setInterval(() => {
const currentActive = carousel.querySelector('.carousel-dot.active');
const currentIdx = currentActive ? parseInt(currentActive.dataset.index) : 0;
const nextIdx = (currentIdx + 1) % total;
goToSlide(carouselId, nextIdx);
}, 3000);
}
};
window.startAdCarousel = function(carouselId, totalImages) {
if (!window.adCarousels) window.adCarousels = {};
if (window.adCarousels[carouselId]) {
clearInterval(window.adCarousels[carouselId]);
}
window.adCarousels[carouselId] = setInterval(() => {
const carousel = document.getElementById(carouselId);
if (!carousel) {
clearInterval(window.adCarousels[carouselId]);
delete window.adCarousels[carouselId];
return;
}
const currentActive = carousel.querySelector('.carousel-dot.active');
const currentIdx = currentActive ? parseInt(currentActive.dataset.index) : 0;
const nextIdx = (currentIdx + 1) % totalImages;
goToSlide(carouselId, nextIdx);
}, 3000); // 3 seconds interval
}
function renderCategories() {
const nav = document.getElementById('categoryNav');
let html = `