78 lines
1.5 KiB
JavaScript
78 lines
1.5 KiB
JavaScript
|
|
/**
|
|||
|
|
* 调试工具函数
|
|||
|
|
* 提供安全的日志输出方法,避免真机调试时的文件系统问题
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
// 是否启用调试模式
|
|||
|
|
const DEBUG_MODE = false;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 安全的日志输出
|
|||
|
|
* @param {string} message - 日志消息
|
|||
|
|
* @param {string} type - 日志类型 (log, warn, error)
|
|||
|
|
*/
|
|||
|
|
export function safeLog(message, type = 'log') {
|
|||
|
|
if (!DEBUG_MODE) return;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
switch (type) {
|
|||
|
|
case 'warn':
|
|||
|
|
console.warn(message);
|
|||
|
|
break;
|
|||
|
|
case 'error':
|
|||
|
|
console.error(message);
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
console.log(message);
|
|||
|
|
}
|
|||
|
|
} catch (e) {
|
|||
|
|
// 如果console输出失败,使用uni.showToast
|
|||
|
|
uni.showToast({
|
|||
|
|
title: typeof message === 'string' ? message : '调试信息',
|
|||
|
|
icon: 'none',
|
|||
|
|
duration: 2000
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 安全的错误处理
|
|||
|
|
* @param {Error} error - 错误对象
|
|||
|
|
* @param {string} context - 错误上下文
|
|||
|
|
*/
|
|||
|
|
export function safeError(error, context = '') {
|
|||
|
|
if (!DEBUG_MODE) return;
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
console.error(`[${context}]`, error);
|
|||
|
|
} catch (e) {
|
|||
|
|
uni.showToast({
|
|||
|
|
title: `错误: ${context}`,
|
|||
|
|
icon: 'none'
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 调试信息输出
|
|||
|
|
* @param {string} message - 调试信息
|
|||
|
|
*/
|
|||
|
|
export function debug(message) {
|
|||
|
|
safeLog(message, 'log');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 警告信息输出
|
|||
|
|
* @param {string} message - 警告信息
|
|||
|
|
*/
|
|||
|
|
export function warn(message) {
|
|||
|
|
safeLog(message, 'warn');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 错误信息输出
|
|||
|
|
* @param {string} message - 错误信息
|
|||
|
|
*/
|
|||
|
|
export function error(message) {
|
|||
|
|
safeLog(message, 'error');
|
|||
|
|
}
|