1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
import router from '@/router/index';
import { useSystemStore } from '@/store/index';
import { cloneDeep, get } from 'lodash';
import { useI18n } from 'vue-i18n';
import { administrator } from '@/utils/constants';
export * from './unit';
export * from './file';
export * from './validator';
/**
* * 判断是否是开发环境
* @return { Boolean }
*/
export const isDev = () => {
return import.meta.env.DEV;
};
// 退出登录清除数据
export const logoutClear = () => {
localStorage.removeItem('asg-userInfo');
router.push({ name: 'login' });
};
// 跳转到首页
export const goHome = async () => {
const systemStore = useSystemStore();
const routeName = get(systemStore, 'menus.0.name', '');
const workspace = get(systemStore, 'workspace.name', '');
await router.push({
name: routeName,
params: {
workspace: workspace,
},
});
};
export function hash(str, lenHash = 32) {
lenHash = lenHash || 32;
str = str || '';
let ar = str.split('').map((a) => a.charCodeAt(0)),
s2alength = ar.length || 1,
i = ar.length ? ar.reduce((p, c) => p + c) : 1,
s = '',
A,
B,
k = 0,
tan = Math.tan;
while (s.length < lenHash) {
A = ar[k++ % s2alength] || 0.5;
B = ar[k++ % s2alength ^ lenHash] || 1.5 ^ lenHash;
i = i + ((A ^ B) % lenHash);
s += tan((i * B) / A)
.toString(16)
.split('.')[1]
.slice(0, 10);
}
return s.slice(0, lenHash);
}
// 三位数加逗号
export const toThousands = (number) => {
return number.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
};
// 获取多久时间之前
export const getTimeAgo = (timestamp1, timestamp2) => {
const { t } = useI18n();
const diff = Math.abs(timestamp1 - timestamp2) / 1000; // 转为秒
let number = '';
let unit = '';
if (diff < 60) {
unit = t('overall.just_now');
} else if (diff < 3600) {
number = Math.floor(diff / 60);
unit = t('overall.minutes');
} else if (diff < 86400) {
number = Math.floor(diff / 3600);
unit = t('overall.hours');
} else if (diff < 604800) {
number = Math.floor(diff / 86400);
unit = t('overall.days');
} else if (diff < 2592000) {
number = Math.floor(diff / 604800);
unit = t('overall.weeks');
} else if (diff < 31536000) {
number = Math.floor(diff / 2592000);
unit = t('overall.months');
} else {
number = Math.floor(diff / 31536000);
unit = t('overall.years');
}
return t('overall.time_ago', {
number,
unit,
ago: number ? t('overall.ago') : '',
});
};
export function getUUID() {
function S4() {
return (
((1 + window.crypto.getRandomValues(new Uint32Array(10))[0]) * 0x10000) |
0
)
.toString(16)
.substring(1);
}
return (
S4() +
S4() +
'-' +
S4() +
'-' +
S4() +
'-' +
S4() +
'-' +
S4() +
S4() +
S4()
);
}
export function isAdministrator() {
let isAdmin = false;
let info = localStorage.getItem('asg-userInfo');
try {
let userInfo = JSON.parse(info);
if (userInfo.accessLevel === administrator) {
isAdmin = true;
}
} catch (error) {}
return isAdmin;
}
|