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 {