232 行
6.2 KiB
Plaintext
232 行
6.2 KiB
Plaintext
<script setup lang="uts">
|
||
import { state, updateGlobalData, setLifeCycleNum, checkSystemTheme } from '@/store/index.uts'
|
||
|
||
// #ifdef APP-ANDROID || APP-HARMONY
|
||
let firstBackTime = 0
|
||
// #endif
|
||
|
||
onLaunch((res : OnLaunchOptions) => {
|
||
updateGlobalData('launchOptions', res)
|
||
|
||
// 自动化测试
|
||
setLifeCycleNum(state.lifeCycleNum + 1000)
|
||
console.log('App Launch')
|
||
|
||
// 页面性能分析
|
||
// const performance = uni.getPerformance()
|
||
// const observer1: PerformanceObserver = performance.createObserver(
|
||
// (entryList: PerformanceObserverEntryList) => {
|
||
// console.log('observer1:entryList.getEntries()' +JSON.stringify(entryList.getEntries()))
|
||
// }
|
||
// )
|
||
// observer1.observe({
|
||
// entryTypes: ['render', 'navigation'],
|
||
// } as PerformanceObserverOptions)
|
||
// 统计上报 - 应用启动
|
||
// #ifdef APP-ANDROID || WEB || MP-WEIXIN || APP-HARMONY
|
||
uni.report({
|
||
name: 'uni-app-launch',
|
||
options: res,
|
||
success(res_data) {
|
||
console.log(res_data);
|
||
}, fail(err) {
|
||
console.log(err);
|
||
}
|
||
})
|
||
// #endif
|
||
// #ifdef APP
|
||
if (process.env.NODE_ENV !== 'development') { //真机运行可以注释此条件
|
||
uni.getPrivacySetting({
|
||
success(res1){
|
||
if(res1.needAuthorization){
|
||
uni.openDialogPage({ url: '/pages/component/button/privacy' })
|
||
}
|
||
}
|
||
})
|
||
}
|
||
// #endif
|
||
// 获取系统主题
|
||
checkSystemTheme();
|
||
})
|
||
|
||
const getRedirectUrl = (scheme : string | null, ulink : string | null) : string | null => {
|
||
//解析scheme或universal link启动直达页面:
|
||
//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方法)
|
||
let url : string | null = null;
|
||
if (null != scheme && scheme.length > 0) {
|
||
const PATHPRE = 'redirect';
|
||
let parts : string | null = null;
|
||
let pos = scheme.search('//');
|
||
if (pos > 0) {
|
||
parts = scheme.substring(pos + 2);
|
||
}
|
||
if (null != parts && parts.startsWith(PATHPRE)) {
|
||
url = parts.substring(PATHPRE.length);
|
||
}
|
||
} else if (null != ulink && ulink.length > 0) {
|
||
const PATH = 'ulink/redirect.html';
|
||
let parts = ulink.split('?');
|
||
if (parts.length > 1 && parts[0].endsWith(PATH) && parts[1].length > 0) {
|
||
parts[1].split('&').forEach((e) => {
|
||
let params = e.split('=');
|
||
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;
|
||
}
|
||
|
||
onAppShow((res : OnShowOptions) => {
|
||
updateGlobalData('showOptions', res)
|
||
|
||
// 处理scheme或通用链接直达
|
||
let url = getRedirectUrl(res.appScheme, res.appLink);
|
||
if (null != url) {
|
||
uni.navigateTo({
|
||
url: url
|
||
})
|
||
}
|
||
|
||
// 自动化测试
|
||
setLifeCycleNum(state.lifeCycleNum + 100)
|
||
console.log('App Show')
|
||
|
||
// #ifdef APP-ANDROID || WEB || MP-WEIXIN || APP-HARMONY
|
||
// 统计上报 - 应用显示
|
||
uni.report({
|
||
name: 'uni-app-show',
|
||
success(res_data) {
|
||
console.log(res_data);
|
||
}, fail(err) {
|
||
console.log(err);
|
||
}
|
||
})
|
||
// #endif
|
||
})
|
||
onAppHide(() => {
|
||
// 自动化测试
|
||
setLifeCycleNum(state.lifeCycleNum - 100)
|
||
console.log('App Hide')
|
||
|
||
// 统计上报 - 应用进入后台
|
||
// #ifdef APP-ANDROID || WEB || MP-WEIXIN || APP-HARMONY
|
||
uni.report({
|
||
name: 'uni-app-hide',
|
||
success(res) {
|
||
console.log(res);
|
||
}, fail(err) {
|
||
console.log(err);
|
||
}
|
||
})
|
||
// #endif
|
||
})
|
||
// #ifdef APP-ANDROID || APP-HARMONY
|
||
onLastPageBackPress(() => {
|
||
// 自动化测试
|
||
setLifeCycleNum(state.lifeCycleNum - 1000)
|
||
console.log('App LastPageBackPress')
|
||
if (firstBackTime == 0) {
|
||
uni.showToast({
|
||
title: '再按一次退出应用',
|
||
position: 'bottom',
|
||
})
|
||
firstBackTime = Date.now()
|
||
setTimeout(() => {
|
||
firstBackTime = 0
|
||
}, 2000)
|
||
} else if (Date.now() - firstBackTime < 2000) {
|
||
firstBackTime = Date.now()
|
||
uni.exit()
|
||
}
|
||
})
|
||
onExit(() => {
|
||
console.log('App Exit')
|
||
})
|
||
// #endif
|
||
onError((err : any) => {
|
||
console.log('App Error', err)
|
||
setLifeCycleNum(state.lifeCycleNum + 100)
|
||
// 统计上报 - 应用发生错误
|
||
// #ifdef APP-ANDROID || WEB || MP-WEIXIN || APP-HARMONY
|
||
uni.report({
|
||
name: 'uni-app-error',
|
||
options: err,
|
||
success(res) {
|
||
console.log(res);
|
||
}, fail(err) {
|
||
console.log(err);
|
||
}
|
||
})
|
||
// #endif
|
||
})
|
||
const increaseLifeCycleNum = () => {
|
||
setLifeCycleNum(state.lifeCycleNum + 100)
|
||
console.log('App increaseLifeCycleNum')
|
||
}
|
||
|
||
defineExpose({
|
||
increaseLifeCycleNum,
|
||
})
|
||
</script>
|
||
|
||
<style>
|
||
/*每个页面公共css */
|
||
@import "./common/uni.css";
|
||
|
||
/* #ifdef WEB */
|
||
.uni-top-window uni-tabbar .uni-tabbar {
|
||
background-color: #fff !important;
|
||
}
|
||
/* #endif */
|
||
/* #ifdef MP-WEIXIN */
|
||
page {
|
||
background-color: #efeff4;
|
||
}
|
||
/* 适配暗黑模式 */
|
||
@media (prefers-color-scheme: dark) {
|
||
page {
|
||
background-color: #646464;
|
||
}
|
||
}
|
||
/* #endif */
|
||
|
||
/* #ifdef VUE3-VAPOR || WEB || MP-WEIXIN */
|
||
page {
|
||
--page-padding: 16px;
|
||
}
|
||
/* #endif */
|
||
|
||
/* 全局样式测试 - 验证是否会影响所有页面和组件 */
|
||
.global-text {
|
||
color: #007AFF;
|
||
font-size: 18px;
|
||
font-weight: bold;
|
||
}
|
||
|
||
.global-box {
|
||
background-color: #E1F5FF;
|
||
padding: 10px;
|
||
margin: 10px;
|
||
border-radius: 8px;
|
||
border: 2px solid #007AFF;
|
||
}
|
||
|
||
/* 全局样式使用!important覆盖组件样式 */
|
||
.global-important-class {
|
||
background-color: #c6deff !important;
|
||
border-color: #3d7cf5 !important;
|
||
}
|
||
|
||
.global-important-box {
|
||
background-color: #fff6c2 !important;
|
||
border: #fcdf02 solid 3px !important;
|
||
}
|
||
</style>
|