<template>
  <view class="container">
    <u--image
      src="/static/favicon.png"
      width="288rpx"
      height="288rpx"
      mode="aspectFit"
      :fade="true"
      duration="450"
    ></u--image>
    <text class="title">智慧生态</text>
  </view>
</template>

<script>
import { getAccessToken, setOpenId, getOpenId } from '@/utils/auth'
export default {
  name: 'Login',
  data() {
    return {
      inviteId: '',
      timer: null
    }
  },
  onLoad(options) {
    if (options.inviteId) {
      this.inviteId = options.inviteId
    }
    this.initApp()
  },
  methods: {
    // 初始化应用
    async initApp() {
      await uni.hideTabBar()
      uni.hideTabBar({
        animation: false
      })
      if (!getOpenId()) {
        // 如果没有openid则获取
        this.getWxOpenId().then(() => {
          this.checkLogin()
        })
      } else {
        // 检查用户登录状态
        this.checkLogin()
      }
    },
    checkLogin() {
      console.log('accessToken', getAccessToken())
      if (!getAccessToken()) {
        // 如果没有token则获取
        this.login()
      } else {
        this.loginSuccess()
      }
    },
    getWxOpenId() {
      return new Promise((resolve, reject) => {
        uni.login({
          success: res => {
            setOpenId(res.code)
            resolve()
          },
          fail: err => {
            reject()
          }
        })
      })
    },
    login() {
      const data = {
        type: 34,
        code: getOpenId(),
        state: 'default',
        userType: this.getUserType()
      }
      this.$store.dispatch('Login', data).then(() => {
        this.loginSuccess()
      })
    },
    getUserType() {
      // 企业邀请
      if (this.inviteId) 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.inviteId) {
        uni.reLaunch({
          url: `/sub/invite/index?inviteId=${this.inviteId}`
        })
        return
      }

      if (user.userType == 2) {
        uni.reLaunch({
          url: `/sub/invite/index`
        })
        return
      }

      // 局内用户首次登录
      if (user.audit == null || user.audit == 1) {
        // 局内用户首次登录
        uni.reLaunch({
          url: `/sub/owner/edit?audit=${user.audit}`
        })
        return
      }

      uni.switchTab({
        url: '/pages/index'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: column nowrap;
  gap: 40rpx;
  background: var(--LightMode-Success-Success, #17c653);

  .title {
    color: var(--LightMode-Light-Light, #fff);
    text-align: center;
    text-shadow: 0px 2px 2px rgba(255, 255, 255, 0.12);
    font-family: ShiShangJianTi;
    font-size: 80rpx;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
    letter-spacing: 4rpx;
  }
}
</style>