auth.vue 3.1 KB

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