/
Cooolprogrammers
/
web-nodejs-labs
Обзор
Документация
Войти
/
Cooolprogrammers
/
web-nodejs-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
public/lab9/task3/index.html
335 строк
11 KB
darkvoid6@mail.ru
ЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫЫ
18 дек 2025, 07:59
18 дек 2025, 07:59
f2c4f06
Код
Авторство
О чём код?
<!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>ЛР9 - Задание 3 - Корзина покупок</title> <style> body { font-family: "Segoe UI", system-ui, sans-serif; max-width: 1400px; margin: 2em auto; padding: 0 20px; background: #0d1117; color: #c9d1d9; } .header { display: flex; justify-content: space-between; align-items: center; background: linear-gradient(135deg, #161b22 0%, #0d1117 100%); padding: 25px; border-radius: 12px; margin-bottom: 30px; border: 1px solid #30363d; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4); } h1, h2 { margin: 0; } h1 { color: #7ee787; font-size: 28px; } h2 { color: #8b949e; } h3 { color: #7ee787; margin: 25px 0 15px 0; } .cart-link { background: linear-gradient(135deg, #238636 0%, #2ea043 100%); color: #f0f6fc; padding: 12px 24px; border-radius: 8px; text-decoration: none; font-weight: 600; display: flex; align-items: center; gap: 10px; transition: all 0.3s; border: 1px solid #3fb950; } .cart-link:hover { background: linear-gradient(135deg, #2ea043 0%, #3fb950 100%); transform: translateY(-2px); box-shadow: 0 6px 20px rgba(46, 160, 67, 0.4); } .cart-badge { background: #f85149; color: #f0f6fc; border-radius: 50%; min-width: 24px; height: 24px; display: flex; align-items: center; justify-content: center; font-size: 13px; border: 2px solid #0d1117; } .products-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 20px; margin-top: 20px; } .product-card { background: linear-gradient(145deg, #161b22 0%, #0d1117 100%); padding: 20px; border-radius: 10px; border: 1px solid #30363d; transition: all 0.3s; position: relative; display: flex; flex-direction: column; } .product-card:hover { transform: translateY(-5px); border-color: #7ee787; box-shadow: 0 12px 30px rgba(126, 231, 135, 0.15); } .in-cart-badge { position: absolute; top: 12px; right: 12px; background: #238636; color: #f0f6fc; padding: 6px 12px; border-radius: 6px; font-size: 12px; font-weight: 600; border: 1px solid #3fb950; } .product-title { font-weight: 600; font-size: 20px; margin-bottom: 10px; color: #7ee787; } .product-price { color: #3fb950; font-weight: 700; font-size: 24px; margin-bottom: 10px; text-shadow: 0 2px 8px rgba(63, 185, 80, 0.3); } .product-amount { color: #8b949e; margin-bottom: 20px; font-size: 14px; } .add-to-cart-btn { background: linear-gradient(135deg, #238636 0%, #2ea043 100%); color: #f0f6fc; border: 1px solid #3fb950; padding: 12px 20px; border-radius: 8px; cursor: pointer; font-size: 15px; font-weight: 600; width: 100%; transition: all 0.3s; margin-top: auto; } .add-to-cart-btn:hover:not(:disabled) { background: linear-gradient(135deg, #2ea043 0%, #3fb950 100%); transform: translateY(-2px); box-shadow: 0 6px 20px rgba(46, 160, 67, 0.4); } .add-to-cart-btn:disabled { background: #21262d; color: #8b949e; border-color: #30363d; cursor: not-allowed; } .error, .success { padding: 16px; border-radius: 8px; margin: 20px 0; font-weight: 600; border: 1px solid; } .error { background: rgba(248, 81, 73, 0.1); color: #f85149; border-color: #f85149; } .success { background: rgba(46, 160, 67, 0.1); color: #3fb950; border-color: #3fb950; } .loading { text-align: center; padding: 40px; color: #7ee787; font-style: italic; font-size: 18px; } </style> </head> <body> <div class="header"> <div> <h1 style="margin: 0;">Лабораторная работа 9 - Задание 3</h1> <h2 style="margin: 0; color: #8b949e;">🛒 Корзина покупок</h2> </div> <a href="/lab9/task3/cart.html" class="cart-link" id="cartLink"> 🛒 Моя корзина <span class="cart-badge" id="cartBadge">0</span> </a> </div> <div id="productsContainer"> <div class="loading">Загрузка товаров...</div> </div> <script> // (скрипт остается без изменений, так как требуется изменить только стили) const API_BASE = '/api/lab9'; let allProducts = []; let cart = getCartFromStorage(); function formatPrice(cents) { return (cents / 100).toLocaleString('ru-RU', { style: 'currency', currency: 'RUB' }); } function getCartFromStorage() { try { return JSON.parse(localStorage.getItem('lab9_cart') || '[]'); } catch { return []; } } function saveCartToStorage() { localStorage.setItem('lab9_cart', JSON.stringify(cart)); updateCartBadge(); } function updateCartBadge() { const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0); document.getElementById('cartBadge').textContent = totalItems; } function addToCart(productId) { const existingItem = cart.find(item => item.productId === productId); if (existingItem) { if (existingItem.quantity < 5) { existingItem.quantity++; } else { alert('Максимальное количество товара в корзине - 5 шт.'); return; } } else { const product = allProducts.find(p => p.id === productId); if (product) { cart.push({ productId: product.id, productTitle: product.title, price: product.price, quantity: 1, maxQuantity: Math.min(product.amount, 5) }); } } saveCartToStorage(); renderProducts(); showMessage('✅ Товар добавлен в корзину', 'success'); } function getCartQuantity(productId) { const item = cart.find(item => item.productId === productId); return item ? item.quantity : 0; } async function loadProducts() { const container = document.getElementById('productsContainer'); container.innerHTML = '<div class="loading">Загрузка товаров...</div>'; try { const response = await fetch(`${API_BASE}/products`); allProducts = await response.json(); renderProducts(); } catch (error) { container.innerHTML = `<div class="error">Ошибка загрузки товаров: ${error.message}</div>`; } } function renderProducts() { const container = document.getElementById('productsContainer'); container.innerHTML = ` <h3>📦 Доступные товары</h3> <div class="products-grid"> ${allProducts.map(product => { const cartQuantity = getCartQuantity(product.id); const canAddMore = cartQuantity < 5 && product.amount > cartQuantity; return ` <div class="product-card"> ${cartQuantity > 0 ? ` <div class="in-cart-badge">В корзине: ${cartQuantity}</div> ` : ''} <div class="product-title">${product.title}</div> <div class="product-price">${formatPrice(product.price)}</div> <div class="product-amount">На складе: ${product.amount} шт.</div> <button class="add-to-cart-btn" onclick="addToCart(${product.id})" ${canAddMore ? '' : 'disabled'} > ${canAddMore ? '➕ Добавить в корзину' : '❌ Недоступно'} </button> </div> `; }).join('')} </div> `; } function showMessage(message, type) { const messageDiv = document.createElement('div'); messageDiv.className = type === 'success' ? 'success' : 'error'; messageDiv.textContent = message; document.body.insertBefore(messageDiv, document.querySelector('.header').nextSibling); setTimeout(() => { messageDiv.remove(); }, 3000); } window.addEventListener('load', () => { updateCartBadge(); loadProducts(); }); </script> </body> </html>