/** * 显示消息提示框 * @param content 提示的标题 */ export function toast(content) { uni.showToast({ icon: 'none', title: content }) } /** * 显示模态弹窗 * @param content 提示的标题 */ export function showConfirm(content) { return new Promise((resolve, reject) => { uni.showModal({ title: '提示', content: content, cancelText: '取消', confirmText: '确定', success: function(res) { resolve(res) } }) }) } /** * 参数处理 * @param params 参数 */ export function tansParams(params) { let result = '' for (const propName of Object.keys(params)) { const value = params[propName] var part = encodeURIComponent(propName) + "=" if (value !== null && value !== "" && typeof (value) !== "undefined") { if (typeof value === 'object') { for (const key of Object.keys(value)) { if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') { let params = propName + '[' + key + ']' var subPart = encodeURIComponent(params) + "=" result += subPart + encodeURIComponent(value[key]) + "&" } } } else { result += part + encodeURIComponent(value) + "&" } } } return result } /** * 格式化日期时间(兼容iOS) * @param dateTime 日期时间字符串或Date对象 * @param format 格式化模板,默认为 'YYYY-MM-DD HH:mm:ss' * @returns {string} 格式化后的日期时间字符串 */ export function formatDateTime(dateTime, format = 'YYYY-MM-DD HH:mm:ss') { if (!dateTime) return '' let date if (typeof dateTime === 'string') { // iOS兼容性处理:将 "yyyy-MM-dd HH:mm:ss" 格式转换为 "yyyy/MM/dd HH:mm:ss" const iosCompatibleDate = dateTime.replace(/-/g, '/') date = new Date(iosCompatibleDate) } else { date = new Date(dateTime) } // 检查日期是否有效 if (isNaN(date.getTime())) { console.warn('Invalid date:', dateTime) return '' } 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') // 根据format模板返回格式化结果 if (format === 'YYYY-MM-DD') { return `${year}-${month}-${day}` } else if (format === 'YYYY-MM-DD HH:mm') { return `${year}-${month}-${day} ${hours}:${minutes}` } else if (format === 'MM-DD HH:mm') { return `${month}-${day} ${hours}:${minutes}` } else { return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` } } /** * 获取系统信息(兼容新旧API) * 替代已废弃的uni.getSystemInfoSync() * @returns {Object} 系统信息对象 */ export function getSystemInfo() { // #ifdef MP-WEIXIN // 微信小程序使用新API try { const windowInfo = uni.getWindowInfo() const deviceInfo = uni.getDeviceInfo() const appBaseInfo = uni.getAppBaseInfo() return { ...windowInfo, ...deviceInfo, ...appBaseInfo, // 兼容旧字段名 windowHeight: windowInfo.windowHeight, windowWidth: windowInfo.windowWidth, screenHeight: windowInfo.screenHeight, screenWidth: windowInfo.screenWidth, statusBarHeight: windowInfo.statusBarHeight, platform: deviceInfo.platform, system: deviceInfo.system, model: deviceInfo.model, brand: deviceInfo.brand, SDKVersion: appBaseInfo.SDKVersion, version: appBaseInfo.version } } catch (e) { // 降级使用旧API console.warn('新API调用失败,降级使用旧API', e) return uni.getSystemInfoSync() } // #endif // #ifndef MP-WEIXIN // 其他平台继续使用旧API return uni.getSystemInfoSync() // #endif }