<script>
import { isLoggedIn, startStatusCheck, stopStatusCheck } from './utils/auth.js'

export default {
  globalData: {
    userInfo: null,
	envVersion:''
  },
  onLaunch: function() {
    console.log('App Launch')

    // #ifdef H5
    // 检查URL中是否有微信授权回调的code参数
    const urlParams = new URLSearchParams(window.location.search)
    const code = urlParams.get('code')

    if (code) {
      console.log('[App] 检测到微信授权回调code:', code)
      // H5环境下使用原生跳转
      const loginUrl = `${window.location.origin}${window.location.pathname}#/pages/login/login?code=${code}`
      console.log('[App] 跳转到登录页:', loginUrl)
      window.location.replace(loginUrl)
      return
    }
    // #endif

    // 如果已登录，启动用户状态定时检查
    if (isLoggedIn()) {
      console.log('[App] 用户已登录，启动状态检查')
      startStatusCheck()
    }

    // #ifdef H5
    // H5键盘适配
    let baseLayoutHeight = window.innerHeight || document.documentElement.clientHeight || 0
    const setKeyboardOffset = () => {
      const vv = window.visualViewport
      if (!vv) {
        document.documentElement.style.setProperty('--keyboard-offset', '0px')
        return
      }

      const currentLayoutHeight = window.innerHeight || document.documentElement.clientHeight || 0
      baseLayoutHeight = Math.max(baseLayoutHeight, currentLayoutHeight)

      const vvHeight = vv.height
      const offsetTop = vv.offsetTop || 0
      const keyboardHeight = Math.max(0, baseLayoutHeight - vvHeight - offsetTop)
      document.documentElement.style.setProperty('--keyboard-offset', `${keyboardHeight}px`)
    }

    this.__keyboardOffsetHandler = setKeyboardOffset

    if (window.visualViewport) {
      window.visualViewport.addEventListener('resize', setKeyboardOffset)
      window.visualViewport.addEventListener('scroll', setKeyboardOffset)
    }

    window.addEventListener('resize', () => {
      const h = window.innerHeight || document.documentElement.clientHeight || 0
      baseLayoutHeight = Math.max(baseLayoutHeight, h)
      setKeyboardOffset()
    })

    window.addEventListener('focusin', setKeyboardOffset)
    window.addEventListener('focusout', () => {
      document.documentElement.style.setProperty('--keyboard-offset', '0px')
    })

    setKeyboardOffset()
    // #endif
  },
  onShow: function() {
	  const accountInfo = wx.getAccountInfoSync();
	  //fakecheck
	  // this.globalData.envVersion = accountInfo.miniProgram.envVersion
    console.log('App Show')
    // 确保导航栏标题正确显示
    uni.setNavigationBarTitle({
      title: '量化交易大师'
    })
    // 应用回到前台时，如果已登录则启动状态检查
    if (isLoggedIn()) {
      startStatusCheck()
    }
  },
  onHide: function() {
    console.log('App Hide')
    // 应用进入后台时停止状态检查，节省资源
    stopStatusCheck()
  }
}
</script>

<style>
/*每个页面公共css */
page {
  background-color: #f5f6fb;
}

/* #ifdef H5 */
uni-page-head,
uni-page-head .uni-page-head {
  height: 44px !important;
  min-height: 44px !important;
  padding-top: 0 !important;
}

uni-page-head .uni-page-head__title {
  line-height: 44px !important;
}
/* #endif */

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
}

.ph-bos{
	width: 750rpx;
	height: 100vh;
	position: relative;
}

.ph-bos .ph-image{
	width: 750rpx;
	height: 100vh;
}

.ph-bos .ph-next{
	width: 140rpx;
	position: absolute;
	top: 92%;
	left: 305rpx;
	text-align: center;
	padding: 4rpx 0rpx;
	background-color: #ffffff;
	border-radius: 20rpx;
}
</style>

