let util = require('./utils/util')

App({
  globalData: {},

  onLaunch: function() {
		this.checkVersion()
	},

  onShow() {
		this.getNavHeight()
	},

	checkVersion() {
		if (!wx.canIUse('getUpdateManager')) return wx.showModal({ title: '提示', content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试' })
		const updateManager = wx.getUpdateManager()
		updateManager.onCheckForUpdate(res => {
			if (res.hasUpdate) {
				wx.showModal({
					title: '更新提示',
					content: '检测到新版本，是否下载新版本并重启小程序',
					success: res => {
						if (res.confirm) {
							util.showLoad('下载中')
							updateManager.onUpdateReady(() => {
								util.hideLoad()
								updateManager.applyUpdate()
							})
              updateManager.onUpdateFailed(() => {
								util.hideLoad()
                wx.showModal({ title: '已有新版本', content: '新版本已经上线了，请删除当前小程序，重新搜索打开' })
              })
						}
					}
				})
			}
		})
	},

  // 获取导航栏高度
  getNavHeight() {
    const menu_btn = wx.getMenuButtonBoundingClientRect()
    wx.getSystemInfo({
      success: res => {
        const statusBar_h = res.statusBarHeight
        const nav_top = menu_btn.top - statusBar_h
        const nav_height = statusBar_h + menu_btn.height + nav_top * 2
        this.globalData.menuBtnHeight = menu_btn.height
        this.globalData.navHeight = nav_height
        this.globalData.navTop = nav_top
        this.globalData.screenHeight = res.screenHeight
      },
      fail: () => setTimeout(() => this.getNavHeight(),1000)
    })
	}
})