<template>
  <div class="container"></div>
</template>

<script>
import { authorize } from '@/pages/index/api/login';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        scopes: [], // 已选中的 scope 数组
      },
      params: {
        // URL 上的 client_id、scope 等参数
        responseType: undefined,
        clientId: undefined,
        redirectUri: undefined,
        state: undefined,
        scopes: [], // 优先从 query 参数获取；如果未传递，从后端获取
      },
      client: {
        // 客户端信息
        name: '',
        logo: '',
      },
      loading: false,
    };
  },
  created() {
    // 解析参数
    // 例如说【自动授权不通过】：client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
    // 例如说【自动授权通过】：client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
    this.params.responseType = this.$route.query.response_type;
    this.params.clientId = this.$route.query.client_id;
    this.params.redirectUri = this.$route.query.redirect_uri;
    this.params.state = this.$route.query.state;
    if (this.$route.query.scope) {
      this.params.scopes = this.$route.query.scope.split(' ');
    }

    this.doAuthorize(true, this.loginForm.scopes, null).then((res) => {
      const href = res.data;
      if (!href) {
        console.log('自动授权未通过！');
        return;
      }
      location.href = href;
    });
  },
  methods: {
    doAuthorize(autoApprove, checkedScopes, uncheckedScopes) {
      return authorize(
        this.params.responseType,
        this.params.clientId,
        this.params.redirectUri,
        this.params.state,
        autoApprove,
        checkedScopes,
        uncheckedScopes,
      );
    },
  },
};
</script>
<style lang="scss" scoped></style>
