提交 dbc879d8 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat: app onError 监听

上级 f2027aeb
<script lang="uts"> <script lang="uts">
import { state, setLifeCycleNum } from '@/store/index.uts' import { state, setLifeCycleNum } from '@/store/index.uts'
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
let firstBackTime = 0 let firstBackTime = 0
// #endif // #endif
export default { export default {
globalData: { globalData: {
str: 'default globalData str', str: 'default globalData str',
num: 0, num: 0,
bool: false, bool: false,
obj: { obj: {
str: 'default globalData obj str', str: 'default globalData obj str',
num: 0, num: 0,
bool: false, bool: false,
}, },
null: null as string | null, null: null as string | null,
arr: [] as number[], arr: [] as number[],
mySet: new Set<string>(), mySet: new Set<string>(),
myMap: new Map<string, any>(), myMap: new Map<string, any>(),
func: () : string => { func: () : string => {
return 'globalData func' return 'globalData func'
}, },
launchOptions: { launchOptions: {
path: '', path: '',
} as OnLaunchOptions, } as OnLaunchOptions,
onShowOption: { onShowOption: {
path: '' path: ''
} as OnShowOptions } as OnShowOptions
}, },
onLaunch: function (res : OnLaunchOptions) { onLaunch: function (res : OnLaunchOptions) {
this.globalData.launchOptions = res this.globalData.launchOptions = res
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum + 1000) setLifeCycleNum(state.lifeCycleNum + 1000)
console.log('App Launch') console.log('App Launch')
// 页面性能分析 // 页面性能分析
// const performance = uni.getPerformance() // const performance = uni.getPerformance()
// const observer1: PerformanceObserver = performance.createObserver( // const observer1: PerformanceObserver = performance.createObserver(
// (entryList: PerformanceObserverEntryList) => { // (entryList: PerformanceObserverEntryList) => {
// console.log('observer1:entryList.getEntries()' +JSON.stringify(entryList.getEntries())) // console.log('observer1:entryList.getEntries()' +JSON.stringify(entryList.getEntries()))
// } // }
// ) // )
// observer1.observe({ // observer1.observe({
// entryTypes: ['render', 'navigation'], // entryTypes: ['render', 'navigation'],
// } as PerformanceObserverOptions) // } as PerformanceObserverOptions)
// 统计上报 - 应用启动 // 统计上报 - 应用启动
// uni.report({ // uni.report({
// name: 'uni-app-launch', // name: 'uni-app-launch',
// options: res, // options: res,
// success(res_data) { // success(res_data) {
// console.log(res_data); // console.log(res_data);
// }, fail(err) { // }, fail(err) {
// console.log(err); // console.log(err);
// } // }
// }) // })
// #ifdef APP // #ifdef APP
if (process.env.NODE_ENV !== 'development') { //真机运行可以注释此条件 if (process.env.NODE_ENV !== 'development') { //真机运行可以注释此条件
uni.getPrivacySetting({ uni.getPrivacySetting({
success(res1){ success(res1){
if(res1.needAuthorization){ if(res1.needAuthorization){
uni.openDialogPage({ uni.openDialogPage({
url: '/pages/component/button/privacy', url: '/pages/component/button/privacy',
}) })
} }
} }
}) })
} }
// #endif // #endif
}, },
onShow: function (res : OnShowOptions) { onShow: function (res : OnShowOptions) {
this.globalData.onShowOption = res this.globalData.onShowOption = res
// 处理scheme或通用链接直达 // 处理scheme或通用链接直达
let url = this.getRedirectUrl(res.appScheme, res.appLink); let url = this.getRedirectUrl(res.appScheme, res.appLink);
if (null != url) { if (null != url) {
uni.navigateTo({ uni.navigateTo({
url: url url: url
}) })
} }
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum + 100) setLifeCycleNum(state.lifeCycleNum + 100)
console.log('App Show') console.log('App Show')
// 统计上报 - 应用显示 // 统计上报 - 应用显示
// uni.report({ // uni.report({
// name: 'uni-app-show', // name: 'uni-app-show',
// success(res_data) { // success(res_data) {
// console.log(res_data); // console.log(res_data);
// }, fail(err) { // }, fail(err) {
// console.log(err); // console.log(err);
// } // }
// }) // })
}, },
onHide: function () { onHide: function () {
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum - 100) setLifeCycleNum(state.lifeCycleNum - 100)
console.log('App Hide') console.log('App Hide')
// 统计上报 - 应用进入后台 // 统计上报 - 应用进入后台
// uni.report({ // uni.report({
// name: 'uni-app-hide', // name: 'uni-app-hide',
// success(res) { // success(res) {
// console.log(res); // console.log(res);
// }, fail(err) { // }, fail(err) {
// console.log(err); // console.log(err);
// } // }
// }) // })
}, },
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
onLastPageBackPress: function () { onLastPageBackPress: function () {
// 自动化测试 // 自动化测试
setLifeCycleNum(state.lifeCycleNum - 1000) setLifeCycleNum(state.lifeCycleNum - 1000)
console.log('App LastPageBackPress') console.log('App LastPageBackPress')
if (firstBackTime == 0) { if (firstBackTime == 0) {
uni.showToast({ uni.showToast({
title: '再按一次退出应用', title: '再按一次退出应用',
position: 'bottom', position: 'bottom',
}) })
firstBackTime = Date.now() firstBackTime = Date.now()
setTimeout(() => { setTimeout(() => {
firstBackTime = 0 firstBackTime = 0
}, 2000) }, 2000)
} else if (Date.now() - firstBackTime < 2000) { } else if (Date.now() - firstBackTime < 2000) {
firstBackTime = Date.now() firstBackTime = Date.now()
uni.exit() uni.exit()
} }
}, },
onExit() { onExit() {
console.log('App Exit') console.log('App Exit')
}, },
// onError(err : any) { // #endif
// // 统计上报 - 应用发生错误 onError(err : any) {
// uni.report({ console.log('App onError', err)
// name: 'uni-app-error', // // #ifdef APP-ANDROID || APP-IOS || WEB
// options: err, // // 统计上报 - 应用发生错误
// success(res) { // uni.report({
// console.log(res); // name: 'uni-app-error',
// }, fail(err) { // options: err,
// console.log(err); // success(res) {
// } // console.log(res);
// }) // }, fail(err) {
// }, // console.log(err);
// #endif // }
methods: { // })
increasetLifeCycleNum() { // // #endif
setLifeCycleNum(state.lifeCycleNum + 100) },
console.log('App increasetLifeCycleNum') methods: {
}, increasetLifeCycleNum() {
getRedirectUrl(scheme : string | null, ulink : string | null) : string | null { setLifeCycleNum(state.lifeCycleNum + 100)
//解析scheme或universal link启动直达页面: console.log('App increasetLifeCycleNum')
//scheme格式:uniappx://redirect/pages/component/view/view?key=value //其中redirect后为页面路径 },
//universal link格式:https://uniappx.dcloud.net.cn/ulink/redirect.html?url=%2Fpages%2Fcomponent%2Fview%2Fview%3Fkey%3Dvalue //通用链接路径需固定,?后面的url参数为直达页面路径,注意url字段值需做url编码(可使用encodeURIComponent方法) getRedirectUrl(scheme : string | null, ulink : string | null) : string | null {
let url : string | null = null; //解析scheme或universal link启动直达页面:
if (null != scheme && scheme.length > 0) { //scheme格式:uniappx://redirect/pages/component/view/view?key=value //其中redirect后为页面路径
const PATHPRE = 'redirect'; //universal link格式:https://uniappx.dcloud.net.cn/ulink/redirect.html?url=%2Fpages%2Fcomponent%2Fview%2Fview%3Fkey%3Dvalue //通用链接路径需固定,?后面的url参数为直达页面路径,注意url字段值需做url编码(可使用encodeURIComponent方法)
let parts : string | null = null; let url : string | null = null;
let pos = scheme.search('//'); if (null != scheme && scheme.length > 0) {
if (pos > 0) { const PATHPRE = 'redirect';
parts = scheme.substring(pos + 2); let parts : string | null = null;
} let pos = scheme.search('//');
if (null != parts && parts.startsWith(PATHPRE)) { if (pos > 0) {
url = parts.substring(PATHPRE.length); parts = scheme.substring(pos + 2);
} }
} else if (null != ulink && ulink.length > 0) { if (null != parts && parts.startsWith(PATHPRE)) {
const PATH = 'ulink/redirect.html'; url = parts.substring(PATHPRE.length);
let parts = ulink.split('?'); }
if (parts.length > 1 && parts[0].endsWith(PATH) && parts[1].length > 0) { } else if (null != ulink && ulink.length > 0) {
parts[1].split('&').forEach((e) => { const PATH = 'ulink/redirect.html';
let params = e.split('='); let parts = ulink.split('?');
if (params.length > 1 && params[0].length > 0 && params[1].length > 0) { if (parts.length > 1 && parts[0].endsWith(PATH) && parts[1].length > 0) {
if ('url' == params[0]) { parts[1].split('&').forEach((e) => {
if (null == url) { let params = e.split('=');
url = decodeURIComponent(params[1]); if (params.length > 1 && params[0].length > 0 && params[1].length > 0) {
} if ('url' == params[0]) {
} if (null == url) {
} url = decodeURIComponent(params[1]);
}); }
} }
} }
return url; });
} }
} }
} return url;
</script> }
}
<style> }
/*每个页面公共css */ </script>
@import "./common/uni.css";
<style>
/* #ifdef WEB */ /*每个页面公共css */
.uni-top-window uni-tabbar .uni-tabbar { @import "./common/uni.css";
background-color: #fff !important;
} /* #ifdef WEB */
/* #endif */ .uni-top-window uni-tabbar .uni-tabbar {
</style> background-color: #fff !important;
}
/* #endif */
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册