xbccs
Version:
customer service
163 lines (162 loc) • 4.92 kB
JavaScript
import CryptoJS from 'crypto-js';
export const getEnv = () => {
try {
if (uni === null || uni === void 0 ? void 0 : uni.connectSocket) {
return 'uniapp';
}
if (window === null || window === void 0 ? void 0 : window.WebSocket) {
return 'web';
}
}
catch (error) {
if (window === null || window === void 0 ? void 0 : window.WebSocket) {
return 'web';
}
}
};
export const getDeviceInfo = () => {
switch (getEnv()) {
case 'uniapp':
const systemInfo = uni.getSystemInfoSync();
const { ua, osName, appName, language, appLanguage, uniPlatform, screenWidth, screenHeight } = systemInfo;
return {
appName,
language: language || appLanguage,
platform: `${uniPlatform} ${osName}`,
userAgent: ua,
screenResolution: `${screenWidth}x${screenHeight}`,
};
case 'web':
return {
language: navigator.language,
platform: navigator.platform,
userAgent: navigator.userAgent,
screenResolution: `${window.screen.width}x${window.screen.height}`,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
}
};
export const encrypt = (text = '', key = 'b09450b5f576e086') => {
if (!text)
return '';
return CryptoJS.AES.encrypt(text, CryptoJS.enc.Utf8.parse(key), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
}).toString();
};
export const decrypt = (text = '', key = 'b09450b5f576e086') => {
if (!text)
return '';
return CryptoJS.AES.decrypt(text, CryptoJS.enc.Utf8.parse(key), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
}).toString(CryptoJS.enc.Utf8);
};
export function formatBytes(bytes) {
if (bytes < 1024) {
return `${bytes} B`;
}
else if (bytes < 1024 * 1024) {
return `${(bytes / 1024).toFixed(2)} KB`;
}
else if (bytes < 1024 * 1024 * 1024) {
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}
else {
return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}
}
export function formatDuration(duration) {
if (!duration) {
return '';
}
let durationStr = '';
const [hours, minutes, seconds] = Array.from(duration.toString().split(':')).map(item => parseInt(item).toString().padStart(2, '0'));
if (hours == '00') {
durationStr = `${minutes}:${seconds}`;
}
else {
durationStr = `${hours}:${minutes}:${seconds}`;
}
return durationStr;
}
export function getImageSize(imgWidth, imgHeight, max = 200) {
if (imgWidth < max && imgHeight < max) {
return { width: `${imgWidth}px`, height: `${imgHeight}px` };
}
if (imgWidth > imgHeight) {
return { width: `${max}px`, height: `${imgHeight / imgWidth * max}px` };
}
else if (imgWidth < imgHeight) {
return { width: `${imgWidth / imgHeight * max}px`, height: `${max}px` };
}
else {
return { width: `${imgWidth}px`, height: `${max}px` };
}
}
export const getFileType = (fileExt) => {
const ext = fileExt.toLowerCase();
const types = {
pdf: 'pdf',
doc: 'word',
docx: 'word',
xls: 'excel',
xlsx: 'excel',
ppt: 'ppt',
pptx: 'ppt',
jpg: 'image',
jpeg: 'image',
png: 'image',
gif: 'image',
bmp: 'image',
mp3: 'audio',
mp4: 'video',
avi: 'video',
wmv: 'video',
flv: 'video',
mkv: 'video',
mov: 'video',
wma: 'audio',
wav: 'audio',
aac: 'audio',
ogg: 'audio',
zip: 'zip',
rar: 'zip',
tar: 'zip',
gz: 'zip',
bz2: 'zip',
'7z': 'zip'
};
return types[ext] || 'unknow';
};
export function stripHtmlTags(str) {
return str.replace(/<\/?[a-z][^>]*(\s+[a-z-]+(="[^"]*")?)*>/gi, '');
}
export const isEmpty = (str = '') => {
return !/[^\s]/.test(str);
};
export const isPhone = (phone = '') => {
const reg = /^1[3-9]\d{9}$/;
return reg.test(phone);
};
export const isEmail = (email = '') => {
const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
return reg.test(email);
};
export function htmlEntities(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/`/g, '`')
.replace(/\//g, '/')
.replace(/\\/g, '\')
.replace(/{/g, '{')
.replace(/}/g, '}')
.replace(/\[/g, '[')
.replace(/\]/g, ']')
.replace(/\(/g, '(')
.replace(/\)/g, ')');
}