1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-15 10:14:44 +08:00

349 lines
11 KiB
Go
Raw Normal View History

2022-12-30 14:22:46 +08:00
import i18n from '@/lang';
2022-08-16 23:30:23 +08:00
export function deepCopy<T>(obj: any): T {
let newObj: any;
try {
newObj = obj.push ? [] : {};
} catch (error) {
newObj = {};
}
for (let attr in obj) {
if (typeof obj[attr] === 'object') {
newObj[attr] = deepCopy(obj[attr]);
} else {
newObj[attr] = obj[attr];
}
}
return newObj;
}
export function randomNum(min: number, max: number): number {
let num = Math.floor(Math.random() * (min - max) + max);
return num;
}
export function getBrowserLang() {
let browserLang = navigator.language ? navigator.language : navigator.browserLanguage;
let defaultBrowserLang = '';
if (
browserLang.toLowerCase() === 'cn' ||
browserLang.toLowerCase() === 'zh' ||
browserLang.toLowerCase() === 'zh-cn'
) {
defaultBrowserLang = 'zh';
} else {
defaultBrowserLang = 'en';
}
return defaultBrowserLang;
}
2023-01-16 15:55:53 +08:00
export function dateFormat(row: any, col: any, dataStr: any) {
2022-08-16 23:30:23 +08:00
const date = new Date(dataStr);
const y = date.getFullYear();
let m: string | number = date.getMonth() + 1;
m = m < 10 ? `0${String(m)}` : m;
let d: string | number = date.getDate();
d = d < 10 ? `0${String(d)}` : d;
let h: string | number = date.getHours();
h = h < 10 ? `0${String(h)}` : h;
let minute: string | number = date.getMinutes();
minute = minute < 10 ? `0${String(minute)}` : minute;
let second: string | number = date.getSeconds();
second = second < 10 ? `0${String(second)}` : second;
return `${String(y)}-${String(m)}-${String(d)} ${String(h)}:${String(minute)}:${String(second)}`;
}
2022-09-06 17:48:49 +08:00
//2016-01-12
2023-01-16 15:55:53 +08:00
export function dateFormatSimple(dataStr: any) {
const date = new Date(dataStr);
const y = date.getFullYear();
let m: string | number = date.getMonth() + 1;
m = m < 10 ? `0${String(m)}` : m;
let d: string | number = date.getDate();
d = d < 10 ? `0${String(d)}` : d;
return `${String(y)}-${String(m)}-${String(d)}`;
}
2022-10-13 18:24:24 +08:00
// 20221013151302
2023-01-16 15:55:53 +08:00
export function dateFormatForName(dataStr: any) {
const date = new Date(dataStr);
const y = date.getFullYear();
let m: string | number = date.getMonth() + 1;
m = m < 10 ? `0${String(m)}` : m;
let d: string | number = date.getDate();
d = d < 10 ? `0${String(d)}` : d;
let h: string | number = date.getHours();
h = h < 10 ? `0${String(h)}` : h;
let minute: string | number = date.getMinutes();
minute = minute < 10 ? `0${String(minute)}` : minute;
let second: string | number = date.getSeconds();
second = second < 10 ? `0${String(second)}` : second;
return `${String(y)}${String(m)}${String(d)}${String(h)}${String(minute)}${String(second)}`;
}
2022-10-13 18:24:24 +08:00
// 10-13 \n 15:13
2023-01-16 15:55:53 +08:00
export function dateFormatWithoutYear(dataStr: any) {
2022-09-08 12:06:53 +08:00
const date = new Date(dataStr);
let m: string | number = date.getMonth() + 1;
m = m < 10 ? `0${String(m)}` : m;
let d: string | number = date.getDate();
d = d < 10 ? `0${String(d)}` : d;
let h: string | number = date.getHours();
h = h < 10 ? `0${String(h)}` : h;
let minute: string | number = date.getMinutes();
minute = minute < 10 ? `0${String(minute)}` : minute;
return `${String(m)}-${String(d)}\n${String(h)}:${String(minute)}`;
}
2022-10-13 18:24:24 +08:00
// 20221013151302
2023-01-16 15:55:53 +08:00
export function dateFormatForSecond(dataStr: any) {
2022-10-13 18:24:24 +08:00
const date = new Date(dataStr);
let h: string | number = date.getHours();
h = h < 10 ? `0${String(h)}` : h;
let minute: string | number = date.getMinutes();
minute = minute < 10 ? `0${String(minute)}` : minute;
let second: string | number = date.getSeconds();
second = second < 10 ? `0${String(second)}` : second;
return `${String(h)}:${String(minute)}:${String(second)}`;
}
2022-09-06 17:48:49 +08:00
export function getRandomStr(e: number): string {
const t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
const a = t.length;
let n = '';
for (let i = 0; i < e; i++) {
n += t.charAt(Math.floor(Math.random() * a));
}
return n;
}
export function getDBName(e: number): string {
const t = 'abcdefhijkmnprstwxyz2345678';
const a = t.length;
let n = '';
for (let i = 0; i < e; i++) {
n += t.charAt(Math.floor(Math.random() * a));
}
return n;
}
2022-09-29 11:13:05 +08:00
export function loadZero(i: number) {
return i < 10 ? '0' + i : '' + i;
}
export function computeSize(size: number): string {
const num = 1024.0;
if (size < num) return size + ' B';
if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + ' KB';
if (size < Math.pow(num, 3)) return (size / Math.pow(num, 2)).toFixed(2) + ' MB';
if (size < Math.pow(num, 4)) return (size / Math.pow(num, 3)).toFixed(2) + ' GB';
return (size / Math.pow(num, 4)).toFixed(2) + ' TB';
}
2022-09-09 18:27:50 +08:00
export function computeSizeFromMB(size: number): string {
const num = 1024.0;
if (size < num) return size + ' MB';
if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + ' GB';
return (size / Math.pow(num, 3)).toFixed(2) + ' TB';
}
export function computeSizeFromKBs(size: number): string {
const num = 1024.0;
if (size < num) return size + ' KB/s';
if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + ' MB/s';
if (size < Math.pow(num, 3)) return (size / Math.pow(num, 2)).toFixed(2) + ' GB/s';
return (size / Math.pow(num, 3)).toFixed(2) + ' TB/s';
}
2022-09-09 18:27:50 +08:00
let icons = new Map([
['.zip', 'p-file-zip'],
['.gz', 'p-file-zip'],
['.tar.bz2', 'p-file-zip'],
['.tar', 'p-file-zip'],
['.tar.gz', 'p-file-zip'],
['.tar.xz', 'p-file-zip'],
2023-01-30 10:10:40 +08:00
['.mp3', 'p-file-mp3'],
['.svg', 'p-file-svg'],
['.txt', 'p-file-txt'],
['.html', 'p-file-html'],
['.word', 'p-file-word'],
['.ppt', 'p-file-ppt'],
['.jpg', 'p-file-jpg'],
['.xlsx', 'p-file-excel'],
['.doc', 'p-file-word'],
['.pdf', 'p-file-pdf'],
2022-09-09 18:27:50 +08:00
]);
export function getIcon(extention: string): string {
if (icons.get(extention) != undefined) {
const icon = icons.get(extention);
return String(icon);
} else {
return 'p-file-normal';
}
}
2022-12-06 15:55:42 +08:00
export function checkIp(value: string): boolean {
if (value === '') {
return true;
}
2022-12-06 15:55:42 +08:00
const reg =
/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
if (!reg.test(value) && value !== '') {
return true;
} else {
return false;
}
2023-07-07 17:41:13 +08:00
}
export function checkDomain(value: string): boolean {
if (value === '') {
return true;
}
const reg = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
if (!reg.test(value) && value !== '') {
return true;
} else {
return false;
}
}
2023-07-07 17:41:13 +08:00
export function checkIpV4V6(value: string): boolean {
if (value === '') {
return true;
}
const IPv4SegmentFormat = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
const IPv4AddressFormat = `(${IPv4SegmentFormat}[.]){3}${IPv4SegmentFormat}`;
const IPv4AddressRegExp = new RegExp(`^${IPv4AddressFormat}$`);
const IPv6SegmentFormat = '(?:[0-9a-fA-F]{1,4})';
const IPv6AddressRegExp = new RegExp(
'^(' +
`(?:${IPv6SegmentFormat}:){7}(?:${IPv6SegmentFormat}|:)|` +
`(?:${IPv6SegmentFormat}:){6}(?:${IPv4AddressFormat}|:${IPv6SegmentFormat}|:)|` +
`(?:${IPv6SegmentFormat}:){5}(?::${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,2}|:)|` +
`(?:${IPv6SegmentFormat}:){4}(?:(:${IPv6SegmentFormat}){0,1}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,3}|:)|` +
`(?:${IPv6SegmentFormat}:){3}(?:(:${IPv6SegmentFormat}){0,2}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,4}|:)|` +
`(?:${IPv6SegmentFormat}:){2}(?:(:${IPv6SegmentFormat}){0,3}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,5}|:)|` +
`(?:${IPv6SegmentFormat}:){1}(?:(:${IPv6SegmentFormat}){0,4}:${IPv4AddressFormat}|(:${IPv6SegmentFormat}){1,6}|:)|` +
`(?::((?::${IPv6SegmentFormat}){0,5}:${IPv4AddressFormat}|(?::${IPv6SegmentFormat}){1,7}|:))` +
')(%[0-9a-zA-Z-.:]{1,})?$',
);
if (!IPv4AddressRegExp.test(value) && !IPv6AddressRegExp.test(value) && value !== '') {
return true;
} else {
return false;
}
2022-12-06 15:55:42 +08:00
}
2022-12-30 14:22:46 +08:00
export function checkCidr(value: string): boolean {
if (value === '') {
return true;
}
const reg =
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\/([0-9]|[1-2][0-9]|3[0-2]))?$/;
if (!reg.test(value) && value !== '') {
return true;
} else {
return false;
}
}
2023-04-01 00:51:25 +08:00
export function checkPort(value: string): boolean {
if (Number(value) <= 0) {
return true;
}
const reg = /^([1-9](\d{0,3}))$|^([1-5]\d{4})$|^(6[0-4]\d{3})$|^(65[0-4]\d{2})$|^(655[0-2]\d)$|^(6553[0-5])$/;
2023-04-01 00:51:25 +08:00
if (!reg.test(value) && value !== '') {
return true;
} else {
return false;
}
}
2022-12-30 14:22:46 +08:00
export function getProvider(provider: string): string {
switch (provider) {
case 'dnsAccount':
return i18n.global.t('website.dnsAccount');
case 'dnsManual':
return i18n.global.t('website.dnsManual');
case 'http':
return 'HTTP';
default:
return i18n.global.t('ssl.manualCreate');
}
}
2023-01-16 15:30:24 +08:00
export function getAge(d1: string): string {
const dateBegin = new Date(d1);
const dateEnd = new Date();
const dateDiff = dateEnd.getTime() - dateBegin.getTime();
const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));
const leave1 = dateDiff % (24 * 3600 * 1000);
const hours = Math.floor(leave1 / (3600 * 1000));
const leave2 = leave1 % (3600 * 1000);
const minutes = Math.floor(leave2 / (60 * 1000));
let res = '';
if (dayDiff > 0) {
res += String(dayDiff) + i18n.global.t('commons.units.day');
2023-01-16 15:30:24 +08:00
if (hours <= 0) {
return res;
}
}
if (hours > 0) {
res += String(hours) + i18n.global.t('commons.units.hour');
2023-01-16 15:30:24 +08:00
return res;
}
if (minutes > 0) {
res += String(minutes) + i18n.global.t('commons.units.minute');
2023-01-16 15:30:24 +08:00
return res;
}
return i18n.global.t('app.less1Minute');
}
export function isJson(str: string) {
try {
if (typeof JSON.parse(str) === 'object') {
return true;
}
} catch {
return false;
}
}
2023-03-31 14:02:28 +08:00
export function toLowerCase(str: string) {
return str.toLowerCase();
}
export function downloadFile(filePath: string) {
let url = `${import.meta.env.VITE_API_URL as string}/files/download?`;
window.open(url + 'path=' + filePath, '_blank');
}
export function downloadWithContent(content: string, fileName: string) {
const downloadUrl = window.URL.createObjectURL(new Blob([content]));
const a = document.createElement('a');
a.style.display = 'none';
a.href = downloadUrl;
a.download = fileName;
const event = new MouseEvent('click');
a.dispatchEvent(event);
}
export function getDateStr() {
let now: Date = new Date();
let year: number = now.getFullYear();
let month: number = now.getMonth() + 1;
let date: number = now.getDate();
let hours: number = now.getHours();
let minutes: number = now.getMinutes();
let seconds: number = now.getSeconds();
let timestamp: string = `${year}-${month < 10 ? '0' + month : month}-${date < 10 ? '0' + date : date}-${
hours < 10 ? '0' + hours : hours
}-${minutes < 10 ? '0' + minutes : minutes}-${seconds < 10 ? '0' + seconds : seconds}`;
return timestamp;
}