<script>
import config from './config'
import store from '@/store'
import { getAccessToken, setOpenId, getOpenId } from '@/utils/auth'

export default {
  data() {
    return {
      invateId: ''
    }
  },
  onLaunch: function (options) {
    if (options.query.invateId) {
      this.invateId = options.query.invateId
    }
    this.initApp()
  },
  methods: {
    // 初始化应用
    async initApp() {
      await uni.hideTabBar()
      uni.hideTabBar({
        animation: false
      })
      // 初始化应用配置
      await this.initConfig()
      // 检查用户登录状态
      this.checkLogin()
    },
    async initConfig() {
      this.globalData.config = config
      uni.requestSubscribeMessage({
        //此处填写刚才申请模板的模板ID
        tmplIds: ['E8RK91cPLMios6ZHoKx6FJOV4H2kodx6yPWYp7jpLJY'],
        success(res) {
          console.log('requestSubscribeMessage', res)
        }
      })
      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.login()
      } else {
        this.loginSuccess()
      }
    },
    login() {
      console.log('-------' + getOpenId())
      if (getOpenId()) {
        const data = {
          type: 34,
          code: getOpenId(),
          state: 'default',
          userType: this.getUserType()
        }
        // 系统登录
        this.$store.dispatch('Login', data).then(() => {
          this.loginSuccess()
        })
      } else {
        uni.login({
          success: res => {
            const data = {
              type: 34,
              code: res.code,
              state: 'default',
              userType: this.getUserType()
            }
            setOpenId(res.code)
            // 系统登录
            this.$store.dispatch('Login', data).then(() => {
              this.loginSuccess()
            })
          }
        })
      }
    },
    getUserType() {
      // 企业邀请
      if (this.invateId) return 2
      // if (this.type == 'internal') return 3
      // return 1
      return 3
    },
    loginSuccess() {
      this.$store.dispatch('GetInfo').then(res => {
        this.handlerNavigateTo(res.data.user)
      })
    },
    handlerNavigateTo(user) {
      // 邀请企业
      if (this.invateId) {
        uni.navigateTo({
          url: `/sub/enterprise/edit?invateId=${this.invateId}`
        })
        return
      }

      // 审核中
      if (user.audit == 1) {
        uni.navigateTo({
          url: '/sub/common/waiting'
        })
        return
      }

      // 局内用户首次登录
      if (user.audit == null) {
        // 局内用户首次登录
        uni.navigateTo({
          url: '/sub/owner/edit'
        })
        return
      }
    }
  }
}
</script>

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