芝麻web文件管理V1.00
编辑当前文件:/home/pulsehostuk9/public_html/teafund.pulsehost.co.uk/api/stock_alert.php
true, 'dedup' => true]); // pretend success exit; } $fund_id = isset($_POST['fund_id']) ? (int)$_POST['fund_id'] : null; $item = isset($_POST['item']) ? strtolower(trim($_POST['item'])) : ''; $note = isset($_POST['note']) ? trim($_POST['note']) : ''; if (!$item) { http_response_code(400); echo json_encode(['ok' => false, 'error' => 'Missing item']); exit; } // Ensure table exists $pdo->exec("CREATE TABLE IF NOT EXISTS stock_alerts ( id INT AUTO_INCREMENT PRIMARY KEY, fund_id INT NULL, item ENUM('cordial','milk','coffee','tea','sugar','other') NOT NULL, note VARCHAR(255) NULL, status ENUM('new','ack','resolved') NOT NULL DEFAULT 'new', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_by_ip VARCHAR(45) NULL, user_agent VARCHAR(255) NULL, INDEX(status), INDEX(created_at), INDEX(item) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"); // Dedup same fund+item last 60 minutes while status is new/ack $stmt = $pdo->prepare("SELECT id FROM stock_alerts WHERE (fund_id <=> ?) AND item=? AND status IN ('new','ack') AND created_at >= (NOW() - INTERVAL 60 MINUTE) ORDER BY id DESC LIMIT 1"); $stmt->execute([$fund_id, $item]); $dup = $stmt->fetchColumn(); if ($dup) { echo json_encode(['ok' => true, 'dedup' => true]); exit; } $ip = $_SERVER['REMOTE_ADDR'] ?? null; $ua = $_SERVER['HTTP_USER_AGENT'] ?? null; $ins = $pdo->prepare("INSERT INTO stock_alerts (fund_id, item, note, status, created_by_ip, user_agent) VALUES (?,?,?,?,?,?)"); $ins->execute([$fund_id, $item, $note ?: null, 'new', $ip, $ua]); $id = (int)$pdo->lastInsertId(); // Lookup fund name for Slack $fundName = null; if ($fund_id) { $fn = $pdo->prepare("SELECT name FROM funds WHERE id=?"); $fn->execute([$fund_id]); $fundName = $fn->fetchColumn() ?: null; } // Send Slack notification (non-blocking best-effort) if (slack_enabled()) { @slack_notify_stock_new(['id'=>$id,'fund_id'=>$fund_id,'item'=>$item,'note'=>$note,'created_by_ip'=>$ip], $fundName); } echo json_encode(['ok' => true, 'id' => $id]); } catch (Throwable $e) { http_response_code(500); echo json_encode(['ok' => false, 'error' => 'server_error']); }