Files
ai_dianshang/miniprogram/pages/error-log/index.js
2025-11-17 14:11:46 +08:00

190 lines
5.0 KiB
JavaScript

// pages/error-log/index.js
Page({
data: {
errorLogs: [],
showDetail: false,
currentError: null
},
onLoad() {
this.loadErrorLogs();
},
// 加载错误日志
loadErrorLogs() {
try {
const logs = wx.getStorageSync('error_logs') || [];
// 如果没有日志,添加一些示例数据
if (logs.length === 0) {
const sampleLogs = [
{
id: Date.now() + 1,
timestamp: Date.now() - 3600000, // 1小时前
type: '网络错误',
level: 'error',
message: 'request:fail url not in domain list',
detail: {
url: 'https://tral.cc/api/v1/users/login',
method: 'POST',
errMsg: 'request:fail url not in domain list',
statusCode: null,
stack: 'Error: request:fail url not in domain list\n at wx.request (native)\n at login (pages/login/index.js:45:5)\n at onTapLogin (pages/login/index.js:25:3)'
}
},
{
id: Date.now() + 2,
timestamp: Date.now() - 1800000, // 30分钟前
type: '登录错误',
level: 'error',
message: '获取微信会话失败: 登录凭证code格式异常',
detail: {
url: 'https://tral.cc/api/v1/users/login',
method: 'POST',
errMsg: '登录凭证code格式异常',
statusCode: 400,
response: {
code: 400,
message: '登录凭证code格式异常',
data: null
},
stack: 'Error: 登录凭证code格式异常\n at handleLoginResponse (utils/api.js:78:9)\n at login (pages/login/index.js:52:7)'
}
},
{
id: Date.now() + 3,
timestamp: Date.now() - 900000, // 15分钟前
type: '网络超时',
level: 'warning',
message: '请求超时,请检查网络连接',
detail: {
url: 'https://tral.cc/api/v1/products',
method: 'GET',
errMsg: 'request:fail timeout',
statusCode: null,
timeout: 5000
}
}
];
wx.setStorageSync('error_logs', sampleLogs);
this.setData({ errorLogs: sampleLogs });
} else {
// 按时间倒序排列
logs.sort((a, b) => b.timestamp - a.timestamp);
this.setData({ errorLogs: logs });
}
} catch (error) {
console.error('加载错误日志失败:', error);
wx.showToast({
title: '加载日志失败',
icon: 'error'
});
}
},
// 显示错误详情
showErrorDetail(e) {
const error = e.currentTarget.dataset.error;
console.log('显示错误详情:', error);
this.setData({
showDetail: true,
currentError: error
});
},
// 关闭错误详情
closeErrorDetail() {
this.setData({
showDetail: false,
currentError: null
});
},
// 复制错误信息
copyErrorInfo(e) {
const text = e.currentTarget.dataset.text;
wx.setClipboardData({
data: text,
success: () => {
wx.showToast({
title: '已复制到剪贴板',
icon: 'success'
});
},
fail: () => {
wx.showToast({
title: '复制失败',
icon: 'error'
});
}
});
},
// 清空错误日志
clearErrorLogs() {
wx.showModal({
title: '确认清空',
content: '确定要清空所有错误日志吗?',
success: (res) => {
if (res.confirm) {
wx.removeStorageSync('error_logs');
this.setData({ errorLogs: [] });
wx.showToast({
title: '已清空日志',
icon: 'success'
});
}
}
});
},
// 导出错误日志
exportErrorLogs() {
const logs = this.data.errorLogs;
if (logs.length === 0) {
wx.showToast({
title: '暂无日志可导出',
icon: 'none'
});
return;
}
const exportData = JSON.stringify(logs, null, 2);
wx.setClipboardData({
data: exportData,
success: () => {
wx.showToast({
title: '日志已复制到剪贴板',
icon: 'success'
});
},
fail: () => {
wx.showToast({
title: '导出失败',
icon: 'error'
});
}
});
},
// 格式化时间
formatTime(timestamp) {
const date = new Date(timestamp);
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 `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
},
// 下拉刷新
onPullDownRefresh() {
this.loadErrorLogs();
wx.stopPullDownRefresh();
}
});