auth.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <div class="hc-body-loading" v-loading="loading" element-loading-text="授权登录中...">
  3. <div class="error-page" v-if="!isErrorShow">
  4. <div class="img" :style="`background-image: url(${svg403});`"/>
  5. <div class="content">
  6. <h1>403</h1>
  7. <div class="desc">抱歉,token授权登录异常,请重新进入</div>
  8. <div class="actions">
  9. <el-button type="primary" block @click="toLoginTap" size="large">手动登录</el-button>
  10. </div>
  11. </div>
  12. </div>
  13. </div>
  14. </template>
  15. <script setup>
  16. import {ref, onMounted} from "vue";
  17. import {useAppStore} from "~src/store";
  18. import {loginByToken} from "~api/user"
  19. import {useRouter, useRoute} from 'vue-router'
  20. import svg403 from '~src/assets/view/403.svg';
  21. import {initUserConfigInfo, setRouterData} from "~sto/user";
  22. import {getObjNullValue} from "vue-utils-plus"
  23. //初始变量
  24. const router = useRouter()
  25. const useRoutes = useRoute()
  26. const useAppState = useAppStore()
  27. //变量
  28. const loading = ref(true)
  29. const isErrorShow = ref(false)
  30. //渲染完成
  31. onMounted(() => {
  32. // 域名后加 /#/auth?token=xxxxxxx&account=aaaa&timestamp=1670233144838&timeInterval=300&moduleCode=UTF-8
  33. const { token, account, timestamp, timeInterval, moduleCode } = useRoutes?.query;
  34. if (token && account && timestamp && timeInterval && moduleCode) {
  35. isErrorShow.value = false
  36. loginByTokenApi({
  37. token: token,
  38. account: account,
  39. timestamp: timestamp,
  40. timeInterval: timeInterval,
  41. moduleCode: moduleCode,
  42. });
  43. } else {
  44. loading.value = false
  45. isErrorShow.value = true
  46. }
  47. })
  48. //请求授权登录
  49. const loginByTokenApi = async (form) => {
  50. const {error, code, data} = await loginByToken(form)
  51. const res = getObjNullValue(data);
  52. if (!error && code === 200 && res) {
  53. useAppState.setTokenVal(res['access_token'])
  54. useAppState.setRefreshTokenVal(res['refresh_token'])
  55. useAppState.setTenantId(res['tenant_id'])
  56. useAppState.setUserInfo(res)
  57. await setRouterData()
  58. await initUserConfigInfo()
  59. loading.value = false
  60. isErrorShow.value = false
  61. window?.$message?.success('授权登录成功');
  62. await router.push({path: '/home/index'});
  63. } else {
  64. window.$message?.error('授权登录失败')
  65. isErrorShow.value = true
  66. loading.value = false
  67. }
  68. }
  69. //跳转登陆
  70. const toLoginTap = () => {
  71. router.push({path: '/login'});
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. @import "../../styles/error/style.scss";
  76. .hc-body-loading {
  77. position: relative;
  78. height: 100%;
  79. width: 100%;
  80. }
  81. </style>