472 lines
11 KiB
JavaScript
472 lines
11 KiB
JavaScript
// 公共方法工具类
|
||
|
||
/**
|
||
* 日期处理相关方法
|
||
*/
|
||
export const dateUtil = {
|
||
/**
|
||
* 格式化日期
|
||
* @param {Date|string|number} date - 日期对象或时间戳
|
||
* @param {string} format - 格式化字符串,默认为 'YYYY-MM-DD HH:mm:ss'
|
||
* @returns {string} 格式化后的日期字符串
|
||
*/
|
||
format(date, format = 'YYYY-MM-DD HH:mm:ss') {
|
||
if (!date) return '';
|
||
|
||
if (typeof date === 'string' || typeof date === 'number') {
|
||
date = new Date(date);
|
||
}
|
||
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
const hours = String(date.getHours()).padStart(2, '0');
|
||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||
const seconds = String(date.getSeconds()).padStart(2, '0');
|
||
|
||
return format
|
||
.replace('YYYY', year)
|
||
.replace('MM', month)
|
||
.replace('DD', day)
|
||
.replace('HH', hours)
|
||
.replace('mm', minutes)
|
||
.replace('ss', seconds);
|
||
},
|
||
|
||
/**
|
||
* 获取当前日期
|
||
* @param {string} format - 格式化字符串
|
||
* @returns {string} 当前日期字符串
|
||
*/
|
||
now(format = 'YYYY-MM-DD HH:mm:ss') {
|
||
return this.format(new Date(), format);
|
||
},
|
||
|
||
/**
|
||
* 获取相对时间
|
||
* @param {Date|string|number} date - 日期对象或时间戳
|
||
* @returns {string} 相对时间描述
|
||
*/
|
||
relative(date) {
|
||
if (!date) return '';
|
||
|
||
if (typeof date === 'string' || typeof date === 'number') {
|
||
date = new Date(date);
|
||
}
|
||
|
||
const now = new Date();
|
||
const diff = now - date;
|
||
const seconds = Math.floor(diff / 1000);
|
||
const minutes = Math.floor(seconds / 60);
|
||
const hours = Math.floor(minutes / 60);
|
||
const days = Math.floor(hours / 24);
|
||
|
||
if (seconds < 60) return '刚刚';
|
||
if (minutes < 60) return `${minutes}分钟前`;
|
||
if (hours < 24) return `${hours}小时前`;
|
||
if (days < 30) return `${days}天前`;
|
||
|
||
return this.format(date, 'YYYY-MM-DD');
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 数字处理相关方法
|
||
*/
|
||
export const numberUtil = {
|
||
/**
|
||
* 格式化数字,添加千分位
|
||
* @param {number|string} num - 数字或数字字符串
|
||
* @returns {string} 格式化后的数字字符串
|
||
*/
|
||
format(num) {
|
||
if (num === null || num === undefined) return '0';
|
||
|
||
const number = typeof num === 'string' ? parseFloat(num) : num;
|
||
if (isNaN(number)) return '0';
|
||
|
||
return number.toLocaleString();
|
||
},
|
||
|
||
/**
|
||
* 格式化金额
|
||
* @param {number|string} amount - 金额
|
||
* @param {number} decimals - 小数位数,默认为2
|
||
* @returns {string} 格式化后的金额字符串
|
||
*/
|
||
formatMoney(amount, decimals = 2) {
|
||
if (amount === null || amount === undefined) return '0.00';
|
||
|
||
const number = typeof amount === 'string' ? parseFloat(amount) : amount;
|
||
if (isNaN(number)) return '0.00';
|
||
|
||
return number.toFixed(decimals);
|
||
},
|
||
|
||
/**
|
||
* 随机数生成
|
||
* @param {number} min - 最小值
|
||
* @param {number} max - 最大值
|
||
* @returns {number} 随机数
|
||
*/
|
||
random(min, max) {
|
||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 字符串处理相关方法
|
||
*/
|
||
export const stringUtil = {
|
||
/**
|
||
* 截断字符串
|
||
* @param {string} str - 原始字符串
|
||
* @param {number} length - 截断长度
|
||
* @param {string} suffix - 后缀,默认为 '...'
|
||
* @returns {string} 截断后的字符串
|
||
*/
|
||
truncate(str, length, suffix = '...') {
|
||
if (!str || str.length <= length) return str;
|
||
|
||
return str.substring(0, length) + suffix;
|
||
},
|
||
|
||
/**
|
||
* 生成唯一ID
|
||
* @returns {string} 唯一ID
|
||
*/
|
||
uuid() {
|
||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||
const r = Math.random() * 16 | 0;
|
||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||
return v.toString(16);
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 手机号脱敏
|
||
* @param {string} phone - 手机号
|
||
* @returns {string} 脱敏后的手机号
|
||
*/
|
||
maskPhone(phone) {
|
||
if (!phone || phone.length !== 11) return phone;
|
||
|
||
return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
|
||
},
|
||
|
||
/**
|
||
* 身份证号脱敏
|
||
* @param {string} idCard - 身份证号
|
||
* @returns {string} 脱敏后的身份证号
|
||
*/
|
||
maskIdCard(idCard) {
|
||
if (!idCard) return idCard;
|
||
|
||
const length = idCard.length;
|
||
if (length < 8) return idCard;
|
||
|
||
return idCard.substring(0, 4) + '*'.repeat(length - 8) + idCard.substring(length - 4);
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 设备信息相关方法
|
||
*/
|
||
export const deviceUtil = {
|
||
/**
|
||
* 获取系统信息
|
||
* @returns {Promise<Object>} 系统信息对象
|
||
*/
|
||
getSystemInfo() {
|
||
return new Promise((resolve, reject) => {
|
||
uni.getSystemInfo({
|
||
success: resolve,
|
||
fail: reject
|
||
});
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 获取状态栏高度
|
||
* @returns {Promise<number>} 状态栏高度
|
||
*/
|
||
async getStatusBarHeight() {
|
||
try {
|
||
const systemInfo = await this.getSystemInfo();
|
||
return systemInfo.statusBarHeight || 0;
|
||
} catch (error) {
|
||
console.error('获取状态栏高度失败:', error);
|
||
return 0;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 判断是否为iOS设备
|
||
* @returns {Promise<boolean>} 是否为iOS设备
|
||
*/
|
||
async isIOS() {
|
||
try {
|
||
const systemInfo = await this.getSystemInfo();
|
||
return systemInfo.platform === 'ios';
|
||
} catch (error) {
|
||
console.error('获取设备平台失败:', error);
|
||
return false;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 判断是否为Android设备
|
||
* @returns {Promise<boolean>} 是否为Android设备
|
||
*/
|
||
async isAndroid() {
|
||
try {
|
||
const systemInfo = await this.getSystemInfo();
|
||
return systemInfo.platform === 'android';
|
||
} catch (error) {
|
||
console.error('获取设备平台失败:', error);
|
||
return false;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 获取屏幕宽度
|
||
* @returns {Promise<number>} 屏幕宽度(px)
|
||
*/
|
||
async getScreenWidth() {
|
||
try {
|
||
const systemInfo = await this.getSystemInfo();
|
||
return systemInfo.screenWidth || 0;
|
||
} catch (error) {
|
||
console.error('获取屏幕宽度失败:', error);
|
||
return 0;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 获取屏幕高度
|
||
* @returns {Promise<number>} 屏幕高度(px)
|
||
*/
|
||
async getScreenHeight() {
|
||
try {
|
||
const systemInfo = await this.getSystemInfo();
|
||
return systemInfo.screenHeight || 0;
|
||
} catch (error) {
|
||
console.error('获取屏幕高度失败:', error);
|
||
return 0;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 获取窗口宽度
|
||
* @returns {Promise<number>} 窗口宽度(px)
|
||
*/
|
||
async getWindowWidth() {
|
||
try {
|
||
const systemInfo = await this.getSystemInfo();
|
||
return systemInfo.windowWidth || 0;
|
||
} catch (error) {
|
||
console.error('获取窗口宽度失败:', error);
|
||
return 0;
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 获取窗口高度
|
||
* @returns {Promise<number>} 窗口高度(px)
|
||
*/
|
||
async getWindowHeight() {
|
||
try {
|
||
const systemInfo = await this.getSystemInfo();
|
||
return systemInfo.windowHeight || 0;
|
||
} catch (error) {
|
||
console.error('获取窗口高度失败:', error);
|
||
return 0;
|
||
}
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 网络请求相关方法
|
||
*/
|
||
export const networkUtil = {
|
||
/**
|
||
* 检查网络状态
|
||
* @returns {Promise<Object>} 网络状态对象
|
||
*/
|
||
checkNetwork() {
|
||
return new Promise((resolve, reject) => {
|
||
uni.getNetworkType({
|
||
success: resolve,
|
||
fail: reject
|
||
});
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 判断是否有网络连接
|
||
* @returns {Promise<boolean>} 是否有网络连接
|
||
*/
|
||
async isConnected() {
|
||
try {
|
||
const networkType = await this.checkNetwork();
|
||
return networkType.networkType !== 'none';
|
||
} catch (error) {
|
||
console.error('检查网络状态失败:', error);
|
||
return false;
|
||
}
|
||
}
|
||
};
|
||
|
||
/**
|
||
* UI相关方法
|
||
*/
|
||
export const uiUtil = {
|
||
/**
|
||
* 显示加载提示
|
||
* @param {string} title - 提示文本
|
||
* @param {boolean} mask - 是否显示遮罩
|
||
* @returns {void}
|
||
*/
|
||
showLoading(title = '加载中', mask = true) {
|
||
uni.showLoading({
|
||
title,
|
||
mask
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 隐藏加载提示
|
||
* @returns {void}
|
||
*/
|
||
hideLoading() {
|
||
uni.hideLoading();
|
||
},
|
||
|
||
/**
|
||
* 显示成功提示
|
||
* @param {string} title - 提示文本
|
||
* @param {number} duration - 显示时长
|
||
* @returns {void}
|
||
*/
|
||
showSuccess(title, duration = 1500) {
|
||
uni.showToast({
|
||
title,
|
||
icon: 'success',
|
||
duration
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 显示错误提示
|
||
* @param {string} title - 提示文本
|
||
* @param {number} duration - 显示时长
|
||
* @returns {void}
|
||
*/
|
||
showError(title, duration = 1500) {
|
||
uni.showToast({
|
||
title,
|
||
icon: 'none',
|
||
duration
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 显示警告提示
|
||
* @param {string} title - 提示文本
|
||
* @param {string} content - 提示内容
|
||
* @param {Function} confirm - 确认回调
|
||
* @param {Function} cancel - 取消回调
|
||
* @returns {void}
|
||
*/
|
||
showConfirm(title, content, confirm, cancel) {
|
||
uni.showModal({
|
||
title,
|
||
content,
|
||
success(res) {
|
||
if (res.confirm) {
|
||
confirm && confirm();
|
||
} else if (res.cancel) {
|
||
cancel && cancel();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 其他工具方法
|
||
*/
|
||
export const util = {
|
||
/**
|
||
* 深拷贝对象
|
||
* @param {Object} obj - 原始对象
|
||
* @returns {Object} 拷贝后的对象
|
||
*/
|
||
deepClone(obj) {
|
||
if (obj === null || typeof obj !== 'object') return obj;
|
||
|
||
if (obj instanceof Date) return new Date(obj.getTime());
|
||
if (obj instanceof Array) return obj.map(item => this.deepClone(item));
|
||
|
||
const clonedObj = {};
|
||
for (const key in obj) {
|
||
if (obj.hasOwnProperty(key)) {
|
||
clonedObj[key] = this.deepClone(obj[key]);
|
||
}
|
||
}
|
||
|
||
return clonedObj;
|
||
},
|
||
|
||
/**
|
||
* 防抖函数
|
||
* @param {Function} func - 要执行的函数
|
||
* @param {number} wait - 等待时间
|
||
* @returns {Function} 防抖后的函数
|
||
*/
|
||
debounce(func, wait = 300) {
|
||
let timeout;
|
||
return function(...args) {
|
||
clearTimeout(timeout);
|
||
timeout = setTimeout(() => func.apply(this, args), wait);
|
||
};
|
||
},
|
||
|
||
/**
|
||
* 节流函数
|
||
* @param {Function} func - 要执行的函数
|
||
* @param {number} limit - 限制时间
|
||
* @returns {Function} 节流后的函数
|
||
*/
|
||
throttle(func, limit = 300) {
|
||
let inThrottle;
|
||
return function(...args) {
|
||
if (!inThrottle) {
|
||
func.apply(this, args);
|
||
inThrottle = true;
|
||
setTimeout(() => inThrottle = false, limit);
|
||
}
|
||
};
|
||
},
|
||
|
||
/**
|
||
* 延迟执行
|
||
* @param {number} ms - 延迟时间(毫秒)
|
||
* @returns {Promise} Promise对象
|
||
*/
|
||
delay(ms) {
|
||
return new Promise(resolve => setTimeout(resolve, ms));
|
||
}
|
||
};
|
||
|
||
// 默认导出所有工具方法
|
||
export default {
|
||
...dateUtil,
|
||
...numberUtil,
|
||
...stringUtil,
|
||
...deviceUtil,
|
||
...networkUtil,
|
||
...uiUtil,
|
||
...util
|
||
};
|