Files
jiapu/public/js/upload-pages.js
T

129 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) {
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 || {};
var ossId = source.ossId;
return {
// int64 OSS ID 不能经由 Number 转换,直接保留服务端原始文本。
ossId: ossId === undefined || ossId === null ? '' : String(ossId),
fileName: source.fileName || source.originalName || '',
url: source.url || ''
};
}
function buildUploadStatus(fileName, ossId) {
return (fileName || '文件') + ' 上传完成,OSS ID' + ossId;
}
function getUploadMode() {
// 当前 PC 目录只定义统一分片上传流程。
return 'resumable';
}
function getApi() {
return root.GenealogyApi && root.GenealogyApi.defaultClient;
}
function shouldRedirectToLogin(api, error) {
var status = error && (error.status || error.code);
return !api || !api.getToken || !api.getToken() || Number(status) === 401;
}
function redirectUnauthorized(api, error) {
if (!shouldRedirectToLogin(api, error)) return false;
if (api && api.clearToken) api.clearToken();
if (!root.location) return true;
if (typeof root.location.replace === 'function') {
root.location.replace('login.html');
return true;
}
root.location.href = 'login.html';
return true;
}
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) {
// 当前初始化响应未展开 uploadId、instant 与 OSS 字段,不能猜测分片闭环。
if (!api || !file) return Promise.reject(new Error('请选择文件'));
return Promise.reject(new Error('当前 PC 文件上传响应未定义头像回填字段,暂不能上传头像'));
}
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 (!file || !target || redirectUnauthorized(api)) 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 (redirectUnauthorized(api, error)) return;
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,
shouldRedirectToLogin: shouldRedirectToLogin,
init: init
};
});