initial commit

This commit is contained in:
XZB
2022-03-16 16:26:28 +08:00
commit 740e33f3a1
73 changed files with 12937 additions and 0 deletions

61
web/src/utils/utils.js Normal file
View File

@@ -0,0 +1,61 @@
import axios from 'axios';
import Qs from 'qs';
function request(url, data, headers, ext, noTrans) {
let _headers = headers ?? {};
_headers = Object.assign({'Content-Type': 'application/x-www-form-urlencoded'}, _headers);
return axios(Object.assign({
url: url,
data: data,
method: 'post',
headers: _headers,
transformRequest: noTrans ? [] : [Qs.stringify],
}, ext??{}));
};
function waitTime(time) {
time = (time ?? 100);
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, time);
});
};
function formatSize(bytes) {
if (bytes === 0) return 'Unknown';
let k = 1024,
i = Math.floor(Math.log(bytes) / Math.log(k)),
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i];
}
function tsToTime(ts) {
if (isNaN(ts)) return 'Unknown';
let hours = Math.floor(ts / 3600);
ts %= 3600;
let minutes = Math.floor(ts / 60);
return `${hours}小时${minutes}分钟`;
}
function post(url, data, ext) {
let form = document.createElement('form');
form.action = url;
form.method = 'POST';
form.target = '_self';
for (const key in ext) {
form[key] = ext[key];
}
for (const key in data) {
let input = document.createElement('input');
input.name = key;
input.value = data[key];
form.appendChild(input);
}
document.body.appendChild(form).submit();
form.remove();
}
export {post, request, waitTime, formatSize, tsToTime};