微信小程序JS监控
参数备注
{
"entryName": "", //项目名称
"name": "", //错误名称
"stacktrace": " ", //错误栈
"time": , //发生错误时的客户端时间戳
"client": { //客户端信息
brand: '', //设备品牌
model: '', //设备型号。
language: '', //微信设置的语言
version: '', //微信版本号
system: '', //操作系统及版本
platform: '', //客户端平台
SDKVersion: '' //客户端基础库版本
},
"url": " ", //页面地址
"apiUrl": "", //接口请求地址
"apiData": "", //接口请求参数
"apiMsg": "" //接口返回错误提示
}
debug.js
import {
http
} from './http.js'
import {
api
} from './api.js'
function HookParams(_appParams, eventName, eventFn) {
if (_appParams[eventName]) {
let _eventFn = _appParams[eventName]
_appParams[eventName] = function(error) {
eventFn.call(this, error, eventName)
return _eventFn.call.apply(_eventFn, [this].concat(Array.prototype.slice.call(arguments)))
}
} else {
_appParams[eventName] = function(error) {
eventFn.call(this, error, eventName)
}
}
}
function objToParam(options = {}) {
let params = ''
for (let key in options) {
params += '&' + key + '=' + options[key]
}
return params.substring(1)
}
function onErrorFn(error, eventName) {
_logReport(error)
}
// 将App暂存起来
let _App = App
App = function(_appParams) {
HookParams(_appParams, 'onError', onErrorFn)
_App(_appParams)
}
//config
let debugConfig = {
entryName: 'entryName',
client: {}
}
//获取设备信息
wx.getSystemInfo({
success: function(res) {
let obj = {
brand: res.brand, //设备品牌
model: res.model, //设备型号。
language: res.language, //微信设置的语言
version: res.version, //微信版本号
system: res.system, //操作系统及版本
platform: res.platform, //客户端平台
SDKVersion: res.SDKVersion //客户端基础库版本
}
debugConfig.client = obj
}
})
//拼装postData
function getPostData(apiUrl = '', apiData = '', apiMsg = '') {
let {
entryName,
client
} = debugConfig,
curPage = getCurrentPages()[getCurrentPages().length - 1],
url = '',
urlParams = '',
name = 'error',
postData = 'postData'
//处理url
if (curPage) {
url = curPage.route
urlParams = objToParam(curPage.options)
if (urlParams) {
url += '?' + urlParams
}
}
name = apiUrl.split('\n')[0] || 'error'
try {
postData = {
data: JSON.stringify({
entryName,
name,
stacktrace: apiUrl,
time: Date.now(),
client,
url,
apiUrl: apiUrl.split(')').length > 1 ? '' : apiUrl,
apiData,
apiMsg
})
}
} catch (e) {
console.error(e)
}
return postData
}
//控制错误的发送
function _logReport(apiUrl, apiData, apiMsg) {
http({
url: api.getUser,
data: getPostData(apiUrl, apiData, apiMsg)
})
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
}
let debug = {
init(option = {}) {
debugConfig = Object.assign({}, debugConfig, option)
},
notifyError(apiUrl, apiData, apiMsg) {
_logReport(apiUrl, apiData, apiMsg)
}
}
module.exports = debug
app.js
import ngmmdebug from './utils/debug.js'
//初始化
ngmmdebug.init({
entryName: 'xxx'
})
// 引入监听
const debugEvent = require('./utils/event.js')
onShow(options) {
// 监听
debugEvent.$on({
name: 'deBug',
tg: this,
success: res => {
console.log('收到消息了-------', res)
// 调用监听
ngmmdebug.notifyError(res.apiUrl, res.apiData, res.apiMsg)
}
})
}
http.js
// 引入
const debugEvent = require('./event.js')
// 在接口报错返回里面发射
debugEvent.$emit({
name: 'deBug',
data: {
apiUrl: params.url,
apiData: params.data,
apiMsg: res || '访问量过大,请稍后再试!'
}
})
评论区