File manager - Edit - /home/webapp69.cm.in.th/u69319090033/Shop/myshop ใหม่/api/seller_dashboard.php
Back
<?php header('Content-Type: application/json; charset=utf-8'); require_once '../db.php'; // Middleware / ตรวจสอบสิทธิ์การเข้าถึง Seller Dashboard // ต้องล็อกอิน และต้องเป็น role='admin' หรือ (role='seller' พร้อม seller_status='approved') if (!isset($_SESSION['user_id'])) { http_response_code(401); echo json_encode(['success' => false, 'message' => 'กรุณาเข้าสู่ระบบก่อน']); exit; } $userRole = $_SESSION['role'] ?? 'user'; $sellerStatus = $_SESSION['seller_status'] ?? 'none'; if ($userRole !== 'admin' && ($userRole !== 'seller' || $sellerStatus !== 'approved')) { http_response_code(403); echo json_encode([ 'success' => false, 'message' => 'คุณยังไม่ได้รับอนุมัติให้เป็นผู้ขาย ไม่สามารถจัดการหรือเพิ่มสินค้าได้ (403 Forbidden)' ]); exit; } $method = $_SERVER['REQUEST_METHOD']; $sellerId = (int)$_SESSION['user_id']; $isAdmin = ($userRole === 'admin'); // ---- GET: seller dashboard data (products + orders) ---- if ($method === 'GET') { // Admin เห็นสินค้าทั้งหมด, Seller เห็นเฉพาะของตัวเอง if ($isAdmin) { $stmt = $conn->prepare("SELECT product_id, seller_id, product_name, price, original_price, stock, approval_status, is_active, sold_count, created_at FROM products ORDER BY created_at DESC"); $stmt->execute(); } else { $stmt = $conn->prepare("SELECT product_id, seller_id, product_name, price, original_price, stock, approval_status, is_active, sold_count, created_at FROM products WHERE seller_id = ? ORDER BY created_at DESC"); $stmt->bind_param('i', $sellerId); $stmt->execute(); } $products = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $stmt->close(); // รายการคำสั่งซื้อที่มีสินค้าของผู้ขาย (Admin เห็นทุกออเดอร์) if ($isAdmin) { $stmt = $conn->prepare(" SELECT DISTINCT o.order_id, o.status, o.total_amount, o.created_at, u.full_name AS customer_name, u.email AS customer_email FROM orders o JOIN users u ON u.user_id = o.user_id ORDER BY o.created_at DESC LIMIT 50 "); $stmt->execute(); } else { $stmt = $conn->prepare(" SELECT DISTINCT o.order_id, o.status, o.total_amount, o.created_at, u.full_name AS customer_name, u.email AS customer_email FROM orders o JOIN order_items oi ON oi.order_id = o.order_id JOIN products p ON p.product_id = oi.product_id JOIN users u ON u.user_id = o.user_id WHERE p.seller_id = ? ORDER BY o.created_at DESC LIMIT 50 "); $stmt->bind_param('i', $sellerId); $stmt->execute(); } $orders = $stmt->get_result()->fetch_all(MYSQLI_ASSOC); $stmt->close(); echo json_encode(['success' => true, 'products' => $products, 'orders' => $orders]); exit; } // ---- POST: เพิ่มสินค้าใหม่ของผู้ขาย ---- if ($method === 'POST') { $input = json_decode(file_get_contents('php://input'), true); $name = trim($input['product_name'] ?? ''); $desc = trim($input['description'] ?? ''); $price = (float)($input['price'] ?? 0); $originalPrice = !empty($input['original_price']) ? (float)$input['original_price'] : null; $category = trim($input['category_id'] ?? ''); $stock = (int)($input['stock'] ?? 0); $imageUrl = trim($input['image_url'] ?? ''); if (empty($name) || $price <= 0 || empty($category)) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'กรุณากรอกข้อมูลสินค้าให้ครบถ้วน']); exit; } // สินค้าจาก Seller จะถูกส่งเข้าสถานะ pending เพื่อรอ Admin อนุมัติ // ถ้า Admin เป็นคนเพิ่มเอง ให้ approved ทันที $approvalStatus = $isAdmin ? 'approved' : 'pending'; $isActive = $isAdmin ? 1 : 0; $stmt = $conn->prepare("INSERT INTO products (seller_id, category_id, product_name, description, price, original_price, stock, image_url, approval_status, is_active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); $stmt->bind_param('isssddissi', $sellerId, $category, $name, $desc, $price, $originalPrice, $stock, $imageUrl, $approvalStatus, $isActive); if ($stmt->execute()) { $productId = $conn->insert_id; $stmt->close(); $msg = $isAdmin ? 'เพิ่มสินค้าเรียบร้อย' : 'เพิ่มสินค้าเรียบร้อย รอ Admin อนุมัติก่อนแสดงในร้านค้า'; echo json_encode(['success' => true, 'message' => $msg, 'product_id' => $productId]); } else { $stmt->close(); http_response_code(500); echo json_encode(['success' => false, 'message' => 'เกิดข้อผิดพลาดในการบันทึกสินค้า']); } exit; } // ---- PUT: แก้ไขสินค้า ---- // Admin แก้ไขได้ทุกสินค้า, Seller แก้ไขได้เฉพาะสินค้าของตัวเอง if ($method === 'PUT') { $input = json_decode(file_get_contents('php://input'), true); $productId = (int)($input['product_id'] ?? 0); if ($productId <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ไม่พบรหัสสินค้า']); exit; } // ตรวจสอบว่าสินค้ามีอยู่จริง และเช็คสิทธิ์ความเป็นเจ้าของ $checkStmt = $conn->prepare("SELECT seller_id FROM products WHERE product_id = ?"); $checkStmt->bind_param('i', $productId); $checkStmt->execute(); $productRow = $checkStmt->get_result()->fetch_assoc(); $checkStmt->close(); if (!$productRow) { http_response_code(404); echo json_encode(['success' => false, 'message' => 'ไม่พบสินค้านี้']); exit; } if (!$isAdmin && (int)$productRow['seller_id'] !== $sellerId) { http_response_code(403); echo json_encode(['success' => false, 'message' => 'คุณไม่มีสิทธิ์แก้ไขสินค้านี้']); exit; } // รับค่าที่จะอัปเดต $name = trim($input['product_name'] ?? ''); $desc = trim($input['description'] ?? ''); $price = (float)($input['price'] ?? 0); $originalPrice = !empty($input['original_price']) ? (float)$input['original_price'] : null; $category = trim($input['category_id'] ?? ''); $stock = (int)($input['stock'] ?? 0); $imageUrl = trim($input['image_url'] ?? ''); if (empty($name) || $price <= 0 || empty($category)) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'กรุณากรอกข้อมูลสินค้าให้ครบถ้วน']); exit; } // Seller แก้ไขสินค้าแล้วให้กลับไปเป็น pending อีกครั้งเพื่อรอ Admin อนุมัติใหม่ // Admin แก้ไขแล้วคงสถานะ approved ไว้เหมือนเดิม if ($isAdmin) { $stmt = $conn->prepare("UPDATE products SET category_id = ?, product_name = ?, description = ?, price = ?, original_price = ?, stock = ?, image_url = ? WHERE product_id = ?"); $stmt->bind_param('sssddisi', $category, $name, $desc, $price, $originalPrice, $stock, $imageUrl, $productId); } else { $stmt = $conn->prepare("UPDATE products SET category_id = ?, product_name = ?, description = ?, price = ?, original_price = ?, stock = ?, image_url = ?, approval_status = 'pending', is_active = 0 WHERE product_id = ? AND seller_id = ?"); $stmt->bind_param('sssddisii', $category, $name, $desc, $price, $originalPrice, $stock, $imageUrl, $productId, $sellerId); } if ($stmt->execute()) { $stmt->close(); $msg = $isAdmin ? 'แก้ไขสินค้าเรียบร้อย' : 'แก้ไขสินค้าเรียบร้อย รอ Admin อนุมัติอีกครั้ง'; echo json_encode(['success' => true, 'message' => $msg]); } else { $stmt->close(); http_response_code(500); echo json_encode(['success' => false, 'message' => 'เกิดข้อผิดพลาดในการแก้ไขสินค้า']); } exit; } // ---- DELETE: ลบสินค้า ---- // Admin ลบได้ทุกสินค้า, Seller ลบได้เฉพาะสินค้าของตัวเอง if ($method === 'DELETE') { $input = json_decode(file_get_contents('php://input'), true); $productId = (int)($input['product_id'] ?? 0); if ($productId <= 0) { http_response_code(400); echo json_encode(['success' => false, 'message' => 'ไม่พบรหัสสินค้า']); exit; } $checkStmt = $conn->prepare("SELECT seller_id FROM products WHERE product_id = ?"); $checkStmt->bind_param('i', $productId); $checkStmt->execute(); $productRow = $checkStmt->get_result()->fetch_assoc(); $checkStmt->close(); if (!$productRow) { http_response_code(404); echo json_encode(['success' => false, 'message' => 'ไม่พบสินค้านี้']); exit; } if (!$isAdmin && (int)$productRow['seller_id'] !== $sellerId) { http_response_code(403); echo json_encode(['success' => false, 'message' => 'คุณไม่มีสิทธิ์ลบสินค้านี้']); exit; } if ($isAdmin) { $stmt = $conn->prepare("DELETE FROM products WHERE product_id = ?"); $stmt->bind_param('i', $productId); } else { $stmt = $conn->prepare("DELETE FROM products WHERE product_id = ? AND seller_id = ?"); $stmt->bind_param('ii', $productId, $sellerId); } if ($stmt->execute()) { $stmt->close(); echo json_encode(['success' => true, 'message' => 'ลบสินค้าเรียบร้อย']); } else { $stmt->close(); http_response_code(500); echo json_encode(['success' => false, 'message' => 'เกิดข้อผิดพลาดในการลบสินค้า']); } exit; } http_response_code(405); echo json_encode(['success' => false, 'message' => 'Method Not Allowed']); ?>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings