@
15m44j img.json 新格式 + 懒重载 + 修 API 挂起/删图不让提交
用户定调: img.json 格式调整(嵌套 api.baseurl/key + 图像/附件支持.最大图片数量/.最大大小/.后缀格式),
代码读新格式; img.json 懒重载(每次新建工单时增量读); 先取消假定未更改推仓库; 删图后能正常提交。
根因 3 个:
1. API 挂起: 生产 img.json 还是旧格式(baseurl/key 顶层), loadImgJson 按旧路径读 → 但
easyUploadFile 用新格式路径 api.baseurl 拿不到 → curl 挂起 60s。
2. 删图不让提交: removeTicketImg 用 files.splice 删了数组但 ticketState.files 引用没同步
(旧代码 splice 后没重新赋值), 提交时 ticketState.files 还残留已删元素。
3. 硬编码 3/2048K/png|jpg: 前端 uploadTicketImage 写死, 后端 uploadTicketFiles 也写死,
与 img.json 配置不同步。
修 5 处:
1. img.json 本地改新格式(api.baseurl/api.key + 图像/附件支持嵌套), 取消假定未更改, 入库。
2. lsky.php loadImgJson(): 改读新格式 api.baseurl/key + 图像/附件支持.附件支持/.最大图片数量/
.最大大小/.后缀格式, 兼容旧格式(顶层 baseurl/key + 图像/附件支持布尔); 每次调用重新读文件
(懒重载, 不缓存); 返回统一结构含 max_images/max_size_mb/allowed_exts/attach_enabled。
3. api/ticket.php: 新增 ticketMaxImages() 读 loadImgJson().max_images; uploadTicketFiles 开头
调 loadImgJson 懒重载, 按 attach_enabled 决定是否接文件, 按 max_images/max_size_mb/
allowed_exts 校验; 真实格式(getimagesize)必须在 img.json 允许后缀里。
4. 新增 api/img_cfg.php: 前端 fetch 端点, 返回 {max_images, max_size_mb, allowed_exts,
attach_enabled} (无 token 校验, 配置非敏感), 每次请求重新读 img.json。
5. player.php: 新增 loadTicketImgCfg() fetch img_cfg.php, uploadTicketImage 前懒重载;
校验改 ticketImgCfg.max_images/max_size_mb/allowed_exts(不再硬编码 3/2048K);
removeTicketImg 修 splice 后重新赋值 ticketState.files(同步清数组引用), 删占位符,
提示"已移除 图片N(剩 M 张可提交)"。
部署清单: img.json(新格式) + lsky.php + api/ticket.php + player.php + api/img_cfg.php(新)。
生产 FTP 推 img.json 新格式(baseurl/key 改 api. 嵌套) + opcache_invalidate.php。
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* 15m44j: img.json 配置懒重载端点 — 前端进工单表单前 fetch 一次, 拿到:
|
||||
* - max_images: 单工单最多几张
|
||||
* - max_size_mb: 单张大小上限 (MB)
|
||||
* - allowed_exts: 允许的后缀数组
|
||||
* - attach_enabled: 附件总开关 (false 时前端禁用"选择图片"按钮)
|
||||
*
|
||||
* 无 token 校验 (配置非敏感, 仅限制前端 UX 文案/上限); 真实上传仍走 ticketCreate 鉴权。
|
||||
* 每次请求重新读 img.json (懒重载, 不缓存), 改配置立即生效。
|
||||
*/
|
||||
while (ob_get_level() > 0) { @ob_end_clean(); }
|
||||
ob_start();
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: 0');
|
||||
|
||||
require_once __DIR__ . '/../core.php';
|
||||
require_once __DIR__ . '/../lsky.php';
|
||||
|
||||
$cfg = loadImgJson();
|
||||
success([
|
||||
'max_images' => max(1, (int)($cfg['max_images'] ?? 3)),
|
||||
'max_size_mb' => max(1, (int)($cfg['max_size_mb'] ?? 2)),
|
||||
'allowed_exts' => array_map('strtolower', (array)($cfg['allowed_exts'] ?? ['jpg','jpeg','png'])),
|
||||
'attach_enabled' => !empty($cfg['attach_enabled']),
|
||||
], 'ok');
|
||||
+33
-18
@@ -152,14 +152,15 @@ function getServiceProviderRole($playerName) {
|
||||
}
|
||||
|
||||
// ★ 单张工单附件图上限(15m44c 兜底: 用 defined() 防任何加载顺序坑致 Undefined constant)
|
||||
// ★ 单张工单附件图上限(15m44g 改 define() 常量, 15m44h 再加固: 函数内引用用 ?? 字面量兜底)
|
||||
// 历史坑: 15m44g 用 $TICKET_MAX_IMAGES 全局变量, 但 normalizeTicketImages/uploadTicketFiles
|
||||
// 是函数作用域, 读全局变量要 global 声明; 函数内未声明时 $TICKET_MAX_IMAGES=null,
|
||||
// '附件图最多 ' . null . ' 张' = "附件图最多 张"(数字被吞)。
|
||||
// 15m44h: 改 define() 常量(常量跨作用域可见), 所有函数内引用点用 "TICKET_MAX_IMAGES ?? 3"
|
||||
// 双保险: 常量存在取常量, 否则字面量 3, 绝不 null。
|
||||
if (!defined('TICKET_MAX_IMAGES')) {
|
||||
define('TICKET_MAX_IMAGES', 3);
|
||||
// ★ 单张工单附件图上限(15m44j: 改读 img.json 的 图像/附件支持.最大图片数量, 不再硬编码)
|
||||
// 兼容兜底: img.json 缺失/解析失败时默认 3。loadImgJson 每次调用重新读文件(懒重载)。
|
||||
function ticketMaxImages() {
|
||||
try {
|
||||
$cfg = loadImgJson();
|
||||
return max(1, (int)($cfg['max_images'] ?? 3));
|
||||
} catch (\Throwable $e) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,7 +180,7 @@ function normalizeTicketImages() {
|
||||
if ($u === '') continue;
|
||||
if (strpos($u, $prefix) !== 0) error('不支持的图片地址(仅接受自家图床)');
|
||||
$out[] = $u;
|
||||
if (count($out) >= (defined('TICKET_MAX_IMAGES') ? TICKET_MAX_IMAGES : 3)) break;
|
||||
if (count($out) >= ticketMaxImages()) break;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
@@ -247,24 +248,35 @@ function normalizeImageExt($ext) {
|
||||
* 单张校验逻辑(错误码/文件名清洗/大小/真实格式/后缀归一)与 ticketUploadImage 完全一致。
|
||||
*/
|
||||
function uploadTicketFiles() {
|
||||
// 15m44j: 每次 ticketCreate 前懒重载 img.json(不缓存),读最大图片数量/最大大小/后缀格式
|
||||
$cfg = loadImgJson();
|
||||
if (empty($cfg['attach_enabled'])) {
|
||||
// 附件支持关 → 不接文件,走旧 JSON 路径(images 字段校验自家图床 URL)
|
||||
$GLOBALS['__ticket_dbg_uploaded'] = 0;
|
||||
$GLOBALS['__ticket_dbg_urls'] = normalizeTicketImages();
|
||||
return $GLOBALS['__ticket_dbg_urls'];
|
||||
}
|
||||
$maxImages = max(1, (int)($cfg['max_images'] ?? 3));
|
||||
$maxBytes = max(1, (int)($cfg['max_size_mb'] ?? 2)) * 1024 * 1024;
|
||||
$allowedExts = array_map('strtolower', (array)($cfg['allowed_exts'] ?? ['jpg','jpeg','png']));
|
||||
// 后缀归一:jpeg/jfif/jpe 归到 jpg;其他原样
|
||||
$extMap = ['jpg'=>'jpg','jpeg'=>'jpg','jpe'=>'jpg','jfif'=>'jpg','png'=>'png','gif'=>'gif','webp'=>'webp','bmp'=>'bmp'];
|
||||
|
||||
// 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 校验路径,保持向后兼容
|
||||
$GLOBALS['__ticket_dbg_uploaded'] = 0;
|
||||
$GLOBALS['__ticket_dbg_urls'] = normalizeTicketImages();
|
||||
return $GLOBALS['__ticket_dbg_urls'];
|
||||
@@ -290,22 +302,25 @@ function uploadTicketFiles() {
|
||||
if ($code === UPLOAD_ERR_NO_FILE) continue;
|
||||
error(isset($msgs[$code]) ? $msgs[$code] : ('图片上传错误码 ' . $code));
|
||||
}
|
||||
if (count($out) >= (defined('TICKET_MAX_IMAGES') ? TICKET_MAX_IMAGES : 3)) {
|
||||
error('附件图最多 ' . (defined('TICKET_MAX_IMAGES') ? TICKET_MAX_IMAGES : 3) . ' 张');
|
||||
if (count($out) >= $maxImages) {
|
||||
error('附件图最多 ' . $maxImages . ' 张(img.json 配置)');
|
||||
}
|
||||
$orig = sanitizeImageFileName(basename((string)$names[$i]));
|
||||
if ((int)$sizes[$i] > 2048 * 1024) error('单张图不能超过 2048K');
|
||||
if ((int)$sizes[$i] > $maxBytes) error('单张图不能超过 ' . round($maxBytes / 1048576, 1) . 'MB');
|
||||
if (!is_uploaded_file($tmps[$i])) error('非法上传');
|
||||
$info = @getimagesize($tmps[$i]);
|
||||
if ($info === false) error('无法识别的图片内容(请确认文件确实是 png 或 jpg)');
|
||||
if ($info === false) error('无法识别的图片内容(请确认文件是 ' . implode('/', $allowedExts) . ')');
|
||||
$itype = (int)$info[2];
|
||||
if ($itype === IMAGETYPE_PNG) $realExt = 'png';
|
||||
elseif ($itype === IMAGETYPE_JPEG) $realExt = 'jpg';
|
||||
elseif ($itype === IMAGETYPE_GIF) $realExt = 'gif';
|
||||
elseif ($itype === IMAGETYPE_WEBP) $realExt = 'webp';
|
||||
elseif ($itype === IMAGETYPE_BMP) $realExt = 'bmp';
|
||||
else error('仅支持 png / jpg 格式');
|
||||
$ext = normalizeImageExt(pathinfo($orig, PATHINFO_EXTENSION));
|
||||
else error('仅支持 ' . implode(' / ', $allowedExts) . ' 格式');
|
||||
// 后缀白名单校验:真实格式必须在 img.json 允许的后缀里
|
||||
if (!in_array($realExt, $allowedExts, true)) {
|
||||
error('仅支持后缀 ' . implode(' / ', $allowedExts) . '(img.json 配置)');
|
||||
}
|
||||
$base = pathinfo($orig, PATHINFO_FILENAME);
|
||||
if ($base === '') $base = 'ticket_image';
|
||||
$orig = mb_substr($base, 0, 60) . '.' . $realExt;
|
||||
|
||||
Reference in New Issue
Block a user