<template>
  <view>

  </view>
</template>

<script setup>
  // 添加路由拦截
  import '@/interceptor/routeInterceptor.js'
  import { onLoad } from '@dcloudio/uni-app'
  import { defaultColor } from '@/static/js/theme.js'
  import { api_wechatLoginEncrypt } from "@/http/api.js"
  import { useWechatAuth } from '@/share/useWechatAuth.js'
  import { useLoginSuccess } from '@/share/useLoginSuccess.js'
  import { useSetTitle } from '@/share/useSetTitle.js'
  
  useSetTitle()
  const { wechatAuth } = useWechatAuth()
  const { loginSuccess } = useLoginSuccess()

  /**
   * 微信登录
   */
  function wechatLoginEncrypt() {
    const url = window.location.href.split("?");
    if (!url[1]) {
      wechatAuth();
    } else {
      const code = {
        code: url[1],
        type: 'user',
      };
      uni.showLoading({
        title: "登录中",
        mask: true,
      });
      api_wechatLoginEncrypt(code).then(res => {
        uni.hideLoading();
        if (res.state == 200) {
          loginSuccess(res.user);
        } else if (res.state == 501) {
          uni.showModal({
            title: '提示',
            content: '您的账号被删除,请联系管理员',
            showCancel: false,
            confirmColor: defaultColor,
            confirmText: '取消',
          });
        } else if (res.state == 403) {
          uni.reLaunch({
            url: `/pages/initBind/initBind?wechat=${res.wechat}`
          })
        } else {
          uni.showToast({
            icon: 'none',
            title: res.remarks || '请求数据失败!'
          });
        }
      });
    }
  }

  onLoad((option) => {
    uni.clearStorageSync();
    wechatLoginEncrypt();
  })
</script>