15m44i 工单图片UX根治: 隐藏原生file input(未选择文件纯误导) + 自定义选择图片按钮 + 移除某张 + 提交前校验0张提示

This commit is contained in:
ypsdf1
2026-11-24 21:09:36 +08:00
parent 8942193565
commit 7e700cadd1
+41 -15
View File
@@ -3940,8 +3940,11 @@ if ($currentVersion !== $BUILD_VERSION) {
<div style="margin-bottom:12px"><label style="color:var(--dim);font-size:13px;display:block;margin-bottom:4px">详细描述</label>
<textarea id="ticketDesc" rows="6" placeholder="详细描述您的问题,支持Markdown格式" style="width:100%;padding:10px;border-radius:6px;border:1px solid var(--border);background:var(--bg);color:var(--text);font-size:14px;resize:vertical;font-family:monospace"></textarea></div>
<div style="margin-bottom:12px"><label style="color:var(--dim);font-size:13px;display:block;margin-bottom:4px">附件图片(最多 3 张)</label>
<input type="file" id="ticketImg" accept=".png,.jpg,.jpeg,image/png,image/jpeg" multiple onchange="uploadTicketImage(this)" style="font-size:13px">
<p style="color:var(--dim);font-size:12px;margin-top:4px">仅支持 png / jpg,单张 ≤ 2048K;选中即上传,并在上方描述光标处插入 [图片N] 占位符</p>
<!-- 15m44i: 隐藏原生 file input (原生"未选择文件"是浏览器 UI 硬编码, 即使 JS 暂存了 File 也改不了, 纯误导),
改成自定义"选择图片"按钮触发隐藏 input, 缩略图列表用 URL.createObjectURL 本地预览 -->
<input type="file" id="ticketImg" accept=".png,.jpg,.jpeg,image/png,image/jpeg" multiple style="display:none" onchange="uploadTicketImage(this)">
<button class="btn" style="font-size:13px;padding:6px 12px" onclick="document.getElementById('ticketImg').click()">📷 选择图片</button>
<p style="color:var(--dim);font-size:12px;margin-top:4px">仅支持 png / jpg,单张 ≤ 2048K,最多 3 张;提交工单时一并上传图床(暂存阶段不上传 API)</p>
<div id="ticketImgList" style="display:flex;gap:8px;flex-wrap:wrap;margin-top:8px"></div>
<p id="ticketImgMsg" style="color:var(--red);font-size:12px;margin-top:4px"></p></div>
<p style="color:var(--dim);font-size:12px;margin-bottom:12px">💡 支持标准Markdown语法:**粗体**、*斜体*、\`代码\`、列表、标题等</p>
@@ -3957,14 +3960,12 @@ if ($currentVersion !== $BUILD_VERSION) {
// ===== 工单附件图:15m44d 改为"选中暂存, 提交工单时一并上传图床"=====
// 旧版"选中即上传图床"的问题: 用户填单一半后悔/问题已解决, 图片却已进 API 图床
// (成为孤儿文件)。改: 前端只暂存 File 对象到 ticketState.files, 不上传;
// 点"提交工单"时才把文件交给 API(ticketCreate 收到 images 文件→上传图床→写库)。
// 前端先走(拿到工单id), 图片转存由后台执行, 取消/后悔则图片从未进 API。
// 15m44i: 隐藏 input 的"选择图片"按钮触发后, 暂存 File + 清 input.value + 重渲染缩略图
async function uploadTicketImage(input) {
const msg = document.getElementById('ticketImgMsg');
if (!input.files || !input.files.length) return;
const files = Array.from(input.files);
input.value = '';
input.value = ''; // 清空原生 input (防重复暂存, 也避免"未选择文件"文字误导)
for (const file of files) {
if ((ticketState.files || []).length >= 3) { setImgMsg(msg, '最多只能选 3 张图片', true); break; }
// 前端只做轻量校验(类型/大小), 真实内容判定留给服务端 getimagesize
@@ -3980,6 +3981,7 @@ if ($currentVersion !== $BUILD_VERSION) {
syncTicketImgInput();
setImgMsg(msg, '已暂存 ' + ticketState.files.length + '/3 张(提交工单时一并上传)', false);
}
renderTicketImgs();
syncTicketImgInput();
}
@@ -4000,26 +4002,45 @@ if ($currentVersion !== $BUILD_VERSION) {
ta.selectionStart = ta.selectionEnd = s + ph.length;
}
// 已选缩略图列表(15m44d 暂存阶段用本地 URL.createObjectURL 预览, 不依赖图床)
// 15m44i: 缩略图列表 + "移除"按钮(用户点 X 删某张暂存图, 同步清 ticketState.files + objectURL)
function renderTicketImgs() {
const box = document.getElementById('ticketImgList');
if (!box) return;
// 优先用本地暂存文件预览(ticketState.files), 兜底旧 images URL
const items = (ticketState.files || []).length ? ticketState.files : (ticketState.images || []);
box.innerHTML = items.map((it, i) => {
// File 对象用 objectURL 预览; 字符串 URL 直接用
const src = (typeof it === 'object' && it !== null && it.type) ? (it._preview || (it._preview = URL.createObjectURL(it))) : it;
const files = ticketState.files || [];
if (files.length === 0) { box.innerHTML = '<span style="color:var(--dim);font-size:12px">未选择图片</span>'; return; }
box.innerHTML = files.map((it, i) => {
const src = (it._preview || (it._preview = URL.createObjectURL(it)));
return `<div style="position:relative;width:76px;height:76px;border:1px solid var(--border);border-radius:6px;overflow:hidden">
<img src="${escHtml(src)}" style="width:100%;height:100%;object-fit:cover">
<span style="position:absolute;left:0;bottom:0;background:rgba(0,0,0,.6);color:#fff;font-size:10px;padding:1px 5px;border-radius:0 4px 0 0">图片${i + 1}</span>
<button onclick="removeTicketImg(${i})" style="position:absolute;right:0;top:0;background:rgba(248,81,73,.85);color:#fff;border:none;width:18px;height:18px;line-height:18px;font-size:12px;cursor:pointer;text-align:center">✕</button>
</div>`;
}).join('');
}
// 3 张用满则禁用选择器(按暂存文件数)
// 15m44i: 移除某张暂存图(同步清 ticketState.files + objectURL + 描述框占位符)
function removeTicketImg(idx) {
const files = ticketState.files || [];
if (idx < 0 || idx >= files.length) return;
const removed = files.splice(idx, 1)[0];
if (removed && removed._preview) URL.revokeObjectURL(removed._preview);
// 删描述框里对应 [图片(idx+1)] 占位符(后续 [图片N] 序号不变, 用户可手动调)
const ta = document.getElementById('ticketDesc');
if (ta) {
const ph = '[图片' + (idx + 1) + ']';
if (ta.value.includes(ph)) ta.value = ta.value.replace(ph, '');
}
renderTicketImgs();
syncTicketImgInput();
setImgMsg(document.getElementById('ticketImgMsg'), '已移除 图片' + (idx + 1), false);
}
// 15m44i: 3 张用满则禁用"选择图片"按钮 (隐藏 input 不再直接禁用, 防按钮点了没反应)
function syncTicketImgInput() {
const btn = document.querySelector('#ticketImgList')?.previousElementSibling;
const fi = document.getElementById('ticketImg');
if (fi) fi.disabled = ((ticketState.files || ticketState.images || []).length >= 3);
if (fi) fi.disabled = ((ticketState.files || []).length >= 3);
if (btn && btn.tagName === 'BUTTON') btn.disabled = ((ticketState.files || []).length >= 3);
}
// 详情渲染:把描述里的 [图片N] 占位符换成 markdown 图片;没被引用的图片补在末尾
@@ -4044,10 +4065,15 @@ if ($currentVersion !== $BUILD_VERSION) {
const errEl = document.getElementById('ticketError');
if (!title) { errEl.textContent = '请输入标题'; return; }
if (!desc) { errEl.textContent = '请输入描述'; return; }
// 15m44i: 提交前校验暂存图, 0 张时提示"未选择图片" (不再"没反应")
const files = ticketState.files || [];
if (files.length === 0) {
errEl.textContent = '未选择图片(可选:点上方"📷 选择图片"暂存 1-3 张, 提交时一并上传图床)';
return;
}
try {
// 15m44d 提交时才传图片: 有暂存文件则用 FormData 多文件(后台上传图床), 否则 JSON
const files = ticketState.files || [];
const url = new URL(API + 'ticket.php', location.href);
url.searchParams.set('action', 'create');
url.searchParams.set('token', TOKEN);