Files
jiapu/public/js/upload-pages.js
T
2026-07-09 17:30:10 +08:00

133 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(function (root, factory) {
// 公共上传模块同时支持浏览器页面和 Node 单元测试。
if (typeof module === 'object' && module.exports) {
module.exports = factory(root);
return;
}
root.UploadPages = factory(root);
if (root.document) {
root.document.addEventListener('DOMContentLoaded', function () {
root.UploadPages.init();
});
}
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
'use strict';
var documentRef = root.document;
function getApi() {
return root.GenealogyApi && root.GenealogyApi.defaultClient;
}
function query(selector, rootNode) {
return (rootNode || documentRef).querySelector(selector);
}
function showMessage(message) {
if (root.layui && root.layui.layer) {
root.layui.layer.msg(message);
return;
}
root.alert(message);
}
function pick(item, keys, fallback) {
var source = item || {};
var index;
var value;
for (index = 0; index < keys.length; index += 1) {
value = source[keys[index]];
if (value !== undefined && value !== null && value !== '') return value;
}
return fallback || '';
}
function normalizeUploadResult(data) {
// FileUploadResult 字段可能因后端实现不同而略有差异,这里只收敛页面需要的值。
var source = data || {};
var ossId = pick(source, ['ossId', 'id', 'fileId', 'oss_id'], '');
var fileName = pick(source, ['fileName', 'originalName', 'name'], '');
var url = pick(source, ['url', 'fileUrl', 'previewUrl'], '');
return {
ossId: String(ossId || ''),
fileName: String(fileName || ''),
url: String(url || '')
};
}
function mergeOssIds(currentValue, nextValue, multiple) {
var next = String(nextValue || '').trim();
var current = String(currentValue || '').trim();
var values;
if (!next) return current;
if (!multiple) return next;
values = current ? current.split(',').map(function (item) {
return item.trim();
}).filter(Boolean) : [];
if (values.indexOf(next) === -1) values.push(next);
return values.join(',');
}
function buildUploadStatus(fileName, ossId) {
return (fileName || '文件') + ' 上传完成,OSS ID' + ossId;
}
async function uploadFromInput(input) {
var api = getApi();
var file = input.files && input.files[0];
var targetSelector = input.getAttribute('data-upload-target');
var statusSelector = input.getAttribute('data-upload-status');
var target = targetSelector && query(targetSelector);
var status = statusSelector && query(statusSelector);
var multiple = input.getAttribute('data-upload-multiple') === 'true';
var result;
if (!api || !file || !target) return;
if (status) status.textContent = '上传中...';
input.disabled = true;
try {
result = normalizeUploadResult(await api.uploadFile(file));
if (!result.ossId) throw new Error('上传结果缺少 OSS ID');
target.value = mergeOssIds(target.value, result.ossId, multiple);
if (status) status.textContent = buildUploadStatus(result.fileName || file.name, result.ossId);
showMessage('文件上传完成');
} catch (error) {
if (status) status.textContent = error.message || '上传失败';
showMessage(error.message || '上传失败');
} finally {
input.disabled = false;
input.value = '';
}
}
function bindActions() {
if (!documentRef) return;
documentRef.addEventListener('change', function (event) {
var input = event.target.closest('[data-upload-target]');
if (!input || input.type !== 'file') return;
uploadFromInput(input);
});
}
function init() {
bindActions();
}
return {
normalizeUploadResult: normalizeUploadResult,
mergeOssIds: mergeOssIds,
buildUploadStatus: buildUploadStatus,
init: init
};
});