@
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] 占位符替换成  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:
+75
-10
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user