|
export function parseTime (time) {
|
if (time) {
|
var date = new Date(time)
|
var year = date.getFullYear()
|
/* 在日期格式中,月份是从0开始的,因此要加0
|
* 使用三元表达式在小于10的前面加0,以达到格式统一 如 09:11:05
|
* */
|
var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
|
var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
|
var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
|
var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
|
var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
|
// 拼接
|
return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
|
} else {
|
return ''
|
}
|
}
|
|
export function removeEmptyValues(obj) {
|
const result = {};
|
for (const key in obj) {
|
if (obj[key] != null && obj[key] !== '') {
|
result[key] = obj[key];
|
}
|
}
|
return result;
|
}
|
|
export function resetObjectValues(obj) {
|
Object.keys(obj).forEach(key => {
|
obj[key] = '';
|
});
|
return obj;
|
}
|
|
export function generateRandomString(length = 16) {
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
let result = '';
|
for (let i = 0; i < length; i++) {
|
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
}
|
return result;
|
}
|
|
/**
|
* 节流函数优化版
|
* @param {Function} func 需要节流的函数
|
* @param {number} wait 节流时间间隔(毫秒)
|
* @param {Object} [options={}] 配置选项
|
* @param {boolean} [options.leading=true] 是否允许首次立即执行
|
* @param {boolean} [options.trailing=true] 是否允许最后一次延迟执行
|
*/
|
export function throttle(func, wait, options = {}) {
|
let timeout, context, args;
|
let previous = 0;
|
|
const later = () => {
|
previous = options.leading === false ? 0 : Date.now();
|
timeout = null;
|
func.apply(context, args);
|
if (!timeout) context = args = null;
|
};
|
|
const throttled = function() {
|
const now = Date.now();
|
if (!previous && options.leading === false) previous = now;
|
|
const remaining = wait - (now - previous);
|
context = this;
|
args = arguments;
|
|
if (remaining <= 0 || remaining > wait) {
|
if (timeout) {
|
clearTimeout(timeout);
|
timeout = null;
|
}
|
previous = now;
|
func.apply(context, args);
|
if (!timeout) context = args = null;
|
} else if (!timeout && options.trailing !== false) {
|
timeout = setTimeout(later, remaining);
|
}
|
};
|
|
return throttled;
|
}
|
|
/**
|
* 根据浏览器语言,映射到 messages 中对应的键(EN, CN, ES, ...)
|
* @returns {string} 语言键,如 'EN', 'CN'
|
*/
|
export function getBrowserLocale() {
|
const navLang = navigator.language
|
console.log('浏览器语言:', navLang)
|
// 提取语言前缀(前两个字母)
|
let langPrefix = navLang.substring(0, 2).toLowerCase()
|
// 特殊处理中文:无论 zh-CN / zh-TW / zh-HK 都映射为 CN
|
if (langPrefix === 'zh') {
|
return 'CN'
|
}
|
// 其他语言映射(支持所有你已配置的语言)
|
const map = {
|
en: 'EN',
|
es: 'ES',
|
fr: 'FR',
|
de: 'DE',
|
ru: 'RU',
|
ar: 'AR',
|
pt: 'PT',
|
ko: 'KO'
|
}
|
// 如果前缀在映射表中,返回对应的大写键,否则返回默认语言(这里默认英文)
|
return map[langPrefix] || 'EN'
|
}
|