支持一楼活动照片多选直传,ZIP改为可选文档附件
This commit is contained in:
Vendored
+1
-1
@@ -322,7 +322,7 @@
|
||||
|
||||
<div id="toast" class="toast hidden" role="status" aria-live="polite"></div>
|
||||
|
||||
<script src="/js/app.js?v=28"></script>
|
||||
<script src="/js/app.js?v=29"></script>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Vendored
+70
-35
@@ -827,7 +827,7 @@
|
||||
let result;
|
||||
try { result = JSON.parse(xhr.responseText); } catch { result = {}; }
|
||||
if (xhr.status >= 200 && xhr.status < 300 && (result.id || result.timetable)) resolve(result);
|
||||
else reject(new Error(xhr.status === 413 ? (contentType === 'application/zip' ? '材料包过大,请提交30MB以内的ZIP' : '课表图片最大10MB') : apiErrorMessage(result.detail, xhr.status)));
|
||||
else reject(new Error(xhr.status === 413 ? (typeof result.detail === 'string' ? result.detail : '上传文件过大,请检查大小限制') : apiErrorMessage(result.detail, xhr.status)));
|
||||
};
|
||||
xhr.onerror = () => reject(new Error('上传失败,请检查网络后重试'));
|
||||
xhr.ontimeout = () => reject(new Error('上传超时,请重试'));
|
||||
@@ -855,36 +855,83 @@
|
||||
}
|
||||
}
|
||||
|
||||
function centerUploadFields(prefix) {
|
||||
return `<label>活动照片<input name="photos" type="file" accept="image/jpeg,image/png,image/webp" multiple /></label>
|
||||
<p class="muted">可直接多选相册照片或拍照,每张不超过8MB。照片数量不限。</p><div id="${prefix}-selected-photos" class="activity-photos"></div>
|
||||
<label>ZIP附件 <span class="hint-inline">(选填)</span><input name="materials" type="file" accept=".zip,application/zip" /></label>
|
||||
<p class="muted">需要附PDF、Word、Excel等文档时再上传ZIP,最大30MB,可不含照片。</p>`;
|
||||
}
|
||||
|
||||
function bindCenterUploads(form, box, label, meter, prefix) {
|
||||
const uploadedPhotos = new Map();
|
||||
let uploadedZip = null;
|
||||
let previewUrls = [];
|
||||
const clearPreview = () => { previewUrls.forEach(url => URL.revokeObjectURL(url)); previewUrls = []; };
|
||||
modalCleanup = clearPreview;
|
||||
form.photos.addEventListener('change', () => {
|
||||
clearPreview();
|
||||
const preview = $('#' + prefix + '-selected-photos'); preview.replaceChildren();
|
||||
for (const file of form.photos.files) {
|
||||
const img = document.createElement('img'), url = URL.createObjectURL(file);
|
||||
previewUrls.push(url); img.src = url; img.alt = file.name; preview.append(img);
|
||||
}
|
||||
box.classList.add('hidden');
|
||||
});
|
||||
form.materials.addEventListener('change', () => { uploadedZip = null; box.classList.add('hidden'); });
|
||||
return async () => {
|
||||
const files = [...form.photos.files], zip = form.materials.files[0];
|
||||
if (files.some(file => file.size > 8 * 1024 * 1024 || !(/\.(jpe?g|png|webp)$/i.test(file.name) || /^image\/(jpeg|png|webp)$/.test(file.type)))) throw new Error('照片支持JPG、PNG、WebP,每张最大8MB');
|
||||
if (zip && (zip.size > 30 * 1024 * 1024 || !zip.name.toLowerCase().endsWith('.zip'))) throw new Error('请提交30MB以内的ZIP附件');
|
||||
if (files.length || zip) box.classList.remove('hidden');
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
if (!form.isConnected) throw new Error('上传已取消');
|
||||
const file = files[i];
|
||||
if (!uploadedPhotos.has(file)) {
|
||||
const result = await uploadFile('/api/center/photos', 'POST', file, (loaded, total) => {
|
||||
const percent = total ? Math.min(100, Math.floor(loaded / total * 100)) : 0;
|
||||
if (total && loaded >= total) { meter.removeAttribute('value'); label.textContent = `正在上传 ${i+1}/${files.length} · 正在处理照片…`; }
|
||||
else { meter.value = percent; label.textContent = `正在上传 ${i+1}/${files.length} · ${percent}%`; }
|
||||
}, file.type || 'application/octet-stream');
|
||||
uploadedPhotos.set(file, result.id);
|
||||
}
|
||||
}
|
||||
if (zip && uploadedZip?.file !== zip) {
|
||||
if (!form.isConnected) throw new Error('上传已取消');
|
||||
const result = await uploadMaterial(zip, (loaded, total) => renderUploadProgress(label, meter, loaded, total, 'ZIP已上传,正在校验附件…'));
|
||||
uploadedZip = { file: zip, id: result.id };
|
||||
}
|
||||
meter.value = 100; label.textContent = '上传完成,正在保存记录…';
|
||||
return { photo_ids: files.map(file => uploadedPhotos.get(file)), material_id: zip ? uploadedZip.id : null };
|
||||
};
|
||||
}
|
||||
|
||||
function openMaterialForm(activity) {
|
||||
modal(`<h3>补充活动材料</h3><p class="modal-sub muted">${esc(activity.title)} · 所有登录成员均可补充材料,无需参加活动。已有材料会保留。</p>
|
||||
<form id="material-form"><label>ZIP材料包<input name="materials" type="file" accept=".zip,application/zip" required /></label>
|
||||
<p class="muted">最大30MB,包内至少1张活动照片,照片数量不限,可附文档。</p>
|
||||
<form id="material-form">${centerUploadFields('material')}<p class="muted">照片和ZIP至少选择一种,补充内容会追加到活动中。</p>
|
||||
<div class="upload-progress hidden" id="material-progress"><p id="material-status" role="status" aria-live="polite"></p><progress id="material-meter" max="100" value="0" aria-labelledby="material-status"></progress></div>
|
||||
<div class="form-error" id="material-error"></div><div class="form-actions"><button type="button" class="btn" id="material-cancel">取消</button><button type="submit" class="btn primary">上传并保存</button></div>
|
||||
</form>`);
|
||||
const form = $('#material-form');
|
||||
const box = $('#material-progress'), label = $('#material-status'), meter = $('#material-meter'), errorBox = $('#material-error');
|
||||
let uploaded = null;
|
||||
const collectUploads = bindCenterUploads(form, box, label, meter, 'material');
|
||||
$('#material-cancel').addEventListener('click', closeModal);
|
||||
form.materials.addEventListener('change', () => { uploaded = null; box.classList.add('hidden'); });
|
||||
form.addEventListener('submit', async event => {
|
||||
event.preventDefault();
|
||||
const button = form.querySelector('[type=submit]');
|
||||
if (button.disabled) return;
|
||||
const file = form.materials.files[0];
|
||||
errorBox.textContent = '';
|
||||
if (!file || file.size > 30 * 1024 * 1024 || !file.name.toLowerCase().endsWith('.zip')) { errorBox.textContent = '请提交30MB以内的ZIP材料包'; return; }
|
||||
button.disabled = true; form.materials.disabled = true; box.classList.remove('hidden');
|
||||
if (!form.photos.files.length && !form.materials.files.length) { errorBox.textContent = '请至少选择照片或ZIP附件'; return; }
|
||||
button.disabled = true; form.materials.disabled = true; form.photos.disabled = true;
|
||||
try {
|
||||
button.textContent = '正在上传…';
|
||||
if (!uploaded) uploaded = await uploadMaterial(file, (loaded, total) => renderUploadProgress(label, meter, loaded, total));
|
||||
const uploaded = await collectUploads();
|
||||
if (!form.isConnected) return;
|
||||
meter.value = 100; label.textContent = '材料上传成功,正在保存…'; button.textContent = '正在保存…';
|
||||
await api(`/activities/${activity.id}/materials`, { method: 'POST', body: { material_id: uploaded.id } });
|
||||
await api(`/activities/${activity.id}/materials`, { method: 'POST', body: uploaded });
|
||||
closeModal(); toast('补充材料已保存'); refreshCurrent();
|
||||
} catch (error) {
|
||||
errorBox.textContent = error.message; label.textContent = uploaded ? '材料已上传,保存失败,可重试。' : '上传未完成,可重试。'; meter.value = uploaded ? 100 : 0;
|
||||
button.disabled = false; form.materials.disabled = false; button.textContent = '上传并保存';
|
||||
errorBox.textContent = error.message; label.textContent = '提交未完成,可重试;已上传成功的照片会复用。'; meter.value = 0;
|
||||
button.disabled = false; form.materials.disabled = false; form.photos.disabled = false; button.textContent = '上传并保存';
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -903,7 +950,7 @@
|
||||
<label>地点<input name="location" maxlength="120" value="${esc(existing?.location || "")}" placeholder="如:3 号会议室" /></label>
|
||||
</div>
|
||||
<label>备注 <span class="hint-inline">(选填)</span><textarea name="description" rows="3" maxlength="2000" placeholder="做什么、带什么、注意事项…">${esc(existing?.description || "")}</textarea></label>
|
||||
<div id="act-photo-fields" class="hidden"><label>活动材料 ZIP(必填)<input name="materials" type="file" accept=".zip,application/zip" /></label><p class="muted">最大30MB,包内须至少含1张活动照片,照片数量不限,可附PDF、Office或文本材料。编辑时不选新包会保留原材料。</p><p id="act-material-name" class="muted"></p><div id="act-photo-preview" class="activity-photos">${(existing?.photo_ids || []).map(id => `<img data-photo="${esc(id)}" alt="活动照片" />`).join('')}</div></div>
|
||||
<div id="act-photo-fields" class="hidden">${centerUploadFields('act')}<p class="muted">照片和ZIP至少选择一种。编辑时会保留原照片和附件,所选内容追加保存。</p><div id="act-photo-preview" class="activity-photos">${(existing?.photo_ids || []).map(id => `<img data-photo="${esc(id)}" alt="活动照片" />`).join('')}</div></div>
|
||||
<label class="checkline" id="act-visibility"><input type="checkbox" name="visibility" ${!existing || existing.visibility === "public" ? "checked" : ""} /> 公开活动(所有人可见;不勾选则仅参加者可见)</label>
|
||||
<p id="act-center-policy" class="muted hidden">全员可见,仅作活动记录,无需参加,也不计入参加统计。</p>
|
||||
<div id="act-upload-progress" class="upload-progress hidden">
|
||||
@@ -921,7 +968,7 @@
|
||||
const progressLabel = $('#act-upload-status');
|
||||
const progressMeter = $('#act-upload-meter');
|
||||
const errorBox = $('#act-error');
|
||||
let uploadedMaterial = null;
|
||||
const collectUploads = bindCenterUploads(activityForm, progressBox, progressLabel, progressMeter, 'act');
|
||||
activityForm.record_type.value = existing?.record_type || defaultType;
|
||||
activityForm.record_type.disabled = state.view === 'center';
|
||||
const togglePhotos = () => {
|
||||
@@ -934,10 +981,6 @@
|
||||
};
|
||||
activityForm.record_type.addEventListener('change', togglePhotos); togglePhotos();
|
||||
loadPhotos($('#act-photo-preview'));
|
||||
activityForm.materials.addEventListener('change', () => {
|
||||
$('#act-material-name').textContent = activityForm.materials.files[0]?.name || '';
|
||||
uploadedMaterial = null; progressBox.classList.add('hidden');
|
||||
});
|
||||
|
||||
$("#act-cancel").addEventListener("click", closeModal);
|
||||
$("#act-form").addEventListener("submit", async (e) => {
|
||||
@@ -960,23 +1003,14 @@
|
||||
const typeWasDisabled = f.record_type.disabled;
|
||||
f.record_type.disabled = true;
|
||||
f.materials.disabled = true;
|
||||
f.photos.disabled = true;
|
||||
try {
|
||||
const file = f.materials.files[0];
|
||||
if (body.record_type === 'center') {
|
||||
body.material_id = existing?.material_id || null;
|
||||
if (file) {
|
||||
if (file.size > 30 * 1024 * 1024 || !file.name.toLowerCase().endsWith('.zip')) throw new Error('请提交30MB以内的ZIP材料包');
|
||||
progressBox.classList.remove('hidden');
|
||||
btn.textContent = '正在上传…';
|
||||
if (uploadedMaterial?.file !== file) {
|
||||
const result = await uploadMaterial(file, (loaded, total) => renderUploadProgress(progressLabel, progressMeter, loaded, total));
|
||||
uploadedMaterial = { file, id: result.id };
|
||||
}
|
||||
body.material_id = uploadedMaterial.id;
|
||||
progressMeter.value = 100;
|
||||
progressLabel.textContent = '材料上传成功,正在保存活动记录…';
|
||||
}
|
||||
if (!body.material_id && !(existing?.photo_ids || []).length) throw new Error('请上传包含活动照片的ZIP材料包');
|
||||
if (!f.photos.files.length && !f.materials.files.length && !(existing?.photo_ids || []).length && !existing?.material_id && !existing?.materials?.length) throw new Error('请至少选择照片或ZIP附件');
|
||||
btn.textContent = '正在上传…';
|
||||
const uploaded = await collectUploads();
|
||||
body.photo_ids = [...new Set([...(existing?.photo_ids || []), ...uploaded.photo_ids])];
|
||||
body.material_id = uploaded.material_id || existing?.material_id || null;
|
||||
} else { body.photo_ids = []; body.material_id = null; }
|
||||
if (!f.isConnected) return;
|
||||
btn.textContent = '正在保存…';
|
||||
@@ -991,11 +1025,12 @@
|
||||
refreshCurrent();
|
||||
} catch (err) {
|
||||
errorBox.textContent = err.message;
|
||||
progressMeter.value = uploadedMaterial ? 100 : 0;
|
||||
if (!progressBox.classList.contains('hidden')) progressLabel.textContent = uploadedMaterial ? '材料已上传,记录保存失败,可重试。' : '上传未完成,可重试。';
|
||||
progressMeter.value = 0;
|
||||
if (!progressBox.classList.contains('hidden')) progressLabel.textContent = '提交未完成,可重试;已上传成功的照片会复用。';
|
||||
btn.disabled = false;
|
||||
f.record_type.disabled = typeWasDisabled;
|
||||
f.materials.disabled = false;
|
||||
f.photos.disabled = false;
|
||||
btn.textContent = isEdit ? '保存修改' : '提交记录';
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user