You've already forked template-MP
Compare commits
3 Commits
1ca6e6c77a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d9df71f28 | ||
|
|
22b1fcdca2 | ||
|
|
cd2b838647 |
@@ -1,5 +1,6 @@
|
|||||||
import Request from '@/lib/luch-request/index.js'
|
import Request from '@/lib/luch-request/index.js'
|
||||||
import tool from '@/common/utils/tool.js'
|
import tool from '@/common/utils/tool.js'
|
||||||
|
import { reportError } from 'uniapp-error-monitor'
|
||||||
|
|
||||||
const baseUrl = import.meta.env.VITE_BASE_URL
|
const baseUrl = import.meta.env.VITE_BASE_URL
|
||||||
const http = new Request()
|
const http = new Request()
|
||||||
@@ -32,21 +33,7 @@ http.interceptors.response.use(response => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (response.data.code !== 1 || response.data.code !== 200) {
|
if (response.data.code !== 1 || response.data.code !== 200) {
|
||||||
tool.reportError(
|
reportError('api', response)
|
||||||
'api',
|
|
||||||
{
|
|
||||||
url: response.config.url,
|
|
||||||
method: response.config.method,
|
|
||||||
statusCode: response.data.code || response.statusCode,
|
|
||||||
statusText: response.data.msg || response.data.message || '未知错误',
|
|
||||||
responseTime: Date.now() - (response.config.startTime || Date.now()),
|
|
||||||
requestData: response.config.data,
|
|
||||||
requestHeaders: response.config.header,
|
|
||||||
environment: import.meta.env.MODE,
|
|
||||||
},
|
|
||||||
{},
|
|
||||||
true
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
|
|||||||
@@ -1,113 +1,5 @@
|
|||||||
// 环境变量处理
|
const baseUrl = import.meta.env.VITE_BASE_URL
|
||||||
const getEnvVar = (key, defaultValue = '') => {
|
const assetsUrl = import.meta.env.VITE_ASSETSURL
|
||||||
if (typeof import !== 'undefined' && import.meta && import.meta.env) {
|
|
||||||
return import.meta.env[key] || defaultValue
|
|
||||||
}
|
|
||||||
if (typeof process !== 'undefined' && process.env) {
|
|
||||||
return process.env[key] || defaultValue
|
|
||||||
}
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
const baseUrl = getEnvVar('VITE_BASE_URL', 'https://api.example.com')
|
|
||||||
const assetsUrl = getEnvVar('VITE_ASSETSURL', '/assets/')
|
|
||||||
|
|
||||||
// 错误监控实例
|
|
||||||
let errorMonitor = null
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化错误监控实例
|
|
||||||
*/
|
|
||||||
function initErrorMonitorInstance() {
|
|
||||||
if (errorMonitor) return errorMonitor
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 在 Node.js 环境下不尝试导入 uniapp-error-monitor
|
|
||||||
if (typeof window === 'undefined' && typeof uni === 'undefined') {
|
|
||||||
throw new Error('非浏览器/UniApp 环境,跳过 uniapp-error-monitor 导入')
|
|
||||||
}
|
|
||||||
|
|
||||||
// 尝试导入 uniapp-error-monitor
|
|
||||||
const ErrorMonitorModule = require('uniapp-error-monitor')
|
|
||||||
if (ErrorMonitorModule.default) {
|
|
||||||
errorMonitor = new ErrorMonitorModule.default()
|
|
||||||
} else if (ErrorMonitorModule.ErrorMonitor) {
|
|
||||||
errorMonitor = new ErrorMonitorModule.ErrorMonitor()
|
|
||||||
} else {
|
|
||||||
throw new Error('未找到可用的 ErrorMonitor 类')
|
|
||||||
}
|
|
||||||
console.log('[Tool] 使用 uniapp-error-monitor')
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('[Tool] uniapp-error-monitor 导入失败,将使用本地错误监控实现:', error.message)
|
|
||||||
|
|
||||||
// 本地错误监控实现
|
|
||||||
errorMonitor = {
|
|
||||||
errorStats: {
|
|
||||||
total: 0,
|
|
||||||
global: 0,
|
|
||||||
promise: 0,
|
|
||||||
console: 0,
|
|
||||||
miniProgram: 0,
|
|
||||||
api: 0,
|
|
||||||
network: 0,
|
|
||||||
manual: 0,
|
|
||||||
lastErrorTime: null,
|
|
||||||
},
|
|
||||||
|
|
||||||
init(config = {}) {
|
|
||||||
console.log('[Tool] 使用本地错误监控实现')
|
|
||||||
// 这里可以添加本地错误监控逻辑
|
|
||||||
},
|
|
||||||
|
|
||||||
reportError(type = 'manual', error, context = {}, forceSend = false) {
|
|
||||||
this.errorStats.total++
|
|
||||||
this.errorStats[type] = (this.errorStats[type] || 0) + 1
|
|
||||||
this.errorStats.lastErrorTime = Date.now()
|
|
||||||
console.log(`[Tool] 错误上报: ${type}`, error)
|
|
||||||
},
|
|
||||||
|
|
||||||
wrapPromise(promise) {
|
|
||||||
return promise.catch(error => {
|
|
||||||
this.reportError('promise', error)
|
|
||||||
throw error
|
|
||||||
})
|
|
||||||
},
|
|
||||||
|
|
||||||
getErrorStats() {
|
|
||||||
return { ...this.errorStats }
|
|
||||||
},
|
|
||||||
|
|
||||||
resetErrorStats() {
|
|
||||||
this.errorStats = {
|
|
||||||
total: 0,
|
|
||||||
global: 0,
|
|
||||||
promise: 0,
|
|
||||||
console: 0,
|
|
||||||
miniProgram: 0,
|
|
||||||
api: 0,
|
|
||||||
network: 0,
|
|
||||||
manual: 0,
|
|
||||||
lastErrorTime: null,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
getEnvironmentInfo() {
|
|
||||||
return {
|
|
||||||
isProduction: getEnvVar('MODE') === 'production',
|
|
||||||
mode: getEnvVar('MODE', 'development'),
|
|
||||||
platform: 'Unknown',
|
|
||||||
errorMonitorEnabled: true,
|
|
||||||
timestamp: Date.now(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return errorMonitor
|
|
||||||
}
|
|
||||||
|
|
||||||
// 初始化错误监控实例
|
|
||||||
const monitorInstance = initErrorMonitorInstance()
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 工具类 - 提供常用的工具方法
|
* 工具类 - 提供常用的工具方法
|
||||||
@@ -120,15 +12,10 @@ class Tool {
|
|||||||
NONE: 0,
|
NONE: 0,
|
||||||
SUCCESS: 1,
|
SUCCESS: 1,
|
||||||
LOADING: 2,
|
LOADING: 2,
|
||||||
ERROR: 3,
|
|
||||||
WARNING: 4,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 字体加载状态缓存
|
// 字体加载状态缓存
|
||||||
this.loadedFonts = new Set()
|
this.loadedFonts = new Set()
|
||||||
|
|
||||||
// 错误监控配置
|
|
||||||
this.config = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -148,8 +35,6 @@ class Tool {
|
|||||||
[this.ICON_TYPES.NONE]: 'none',
|
[this.ICON_TYPES.NONE]: 'none',
|
||||||
[this.ICON_TYPES.SUCCESS]: 'success',
|
[this.ICON_TYPES.SUCCESS]: 'success',
|
||||||
[this.ICON_TYPES.LOADING]: 'loading',
|
[this.ICON_TYPES.LOADING]: 'loading',
|
||||||
[this.ICON_TYPES.ERROR]: 'error',
|
|
||||||
[this.ICON_TYPES.WARNING]: 'none', // uni-app toast不支持warning图标
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
@@ -453,58 +338,16 @@ class Tool {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 处理支付流程的完整方法
|
|
||||||
* @param {Object} paymentData 支付参数
|
|
||||||
* @param {Function} successCallback 成功回调
|
|
||||||
* @param {Function} errorCallback 错误回调
|
|
||||||
* @returns {Promise<Object>} 支付结果
|
|
||||||
*/
|
|
||||||
async handlePayment(paymentData, successCallback = null, errorCallback = null) {
|
|
||||||
try {
|
|
||||||
// 调用微信支付
|
|
||||||
const res = await this.requestPayment(paymentData)
|
|
||||||
this.alert('支付完成,正在验证结果...', this.ICON_TYPES.LOADING)
|
|
||||||
|
|
||||||
// 如果有成功回调,调用它
|
|
||||||
if (successCallback && typeof successCallback === 'function') {
|
|
||||||
await successCallback(res)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
} catch (error) {
|
|
||||||
console.log('支付失败:', error)
|
|
||||||
|
|
||||||
// 错误处理
|
|
||||||
if (error.errMsg && error.errMsg.includes('cancel')) {
|
|
||||||
await this.alert('支付已取消', this.ICON_TYPES.WARNING)
|
|
||||||
} else {
|
|
||||||
await this.alert('支付失败,请重试', this.ICON_TYPES.ERROR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果有错误回调,调用它
|
|
||||||
if (errorCallback && typeof errorCallback === 'function') {
|
|
||||||
await errorCallback(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
throw error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
upload(filePath) {
|
upload(filePath) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.uploadFile({
|
uni.uploadFile({
|
||||||
url: `${baseUrl}/j1732/api/front/upload/image`,
|
url: `${baseUrl}file/upload`,
|
||||||
fileType: 'image',
|
fileType: 'image',
|
||||||
header: {
|
header: {
|
||||||
'Authori-zation': `${this.storage('token')}`,
|
Authorization: `Bearer ${this.storage('token')}`,
|
||||||
},
|
},
|
||||||
filePath,
|
filePath,
|
||||||
name: 'multipart',
|
name: 'file',
|
||||||
formData: {
|
|
||||||
model: 'user', // 用户模块
|
|
||||||
pid: 7, // 前台用户分类ID
|
|
||||||
},
|
|
||||||
success: ({ data }) => {
|
success: ({ data }) => {
|
||||||
resolve(JSON.parse(data))
|
resolve(JSON.parse(data))
|
||||||
},
|
},
|
||||||
@@ -514,103 +357,6 @@ class Tool {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 初始化全局错误监控
|
|
||||||
* @param {Object} options 配置选项
|
|
||||||
* @param {boolean} [options.enableGlobalError=true] 是否启用全局错误捕获
|
|
||||||
* @param {boolean} [options.enablePromiseError=true] 是否启用Promise错误捕获
|
|
||||||
* @param {boolean} [options.enableConsoleError=true] 是否启用console.error捕获
|
|
||||||
* @param {string} [options.webhookUrl] 自定义webhook地址,不传则使用环境变量
|
|
||||||
* @param {number} [options.maxRetries=3] 发送失败时最大重试次数
|
|
||||||
* @param {number} [options.retryDelay=1000] 重试延迟时间(毫秒)
|
|
||||||
* @param {boolean} [options.forceEnable=false] 强制启用错误监控(忽略环境检查)
|
|
||||||
*/
|
|
||||||
initErrorMonitor(options = {}) {
|
|
||||||
const config = {
|
|
||||||
enableGlobalError: true,
|
|
||||||
enablePromiseError: true,
|
|
||||||
enableConsoleError: false,
|
|
||||||
webhookUrl: import.meta.env.VITE_WEBHOOK,
|
|
||||||
maxRetries: 3,
|
|
||||||
retryDelay: 1000,
|
|
||||||
forceEnable: false,
|
|
||||||
...options,
|
|
||||||
}
|
|
||||||
|
|
||||||
this.config = config
|
|
||||||
|
|
||||||
// 使用 uniapp-error-monitor 或本地实现初始化错误监控
|
|
||||||
monitorInstance.init(config)
|
|
||||||
|
|
||||||
console.log('错误监控已初始化(基于 uniapp-error-monitor)')
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 手动上报错误
|
|
||||||
* @param {string} type 错误类型 ('manual', 'api', 'network', 'global', 'promise', 'console', 'miniProgram')
|
|
||||||
* @param {Error|Object} error 错误对象或错误信息
|
|
||||||
* @param {Object} [context] 错误上下文信息
|
|
||||||
* @param {boolean} [forceSend=false] 强制发送(忽略环境检查)
|
|
||||||
*/
|
|
||||||
reportError(type = 'manual', error, context = {}, forceSend = false) {
|
|
||||||
monitorInstance.reportError(type, error, context, forceSend)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 包装Promise,自动捕获Promise错误
|
|
||||||
* @param {Promise} promise 要包装的Promise
|
|
||||||
* @returns {Promise} 包装后的Promise
|
|
||||||
*/
|
|
||||||
wrapPromise(promise) {
|
|
||||||
return monitorInstance.wrapPromise(promise)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取错误统计信息
|
|
||||||
* @returns {Object} 错误统计信息
|
|
||||||
*/
|
|
||||||
getErrorStats() {
|
|
||||||
return monitorInstance.getErrorStats()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重置错误统计
|
|
||||||
*/
|
|
||||||
resetErrorStats() {
|
|
||||||
monitorInstance.resetErrorStats()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取当前环境信息
|
|
||||||
* @returns {Object} 环境信息
|
|
||||||
*/
|
|
||||||
getEnvironmentInfo() {
|
|
||||||
return monitorInstance.getEnvironmentInfo()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 确认对话框
|
|
||||||
* @param {string} content 确认内容
|
|
||||||
* @param {string} [title='提示'] 标题
|
|
||||||
* @returns {Promise<boolean>} 用户选择结果,true为确认,false为取消
|
|
||||||
*/
|
|
||||||
confirm(content, title = '提示') {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
uni.showModal({
|
|
||||||
title,
|
|
||||||
content,
|
|
||||||
confirmText: '确认',
|
|
||||||
cancelText: '取消',
|
|
||||||
success: res => {
|
|
||||||
resolve(res.confirm)
|
|
||||||
},
|
|
||||||
fail: () => {
|
|
||||||
resolve(false)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建单例并导出
|
// 创建单例并导出
|
||||||
|
|||||||
17
main.js
17
main.js
@@ -2,9 +2,9 @@ import App from './App'
|
|||||||
import uviewPlus from '/uview-plus'
|
import uviewPlus from '/uview-plus'
|
||||||
import globalMixin from './mixins/global'
|
import globalMixin from './mixins/global'
|
||||||
import store from './store'
|
import store from './store'
|
||||||
import tool from './common/utils/tool'
|
|
||||||
import { createSSRApp } from 'vue'
|
import { createSSRApp } from 'vue'
|
||||||
import './uni.promisify.adaptor'
|
import './uni.promisify.adaptor'
|
||||||
|
import { initErrorMonitor } from 'uniapp-error-monitor'
|
||||||
|
|
||||||
uni.$zp = {
|
uni.$zp = {
|
||||||
config: {
|
config: {
|
||||||
@@ -19,14 +19,13 @@ export function createApp() {
|
|||||||
app.use(globalMixin)
|
app.use(globalMixin)
|
||||||
app.use(store)
|
app.use(store)
|
||||||
|
|
||||||
// 初始化全局错误监控
|
// 初始化错误监控
|
||||||
// 注意:只有在生产环境下才会启用错误监控和WebHook上报
|
initErrorMonitor({
|
||||||
// 开发环境和体验版会自动禁用错误监控
|
webhookUrl: import.meta.env.VITE_WEBHOOK, // 必填
|
||||||
tool.initErrorMonitor({
|
enableGlobalError: true, // 启用全局错误捕获
|
||||||
enableGlobalError: true,
|
enablePromiseError: true, // 启用 Promise 错误捕获
|
||||||
enablePromiseError: true,
|
enableConsoleError: false, // 禁用 console.error 捕获
|
||||||
enableConsoleError: false, // 可选开启console错误监控
|
// forceEnable: true,
|
||||||
// forceEnable: true, // 如需强制启用,可取消注释此行
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return { app }
|
return { app }
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dayjs": "*",
|
"dayjs": "*",
|
||||||
"dotenv": "^17.2.2",
|
"dotenv": "^17.2.2",
|
||||||
"uniapp-error-monitor": "^1.0.0",
|
"uniapp-error-monitor": "*",
|
||||||
"vue": "^3.5.21"
|
"vue": "^3.5.21"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user