index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <div v-loading="loading" class="social-callback"></div>
  3. </template>
  4. <script setup lang="ts">
  5. import { login, callback } from '@/api/login';
  6. import { setToken, getToken } from '@/utils/auth';
  7. import { LoginData } from '@/api/types';
  8. const route = useRoute();
  9. const loading = ref(true);
  10. /**
  11. * 接收Route传递的参数
  12. * @param {Object} route.query.
  13. */
  14. const code = route.query.code as string;
  15. const state = route.query.state as string;
  16. const source = route.query.source as string;
  17. const tenantId = localStorage.getItem("tenantId") ? localStorage.getItem("tenantId") as string : '000000';
  18. const processResponse = async (res: any) => {
  19. if (res.code !== 200) {
  20. throw new Error(res.msg);
  21. }
  22. if (res.data !== null) {
  23. setToken(res.data.access_token);
  24. }
  25. ElMessage.success(res.msg);
  26. setTimeout(() => {
  27. location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
  28. }, 2000);
  29. };
  30. const handleError = (error: any) => {
  31. ElMessage.error(error.message);
  32. setTimeout(() => {
  33. location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
  34. }, 2000);
  35. };
  36. const callbackByCode = async (data: LoginData) => {
  37. try {
  38. const res = await callback(data);
  39. await processResponse(res);
  40. loading.value = false;
  41. } catch (error) {
  42. handleError(error);
  43. }
  44. };
  45. const loginByCode = async (data: LoginData) => {
  46. console.log(2)
  47. try {
  48. const res = await login(data);
  49. await processResponse(res);
  50. loading.value = false;
  51. } catch (error) {
  52. handleError(error);
  53. }
  54. };
  55. const init = async () => {
  56. const data: LoginData = {
  57. socialCode: code,
  58. socialState: state,
  59. tenantId: tenantId,
  60. source: source,
  61. clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
  62. grantType: 'social'
  63. };
  64. if (!getToken()) {
  65. await loginByCode(data);
  66. } else {
  67. await callbackByCode(data);
  68. }
  69. };
  70. onMounted(() => {
  71. nextTick(() => {
  72. init();
  73. });
  74. });
  75. </script>