完成10%
This commit is contained in:
@@ -17,9 +17,11 @@
|
||||
|
||||
function normalizeUploadResult(data) {
|
||||
var source = data || {};
|
||||
var ossId = source.ossId;
|
||||
|
||||
return {
|
||||
ossId: String(source.ossId || ''),
|
||||
// int64 OSS ID 不能经由 Number 转换,直接保留服务端原始文本。
|
||||
ossId: ossId === undefined || ossId === null ? '' : String(ossId),
|
||||
fileName: source.fileName || source.originalName || '',
|
||||
url: source.url || ''
|
||||
};
|
||||
@@ -38,6 +40,24 @@
|
||||
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);
|
||||
}
|
||||
@@ -64,7 +84,7 @@
|
||||
var status = query(input.getAttribute('data-upload-status'));
|
||||
var result;
|
||||
|
||||
if (!api || !file || !target) return;
|
||||
if (!file || !target || redirectUnauthorized(api)) return;
|
||||
input.disabled = true;
|
||||
|
||||
try {
|
||||
@@ -73,6 +93,7 @@
|
||||
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 {
|
||||
@@ -101,6 +122,7 @@
|
||||
buildUploadStatus: buildUploadStatus,
|
||||
getUploadMode: getUploadMode,
|
||||
uploadFileForPage: uploadFileForPage,
|
||||
shouldRedirectToLogin: shouldRedirectToLogin,
|
||||
init: init
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user