<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '授权登录',
  },
  needLogin: true,
}
</route>

<template>
  <view class="flex flex-col items-center">
    <image
      class="w-[440rpx] h-[440rpx] mt-[136rpx]"
      src="https://api.static.ycymedu.com/images/logo.png"
      mode="aspectFit"
    ></image>

    <button
      class="w-[493rpx]! mb-[40rpx] h-[88rpx]! rounded-[44rpx] text-[32rpx] text-white flex items-center justify-center bg-[#1580FF]"
      @click="handleAuthPage"
    >
      授权网页登录
    </button>

    <button class="text-[#A2A2A2] bg-[#fff] text-[32rpx]" @click="handleCancel">取消登录</button>
  </view>
</template>

<script lang="ts" setup>
import { authQrCode } from '@/service/index/api'
import { useUserStore } from '@/store/user'

const userStore = useUserStore()

let scene = ref('')
const handleCancel = () => {
  uni.switchTab({ url: '/pages/home/index/index' })
}

const handleAuthPage = () => {
  authQrCode({
    userCode: scene.value,
    messageType: 2,
    weChat: { openId: userStore.userInfo.openid, sessionKey: '', unionId: '' },
  }).then((resp) => {
    if (resp.code == 200) {
      uni.showToast({
        title: '授权成功',
        icon: 'none',
        mask: true,
      })
      uni.reLaunch({
        url: '/pages/home/index/index',
      })
    } else {
      uni.showToast({
        title: '二维码失效，请刷新后重试',
        icon: 'none',
        mask: true,
      })
    }
  })
}

onLoad((options) => {
  scene.value = options.scene || ''
  console.log('用户是否登录了', userStore.isLoginFlag)

  if (!userStore.isLoginFlag) {
    uni.navigateTo({
      url: `/login-sub/index?scene=${scene.value}`,
    })
  }
})
</script>
