减少App.vue的代码依赖,优化获取顶部自定义导航栏的高度信息

This commit is contained in:
JaylenTech
2021-12-31 09:43:37 +08:00
parent 61b53bdafc
commit e89b56d3ce
3 changed files with 29 additions and 87 deletions

71
App.vue
View File

@@ -1,79 +1,8 @@
<script> <script>
import Vue from 'vue' import Vue from 'vue'
import store from './store/index.js'
import updateCustomBarInfo from './tuniao-ui/libs/function/updateCustomBarInfo.js'
export default { export default {
onLaunch: function() { onLaunch: function() {
uni.getSystemInfo({
success: function(e) {
// #ifndef H5
// 获取手机系统版本
const system = e.system.toLowerCase()
const platform = e.platform.toLowerCase()
// 判断是否为ios设备
if (platform.indexOf('ios') != -1 && (system.indexOf('ios') != -1 || system.indexOf('macos') != -1)) {
Vue.prototype.SystemPlatform = 'apple'
} else if (platform.indexOf('android') != -1 && (system.indexOf('android') != -1)) {
Vue.prototype.SystemPlatform = 'android'
} else {
Vue.prototype.SystemPlatform = 'devtools'
}
// #endif
}
})
// 获取设备的状态栏信息和自定义顶栏信息
// store.dispatch('updateCustomBarInfo')
updateCustomBarInfo().then((res) => {
store.commit('$tStore', {
name: 'vuex_status_bar_height',
value: res.statusBarHeight
})
store.commit('$tStore', {
name: 'vuex_custom_bar_height',
value: res.customBarHeight
})
})
// #ifdef MP-WEIXIN
//更新检测
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
updateManager && updateManager.onCheckForUpdate((res) => {
if (res.hasUpdate) {
updateManager.onUpdateReady(() => {
uni.showModal({
title: '更新提示',
content: '新版本已经准备就绪,是否需要重新启动应用?',
success: (res) => {
if (res.confirm) {
uni.clearStorageSync() // 更新完成后刷新storage的数据
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(() => {
uni.showModal({
title: '已有新版本上线',
content: '小程序自动更新失败,请删除该小程序后重新搜索打开哟~~~',
showCancel: false
})
})
} else {
//没有更新
}
})
} else {
uni.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请更新到最新的微信后再重试。',
showCancel: false
})
}
// #endif
}, },
onShow: function() { onShow: function() {
// console.log('App Show') // console.log('App Show')

View File

@@ -175,16 +175,7 @@
}, },
mounted() { mounted() {
// 获取vuex中的自定义顶栏的高度 // 获取vuex中的自定义顶栏的高度
let customBarHeight = this.vuex_custom_bar_height this.updateNavBarInfo()
let statusBarHeight = this.vuex_status_bar_height
// 如果获取失败则重新获取
if (!customBarHeight) {
this.$t.updateCustomBar()
customBarHeight = this.vuex_custom_bar_height
statusBarHeight = this.vuex_status_bar_height
}
this.customBarHeight = customBarHeight
this.statusBarHeight = statusBarHeight
}, },
created() { created() {
// 获取胶囊信息 // 获取胶囊信息
@@ -196,6 +187,32 @@
// #endif // #endif
}, },
methods: { methods: {
// 更新导航栏的高度
async updateNavBarInfo() {
// 获取vuex中的自定义顶栏的高度
let customBarHeight = this.vuex_custom_bar_height
let statusBarHeight = this.vuex_status_bar_height
// 如果获取失败则重新获取
if (!customBarHeight) {
try {
const navBarInfo = await this.$t.updateCustomBar()
customBarHeight = navBarInfo.customBarHeight
statusBarHeight = navBarInfo.statusBarHeight
} catch(e) {
setTimeout(() => {
this.updateNavBarInfo()
}, 10)
return
}
}
// 更新vuex中的导航栏信息
this && this.$t.vuex('vuex_status_bar_height', statusBarHeight)
this && this.$t.vuex('vuex_custom_bar_height', customBarHeight)
this.customBarHeight = customBarHeight
this.statusBarHeight = statusBarHeight
},
// 处理返回事件 // 处理返回事件
async handlerBack() { async handlerBack() {
if (this.beforeBack && typeof(this.beforeBack) === 'function') { if (this.beforeBack && typeof(this.beforeBack) === 'function') {

View File

@@ -1,8 +1,8 @@
/** /**
* 更新自定义顶部导航栏的高度 * 更新自定义顶部导航栏的高度
*/ */
async function updateCustomBarInfo () { function updateCustomBarInfo () {
return await new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
uni.getSystemInfo({ uni.getSystemInfo({
success: (e) => { success: (e) => {
let statusBarHeight = 0 let statusBarHeight = 0
@@ -27,8 +27,6 @@ async function updateCustomBarInfo () {
statusBarHeight = e.statusBarHeight statusBarHeight = e.statusBarHeight
customBarHeight = e.statusBarHeight + e.titleBarHeight customBarHeight = e.statusBarHeight + e.titleBarHeight
// #endif // #endif
this && this.$t.vuex('vuex_status_bar_height', statusBarHeight)
this && this.$t.vuex('vuex_custom_bar_height', customBarHeight)
resolve({ resolve({
statusBarHeight, statusBarHeight,
customBarHeight customBarHeight
@@ -36,8 +34,6 @@ async function updateCustomBarInfo () {
}, },
fail: (err) => { fail: (err) => {
console.log("获取设备信息失败", err); console.log("获取设备信息失败", err);
this && this.$t.vuex('vuex_status_bar_height', 0)
this && this.$t.vuex('vuex_custom_bar_height', 0)
reject() reject()
} }
}) })