Files
jiapu/public/js/upload-pages.js
T
2026-07-11 21:36:26 +08:00

107 lines
2.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) {
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 normalizeUploadResult(data) {
var source = data || {};
return {
ossId: String(source.ossId || ''),
fileName: source.fileName || source.originalName || '',
url: source.url || ''
};
}
function buildUploadStatus(fileName, ossId) {
return (fileName || '文件') + ' 上传完成,OSS ID' + ossId;
}
function getUploadMode() {
// 本期只保留头像单文件上传,不再根据文件大小切换分片流程。
return 'single';
}
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;
}
if (root.alert) root.alert(message);
}
function uploadFileForPage(api, file) {
// 上传结果仅用于回填资料表单中的 avatarOssId。
if (!api || !file) return Promise.reject(new Error('请选择文件'));
return api.uploadFile(file);
}
async function uploadFromInput(input) {
var api = getApi();
var file = input.files && input.files[0];
var target = query(input.getAttribute('data-upload-target'));
var status = query(input.getAttribute('data-upload-status'));
var result;
if (!api || !file || !target) return;
input.disabled = true;
try {
result = normalizeUploadResult(await uploadFileForPage(api, file));
if (!result.ossId) throw new Error('上传响应缺少 ossId');
target.value = result.ossId;
if (status) status.textContent = buildUploadStatus(result.fileName || file.name, result.ossId);
} 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,
buildUploadStatus: buildUploadStatus,
getUploadMode: getUploadMode,
uploadFileForPage: uploadFileForPage,
init: init
};
});