<script setup>
import { useUniApi } from 'hooks/use-uni-api'
import { getUserInfoApi, postElevatorWechatUserBindApi } from '@/api/system'
import { useUserStore } from '@/store/user'

const { navigateTo } = useUniApi()
const { setUserInfo } = useUserStore()
onMounted(async () => {
  const { error, data } = await getUserInfoApi()
  if (error) {
    navigateTo('/pages/views/login/index')
  } else {
    const { user, roles } = data
    user && setUserInfo(user)
    if (roles.includes('superadmin') || roles.includes('maintenance')) {
      uni.switchTab({
        url: '/pages/tabbar/message/index'
      })
    } else if (roles.includes('expert')) {
      uni.reLaunch({
        url: '/pages/expert/index'
      })
    } else {
      uni.reLaunch({
        url: '/pages/views/no-permission/index'
      })
    }
  }
  sessionLogin()
})

const sessionLogin = () => {
  uni.login({
    success: ({ code }) => {
      postElevatorWechatUserBindApi({ code })
    }
  })
}
</script>

<template>
  <view w-full h-100vh>
    <up-loading-page
      :loading="true"
      loading-mode="semicircle"
      loading-text=""
    />
  </view>
</template>
