feat: 应用级事件页面

master
luqianyu 2020-12-08 10:03:13 +08:00 committed by XuPeng
parent 997c3dbb7b
commit 6a4ee92619
7 changed files with 104 additions and 2 deletions

7
app.js
View File

@ -32,6 +32,13 @@ App({
}
}
})
wx.onAppShow((res) => {
console.log('onAppShow', res);
})
wx.onAppHide((res) => {
console.log('onAppHide', res);
})
},
globalData: {
userInfo: null

View File

@ -0,0 +1,60 @@
Page({
data: {
type: 'unset',
launchOptions: {},
items: [
'path', 'scene', 'query', 'shareTicket', 'referrerInfo',
],
},
created() {
const that = this;
wx.onAppShow((res) => {
console.log('wx.onAppShow', res);
that.setData({
launchOptions: res,
type: 'onAppShow'
})
})
wx.onAppHide((res) => {
console.log('wx.onAppHide', res);
that.setData({
launchOptions: {},
type: 'onAppHide'
})
})
},
getLaunchOptions() {
const that = this
const result = wx.getLaunchOptionsSync()
console.log('wx.getLaunchOptionsSync', result)
for(const key in result) {
if(typeof result[key] === 'object') {
result[key] = JSON.stringify(result[key])
}
}
that.setData({
launchOptions: result
})
},
getEnterOptionsSync() {
const that = this
const result = wx.getEnterOptionsSync()
console.log('wx.getEnterOptionsSync', result)
for(const key in result) {
if(typeof result[key] === 'object') {
result[key] = JSON.stringify(result[key])
}
}
that.setData({
launchOptions: result
})
},
clear() {
this.setData({
launchOptions: {}
})
}
})

View File

@ -0,0 +1,3 @@
{
"launchOptions": "获取小程序启动时的参数"
}

View File

@ -0,0 +1,22 @@
<import src="../../../common/head.wxml" />
<import src="../../../common/foot.wxml" />
<view class="container">
<template is="head" data="{{title: 'getLaunchOptionsSync'}}"/>
<view>打开这个页面将小程序切换到后台期望wx.onAppHide被正确调用</view>
<view>打开这个页面将小程序切换回前台期望wx.onAppShow被正确调用</view>
<view class="page-body">
<view class="page-section">
<view class="page-body-info">
<view class="page-body-title">{{type}}</view>
<block>
<view wx:for="{{items}}">
<text class="page-body-text">{{item}}: {{launchOptions[item]}}</text>
</view>
</block>
</view>
</view>
</view>
<template is="foot" />
</view>

View File

@ -0,0 +1,7 @@
.page-body-info {
height: 180px;
}
.btn-area {
width: 100%;
}

View File

@ -19,7 +19,10 @@ Page({
zh: '获取小程序启动参数',
url: 'launch-options/launch-options'
},
{
zh: '应用级事件',
url: 'app-level-event/app-level-event'
},
],
},
{

View File

@ -19,7 +19,7 @@
<button type="primary" bindtap="getEnterOptionsSync">小程序本次启动参数</button>
<button bindtap="clear">清空</button>
</view>
</view>
</view>
</view>
<template is="foot" />