From 889093a95a452c8d1941f55289c833cca1b5c9f2 Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Tue, 24 Nov 2026 20:31:05 +0800 Subject: [PATCH] =?UTF-8?q?@=2015m44f=20=E5=B7=A5=E5=8D=95=E5=9B=BE?= =?UTF-8?q?=E7=89=87"=E6=8F=90=E4=BA=A4=E6=89=8D=E4=B8=8A=E4=BC=A0"?= =?UTF-8?q?=E4=BF=AE=E5=BD=BB=E5=BA=95:=20=E5=AD=97=E6=AE=B5=E5=90=8D?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=20+=20=E5=8D=A0=E4=BD=8D=E7=AC=A6=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=20+=20=E8=AF=8A=E6=96=AD=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户视频实测: 真点了提交工单, 但 EasyImages 后台"今天还没有上传的图片" → 图片没真到 API。 根因 3 个: 1. PHP 8 解析 FormData "images[]" 字段名时, 可能把 $_FILES 顶层键解析成 "images" 而非 "images[]", 后端 uploadTicketFiles 只接 $_FILES[images[]] → 命中 fallback JSON 路径 → 旧 JSON images 空数组 → 图床零请求。 2. 提交成功后, 工单描述框里的 [图片1] 占位符没被替换成 markdown 图片 (用户期望"图片没替换占位符")。 3. 无诊断日志, 无法定位"图片没真去 API"的根因。 修: 1. uploadTicketFiles 兼容 3 种字段名解析: images[] (标准) / images (PHP 8 解析) / image (单文件兜底), 单文件场景补成多文件数组结构。 2. submitTicket 成功后: 从 data.data.images 拿到图床 URL, 调 applyTicketImgs(desc, imgs) 把描述里的 [图片N] 占位符替换成 ![](url) markdown, 写回 ticketDesc 输入框 (用户可见)。 ticketState.images = imgs (详情视图渲染时 applyTicketImgs 用)。 3. ticketCreate success 加 "images" 字段 (返回图床 URL 列表给前端)。 4. 新增 writeTicketCreateDebug: success 前把关键诊断信息写到 /plugin/ticket_debug.log (ts/player/content_type/files_keys/files_detail/uploaded_count/img_urls/type/title), 1 天自动清理。用户/开发下次"图片没真去 API"时, curl /plugin/ticket_debug.log 一眼定位。 部署: api/ticket.php + player.php 两件 FTP 上传 + 生产 curl opcache_invalidate.php。 --- api/ticket.php | 85 ++++++++++++++++++++++++++++++++++++++++++++------ player.php | 17 ++++++++-- 2 files changed, 89 insertions(+), 13 deletions(-) diff --git a/api/ticket.php b/api/ticket.php index 3b8fcbf..371051a 100644 --- a/api/ticket.php +++ b/api/ticket.php @@ -239,11 +239,29 @@ function normalizeImageExt($ext) { * 单张校验逻辑(错误码/文件名清洗/大小/真实格式/后缀归一)与 ticketUploadImage 完全一致。 */ function uploadTicketFiles() { - $files = isset($_FILES['images[]']) ? $_FILES['images[]'] : null; + // 15m44f:兼容三种字段名解析:'images[]'(标准)/ 'images'(PHP 8 可能解析成)/ 'image'(单文件兜底) + $files = null; + if (isset($_FILES['images[]']) && is_array($_FILES['images[]']) && !empty($_FILES['images[]']['name'])) { + $files = $_FILES['images[]']; + } elseif (isset($_FILES['images']) && is_array($_FILES['images'])) { + $files = $_FILES['images']; + // 单文件场景(PHP 解析成 1 维数组)→ 补成多文件数组结构 + if (!isset($files['name']) || !is_array($files['name'])) { + $files = ['name' => [$files['name'] ?? ''], 'tmp_name' => [$files['tmp_name'] ?? ''], 'size' => [$files['size'] ?? 0], 'error' => [$files['error'] ?? UPLOAD_ERR_NO_FILE], 'full_name' => isset($files['full_name']) ? [$files['full_name']] : null]; + } + } elseif (isset($_FILES['image']) && is_array($_FILES['image'])) { + // 单文件兜底(旧 ticketUploadImage 的字段名) + $f = $_FILES['image']; + $files = ['name' => [$f['name'] ?? ''], 'tmp_name' => [$f['tmp_name'] ?? ''], 'size' => [$f['size'] ?? 0], 'error' => [$f['error'] ?? UPLOAD_ERR_NO_FILE]]; + } + if (empty($files) || !is_array($files) || empty($files['name'])) { // 未以多文件上送(或无图)→ 走旧 JSON images 校验路径,保持向后兼容 - return normalizeTicketImages(); + $GLOBALS['__ticket_dbg_uploaded'] = 0; + $GLOBALS['__ticket_dbg_urls'] = normalizeTicketImages(); + return $GLOBALS['__ticket_dbg_urls']; } + $names = $files['name']; $tmps = $files['tmp_name']; $sizes = $files['size']; @@ -261,7 +279,6 @@ function uploadTicketFiles() { UPLOAD_ERR_CANT_WRITE => '服务器写入临时文件失败', ]; $code = (int)$errs[$i]; - // 未选择文件(空槽)直接跳过,不当错误 if ($code === UPLOAD_ERR_NO_FILE) continue; error(isset($msgs[$code]) ? $msgs[$code] : ('图片上传错误码 ' . $code)); } @@ -289,6 +306,8 @@ function uploadTicketFiles() { if (empty($r['ok'])) error('图片上传失败: ' . (isset($r['msg']) && $r['msg'] !== '' ? $r['msg'] : '未知错误')); $out[] = $r['url']; } + $GLOBALS['__ticket_dbg_uploaded'] = count($out); + $GLOBALS['__ticket_dbg_urls'] = $out; return $out; } @@ -351,24 +370,24 @@ function ticketUploadImage() { function ticketCreate() { $info = requireWebToken(); $player = $info['player']; - + $type = trim(getParam('type', '')); $title = trim(getParam('title', '')); $description = trim(getParam('description', '')); - + if (empty($type)) error('请选择工单类型'); if (empty($title)) error('请输入工单标题'); if (mb_strlen($title) > 100) error('标题不能超过100字'); if (mb_strlen($description) > 5000) error('描述不能超过5000字'); - + $validTypes = ['bug', 'help', 'report', 'apply', 'other']; if (!in_array($type, $validTypes)) error('无效的工单类型'); - + $images = uploadTicketFiles(); // 附件图(≤3,来自 FormData 多文件或旧 JSON) $db = getDB(); $now = time(); - + $db->exec('BEGIN IMMEDIATE'); try { $stmt = $db->prepare("INSERT INTO web_tickets (type, status, requester, title, description, images, created_at, updated_at) VALUES (:type, 'submitted', :player, :title, :desc, :images, :now, :now)"); @@ -385,8 +404,54 @@ function ticketCreate() { $db->exec('ROLLBACK'); error('创建失败: ' . $e->getMessage()); } - - success(['id' => $ticketId], '工单提交成功'); + + // 15m44f 诊断日志(success 前写) + $GLOBALS['__ticket_dbg_player'] = $player; + $GLOBALS['__ticket_dbg_type'] = $type; + $GLOBALS['__ticket_dbg_title'] = $title; + $GLOBALS['__ticket_dbg_urls'] = $images; + writeTicketCreateDebug(); + + success(['id' => $ticketId, 'images' => $images], '工单提交成功'); +} + +// 15m44f 诊断日志:ticketCreate 在 success 前把关键诊断信息写到 /plugin/ticket_debug.log +// 排查"前端提交带 images[] 多文件,但图床 EasyImages 后台没收到"的根因: +// - _FILES_keys: 实际收到的 $_FILES 顶层键(区分 'images[]' vs 'images' vs 空) +// - uploaded: 本次实际转存图床成功的图片数 +// - img_urls: 写库的 image URL 列表 +// - content_type: 请求 Content-Type(区分 FormData multipart vs JSON) +// 日志 1 天自动删除,避免磁盘堆积。 +function writeTicketCreateDebug() { + $logDir = dirname(__DIR__); + $logFile = $logDir . '/ticket_debug.log'; + // 清理 1 天前的日志 + if (is_dir($logDir)) { + $cut = time() - 86400; + foreach (glob($logDir . '/ticket_debug_*.log') as $old) { + if (file_exists($old) && filemtime($old) < $cut) @unlink($old); + } + } + $ts = date('Ymd_His'); + $line = json_encode([ + 'ts' => $ts, + 'player' => $GLOBALS['__ticket_dbg_player'] ?? '', + 'content_type' => $_SERVER['CONTENT_TYPE'] ?? ($_SERVER['HTTP_CONTENT_TYPE'] ?? ''), + 'files_keys' => array_keys((array)$_FILES), + 'files_detail' => array_map(function($k) { + $f = $_FILES[$k]; + return [ + 'name' => is_array($f['name'] ?? null) ? array_map('basename', $f['name']) : ($f['name'] ?? ''), + 'error' => $f['error'] ?? null, + 'size' => $f['size'] ?? null, + ]; + }, array_keys((array)$_FILES)), + 'uploaded_count' => $GLOBALS['__ticket_dbg_uploaded'] ?? 0, + 'img_urls' => $GLOBALS['__ticket_dbg_urls'] ?? [], + 'type' => $GLOBALS['__ticket_dbg_type'] ?? '', + 'title' => $GLOBALS['__ticket_dbg_title'] ?? '', + ], JSON_UNESCAPED_UNICODE); + @file_put_contents($logFile, $line . "\n", FILE_APPEND | LOCK_EX); } function ticketMyList() { diff --git a/player.php b/player.php index 3c8f0ff..e4f4971 100644 --- a/player.php +++ b/player.php @@ -4054,12 +4054,12 @@ if ($currentVersion !== $BUILD_VERSION) { let res; if (files.length > 0) { - // FormData: 字段 + 多文件(images[] 同名字段, 服务端 $_FILES['image'] 数组) + // FormData: 字段 + 多文件(images[] 同名字段, 服务端 uploadTicketFiles 兼容解析) const fd = new FormData(); fd.append('type', type); fd.append('title', title); fd.append('description', desc); - files.forEach(f => fd.append('images[]', f, f.name)); + files.forEach((f, i) => fd.append('images[]', f, f.name)); res = await fetch(url, { method: 'POST', body: fd }); } else { res = await fetch(url, { @@ -4070,10 +4070,21 @@ if ($currentVersion !== $BUILD_VERSION) { } const data = await res.json(); if (data.success) { + // 15m44f:提交成功才把描述里的 [图片N] 占位符替换成 markdown 图片 + // (暂存阶段保留 [图片N] 占位符便于用户在描述框定位;提交时替换) + const imgs = data.data && data.data.images ? data.data.images : (ticketState.images || []); + if (imgs.length > 0) { + const descEl = document.getElementById('ticketDesc'); + if (descEl) { + const oldDesc = descEl.value; + const newDesc = applyTicketImgs(oldDesc, imgs); + if (newDesc !== oldDesc) descEl.value = newDesc; + } + } // 清 objectURL + 暂存(防内存泄漏) (ticketState.files || []).forEach(f => { if (f._preview) URL.revokeObjectURL(f._preview); }); ticketState.files = []; - ticketState.images = []; + ticketState.images = imgs; // 详情视图渲染时 applyTicketImgs 用 ticketState.view = 'list'; renderTicket(document.getElementById('content')); } else {