<template>
  <div></div>
</template>

<script lang="ts">
  import { ref } from 'vue';
  import { ResultEnum } from '@/enums/httpEnum';
  import { useMessage } from 'naive-ui';
  import { useUserStore } from '@/store/modules/user';
  import { useRouter } from 'vue-router';
  import { storage } from '@/utils/Storage';
  import { ACCESS_TOKEN } from '@/store/mutation-types';

  export default {
    setup() {
      return {
        serviceModalRef: ref(),
        message: useMessage(),
        userStore: useUserStore(),
        router: useRouter(),
      };
    },
    created() {
      this.handleAuthCallback();
    },
    methods: {
      async handleAuthCallback() {
        const params = new URLSearchParams(window.location.search);
        // 获取 access-token 的值
        const fsCode = params.get('code');

        console.log('fsCode:' + fsCode);
        if (fsCode) {
          try {
            const { code, msg } = await this.userStore.feishuLogin({ code: fsCode });
            console.log('登录结果:', { code, msg });
            if (code == ResultEnum.SUCCESS) {
              this.message.success('登录成功，即将进入系统');
              // this.openModal();
              await this.router.push('/selectSystem');
            } else {
              // if (code === 9999) {
              //   this.message.info(msg);
              //   await this.router.push('/login');
              // }
              this.message.info(msg || '登录失败1');
              storage.remove(ACCESS_TOKEN);
              await this.router.push('/login');
            }
          } catch (error) {
            console.error('登录失败:', error);
            this.message.info('登录失败2');
            storage.remove(ACCESS_TOKEN);
            await this.router.push('/login');
          }
        } else {
          this.message.info('取消授权');
          storage.remove(ACCESS_TOKEN);
          await this.router.push('/login');
        }
      },
    },
  };
</script>
