From a72bbfd970485d1ed695b9dd408b2b8ef4cd3702 Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Tue, 7 Jul 2026 13:38:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=20=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E9=A1=B5=E5=8A=A0=E8=BD=BD=E5=8D=A1=E6=AD=BB?= =?UTF-8?q?(=E5=A2=9E=E5=8A=A0=E8=AF=B7=E6=B1=82=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E4=B8=8E=E9=87=8D=E8=AF=95)=20+=20=E8=AE=A2=E5=8D=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E9=87=8A=E6=94=BE=E4=BC=9A=E8=AF=9D=E9=94=81=E5=B9=B6?= =?UTF-8?q?=E5=AF=B9database=20is=20locked=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/cashier.php | 43 +++++++++++++++++++++++++++++++------------ orders.php | 30 ++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/api/cashier.php b/api/cashier.php index c07ecaa..e6d98df 100644 --- a/api/cashier.php +++ b/api/cashier.php @@ -151,29 +151,48 @@ function cashierPlayerCheck() { // ===== 订单列表 ===== function cashierOrderList() { $role = requireCashierOrAdminSession(); + // 收银员只能看自己的订单 + $c = null; + if ($role === 'cashier') { + $c = getCurrentCashier(); + } + // ★ 尽早释放会话锁:查询期间不再占用 session 文件,避免阻塞同会话并发请求 + if (session_status() === PHP_SESSION_ACTIVE) session_write_close(); + $db = getDB(); $limit = (int)getParam('limit', 100); if ($limit <= 0 || $limit > 500) $limit = 100; $sql = "SELECT * FROM cashier_orders"; $params = []; - // 收银员只能看自己的订单 - if ($role === 'cashier') { - $c = getCurrentCashier(); + if ($role === 'cashier' && $c) { $sql .= " WHERE operator_name = :op"; $params[':op'] = $c['username']; } $sql .= " ORDER BY created_at DESC LIMIT " . (int)$limit; - $stmt = $db->prepare($sql); - foreach ($params as $k => $v) { - $stmt->bindValue($k, $v, SQLITE3_TEXT); - } - $result = $stmt->execute(); + + // ★ 重试规避 database is locked(Java 高频写入时 SQLite 偶发锁,最多重试4次) $rows = []; - while ($r = $result->fetchArray(SQLITE3_ASSOC)) { - if (!empty($r['items_detail'])) { - $r['items_detail'] = json_decode($r['items_detail'], true) ?: []; + $attempts = 0; + while ($attempts < 4) { + $attempts++; + $stmt = $db->prepare($sql); + if (!$stmt) break; + foreach ($params as $k => $v) { + $stmt->bindValue($k, $v, SQLITE3_TEXT); } - $rows[] = $r; + $result = @$stmt->execute(); + if ($result === false) { + $err = $db->lastErrorMsg(); + if (stripos($err, 'locked') !== false && $attempts < 4) { usleep(200000); continue; } + exit(json_encode(['success' => false, 'message' => '查询失败: ' . $err], JSON_UNESCAPED_UNICODE)); + } + while ($r = $result->fetchArray(SQLITE3_ASSOC)) { + if (!empty($r['items_detail'])) { + $r['items_detail'] = json_decode($r['items_detail'], true) ?: []; + } + $rows[] = $r; + } + break; } exit(json_encode(['success' => true, 'data' => $rows], JSON_UNESCAPED_UNICODE)); } diff --git a/orders.php b/orders.php index 51b6954..6da9b63 100644 --- a/orders.php +++ b/orders.php @@ -127,12 +127,24 @@ $initialState = json_encode([