index.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 = route.query.tenantId as string ? route.query.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 && res.data.access_token !== null) {
  23. setToken(res.data.access_token);
  24. }
  25. ElMessage.success(res.msg);
  26. setTimeout(() => {
  27. if (res.data !== null && res.data.domain !== null) {
  28. location.href = res.data.domain + import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
  29. } else {
  30. location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
  31. }
  32. }, 2000);
  33. };
  34. const handleError = (error: any) => {
  35. ElMessage.error(error.message);
  36. setTimeout(() => {
  37. location.href = import.meta.env.VITE_APP_CONTEXT_PATH + 'index';
  38. }, 2000);
  39. };
  40. const callbackByCode = async (data: LoginData) => {
  41. try {
  42. const res = await callback(data);
  43. await processResponse(res);
  44. loading.value = false;
  45. } catch (error) {
  46. handleError(error);
  47. }
  48. };
  49. const loginByCode = async (data: LoginData) => {
  50. try {
  51. const res = await login(data);
  52. await processResponse(res);
  53. loading.value = false;
  54. } catch (error) {
  55. handleError(error);
  56. }
  57. };
  58. const init = async () => {
  59. const data: LoginData = {
  60. socialCode: code,
  61. socialState: state,
  62. tenantId: tenantId,
  63. source: source,
  64. clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e',
  65. grantType: 'social'
  66. };
  67. if (!getToken()) {
  68. await loginByCode(data);
  69. } else {
  70. await callbackByCode(data);
  71. }
  72. };
  73. onMounted(() => {
  74. nextTick(() => {
  75. init();
  76. });
  77. });
  78. </script>