<script>
import config from './config.js'
import { getAccessToken, setOpenId, getOpenId } from '@/utils/auth'
export default {
  data() {
    return {}
  },
  async onLaunch(options) {
    await this.initApp()
  },
  methods: {
    // 初始化应用
    initApp() {
      uni.hideTabBar()
      // 初始化应用配置
      this.initConfig()
      if (!getOpenId()) {
        this.applogin()
        return
      }
      // 检查用户登录状态
      // this.checkLogin()
    },
    initConfig() {
      this.globalData.config = config
      uni.getSystemInfoAsync({
        success: res => {
          uni.setStorageSync('MOBILE', res.model)
          uni.setStorageSync('SYSTEM', res.system)
          uni.setStorageSync('PIXELRATIO', res.pixelRatio)
        },
        fail: err => {
          console.error('获取设备信息失败：', err)
        }
      })
    },
    checkLogin() {
      if (!getAccessToken()) {
        this.$tab.reLaunch('/pages/login')
      }
    },
    async applogin() {
      uni.login({
        success: res => {
          setOpenId(res.code)
        },
        fail: err => {
          console.log('getOpenIdFail', err)
        }
      })
    }
  }
}
</script>

<style lang="scss">
@import '@/uni_modules/windi-css-uniapp/index.scss';
@import '@/static/scss/index.scss';
</style>
