15m44f 工单图片"提交才上传"修彻底: 字段名兼容 + 占位符替换 + 诊断日志

用户视频实测: 真点了提交工单, 但 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。
This commit is contained in:
ypsdf1
2026-11-24 20:31:05 +08:00
parent e73b1ea86e
commit 889093a95a
2 changed files with 89 additions and 13 deletions
+75 -10
View File
@@ -239,11 +239,29 @@ function normalizeImageExt($ext) {
* 单张校验逻辑(错误码/文件名清洗/大小/真实格式/后缀归一)与 ticketUploadImage 完全一致。 * 单张校验逻辑(错误码/文件名清洗/大小/真实格式/后缀归一)与 ticketUploadImage 完全一致。
*/ */
function uploadTicketFiles() { 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'])) { if (empty($files) || !is_array($files) || empty($files['name'])) {
// 未以多文件上送(或无图)→ 走旧 JSON images 校验路径,保持向后兼容 // 未以多文件上送(或无图)→ 走旧 JSON images 校验路径,保持向后兼容
return normalizeTicketImages(); $GLOBALS['__ticket_dbg_uploaded'] = 0;
$GLOBALS['__ticket_dbg_urls'] = normalizeTicketImages();
return $GLOBALS['__ticket_dbg_urls'];
} }
$names = $files['name']; $names = $files['name'];
$tmps = $files['tmp_name']; $tmps = $files['tmp_name'];
$sizes = $files['size']; $sizes = $files['size'];
@@ -261,7 +279,6 @@ function uploadTicketFiles() {
UPLOAD_ERR_CANT_WRITE => '服务器写入临时文件失败', UPLOAD_ERR_CANT_WRITE => '服务器写入临时文件失败',
]; ];
$code = (int)$errs[$i]; $code = (int)$errs[$i];
// 未选择文件(空槽)直接跳过,不当错误
if ($code === UPLOAD_ERR_NO_FILE) continue; if ($code === UPLOAD_ERR_NO_FILE) continue;
error(isset($msgs[$code]) ? $msgs[$code] : ('图片上传错误码 ' . $code)); 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'] : '未知错误')); if (empty($r['ok'])) error('图片上传失败: ' . (isset($r['msg']) && $r['msg'] !== '' ? $r['msg'] : '未知错误'));
$out[] = $r['url']; $out[] = $r['url'];
} }
$GLOBALS['__ticket_dbg_uploaded'] = count($out);
$GLOBALS['__ticket_dbg_urls'] = $out;
return $out; return $out;
} }
@@ -351,24 +370,24 @@ function ticketUploadImage() {
function ticketCreate() { function ticketCreate() {
$info = requireWebToken(); $info = requireWebToken();
$player = $info['player']; $player = $info['player'];
$type = trim(getParam('type', '')); $type = trim(getParam('type', ''));
$title = trim(getParam('title', '')); $title = trim(getParam('title', ''));
$description = trim(getParam('description', '')); $description = trim(getParam('description', ''));
if (empty($type)) error('请选择工单类型'); if (empty($type)) error('请选择工单类型');
if (empty($title)) error('请输入工单标题'); if (empty($title)) error('请输入工单标题');
if (mb_strlen($title) > 100) error('标题不能超过100字'); if (mb_strlen($title) > 100) error('标题不能超过100字');
if (mb_strlen($description) > 5000) error('描述不能超过5000字'); if (mb_strlen($description) > 5000) error('描述不能超过5000字');
$validTypes = ['bug', 'help', 'report', 'apply', 'other']; $validTypes = ['bug', 'help', 'report', 'apply', 'other'];
if (!in_array($type, $validTypes)) error('无效的工单类型'); if (!in_array($type, $validTypes)) error('无效的工单类型');
$images = uploadTicketFiles(); // 附件图(≤3,来自 FormData 多文件或旧 JSON) $images = uploadTicketFiles(); // 附件图(≤3,来自 FormData 多文件或旧 JSON)
$db = getDB(); $db = getDB();
$now = time(); $now = time();
$db->exec('BEGIN IMMEDIATE'); $db->exec('BEGIN IMMEDIATE');
try { 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)"); $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'); $db->exec('ROLLBACK');
error('创建失败: ' . $e->getMessage()); 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() { function ticketMyList() {
+14 -3
View File
@@ -4054,12 +4054,12 @@ if ($currentVersion !== $BUILD_VERSION) {
let res; let res;
if (files.length > 0) { if (files.length > 0) {
// FormData: 字段 + 多文件(images[] 同名字段, 服务端 $_FILES['image'] 数组) // FormData: 字段 + 多文件(images[] 同名字段, 服务端 uploadTicketFiles 兼容解析)
const fd = new FormData(); const fd = new FormData();
fd.append('type', type); fd.append('type', type);
fd.append('title', title); fd.append('title', title);
fd.append('description', desc); 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 }); res = await fetch(url, { method: 'POST', body: fd });
} else { } else {
res = await fetch(url, { res = await fetch(url, {
@@ -4070,10 +4070,21 @@ if ($currentVersion !== $BUILD_VERSION) {
} }
const data = await res.json(); const data = await res.json();
if (data.success) { 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 + 暂存(防内存泄漏) // 清 objectURL + 暂存(防内存泄漏)
(ticketState.files || []).forEach(f => { if (f._preview) URL.revokeObjectURL(f._preview); }); (ticketState.files || []).forEach(f => { if (f._preview) URL.revokeObjectURL(f._preview); });
ticketState.files = []; ticketState.files = [];
ticketState.images = []; ticketState.images = imgs; // 详情视图渲染时 applyTicketImgs 用
ticketState.view = 'list'; ticketState.view = 'list';
renderTicket(document.getElementById('content')); renderTicket(document.getElementById('content'));
} else { } else {