<template>
  <div>
    <h1>{{ state === '12351' ? '天猫' : '阿奇索' }} 登录回调</h1>
    <p v-if="loading">加载中...</p>
    <p v-if="error">{{ errorMessage }}</p>
    <p v-if="success">登录成功！</p>
  </div>
</template>

<script lang="ts">
import { authorizeTMAllGameLogin } from '@/api/t-mall-game-account';

export default {
  name: 'TMallGameAccountCallback',
  data() {
    return {
      state: '',
      loading: true,
      success: false,
      error: false,
      errorMessage: ''
    };
  },
  created() {
    this.handleCallback();
  },
  methods: {
    handleCallback() {
      const urlParams = new URLSearchParams(window.location.search);
      const code = urlParams.get('code');
      const error = urlParams.get('error');
      this.state = urlParams.get('state');
      this.loading = true;
      if (error) {
        this.errorMessage = '授权失败: ' + urlParams.get('error_description');
        this.error = true;
        this.loading = false;
      } else if (code) {
        // 发送请求到后端处理code
        this.postCodeToBackend(code, this.state);
        this.success = true;
      } else {
        this.errorMessage = '未找到授权码';
        this.error = true;
      }
      this.loading = false;
    },
    async postCodeToBackend(code: string, channel: string) {
      // 这里应该是一个异步请求到后端
      await authorizeTMAllGameLogin({ code, channel });
    }
  }
};
</script>
