auth.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view>
  3. <!-- 使用Uniapp的loading组件 -->
  4. <view v-if="loading" class="hc-body-loading">
  5. <text>授权登录中...</text>
  6. </view>
  7. <!-- 错误页面 -->
  8. <view v-if="isErrorShow" class="error-page">
  9. <view :style="`background-image: url(${svg403});`" class="img"></view>
  10. <view class="content">
  11. <text class="title">403</text>
  12. <text class="desc">抱歉,token授权登录异常,请重新进入</text>
  13. <view class="actions">
  14. <!-- 使用Uniapp的button组件 -->
  15. <button class="uni-btn" type="primary" size="large" @click="toLoginTap">手动登录</button>
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script setup>
  22. import { onMounted, ref } from 'vue'
  23. import {useAppStore} from "@/store";
  24. import userApi from '@/httpApi/modules/user'
  25. import { getTopUrl } from '@/utils/tools'
  26. import { useRoute, useRouter } from 'vue-router'
  27. // import svg403 from '~src/assets/view/403.svg'
  28. import { getObjVal, getObjValue, isNullES } from 'js-fast-way'
  29. import { setUserAppInfo, setUserTenantInfo } from '@/store/user'
  30. import { onLaunch, onShow } from '@dcloudio/uni-app';
  31. import {setToken, setRefreshToken} from '@/httpApi/util/auth';
  32. // 监听应用启动事件
  33. onLaunch((options) => {
  34. console.log(options,'options');
  35. });
  36. //初始变量
  37. const router = useRouter()
  38. const useRoutes = useRoute()
  39. const store = useAppStore()
  40. //先清理下缓存
  41. store.clearStoreData()
  42. const toUrl=ref("/pages/index/index")
  43. //渲染完成
  44. onMounted(() => {
  45. // http://质检的域名/#/auth-token?token=xxx&tid=xxx&pid=xxx&cid=xxx&layout=no&url=xxx
  46. console.log(useRoutes.query,'useRoutes.query');
  47. const { token, tid, pid, cid, layout, url } = getObjValue(useRoutes.query)
  48. if (!isNullES(token)) {
  49. isErrorShow.value = false
  50. toUrl.value = url ?? '/pages/index/index'
  51. //缓存数据
  52. setToken(token)
  53. store.setProjectId(pid)
  54. store.setContractId(cid)
  55. //处理授权登录
  56. setLoginByTokenData(token, tid)
  57. } else {
  58. loading.value = false
  59. isErrorShow.value = true
  60. }
  61. })
  62. //获取租户id
  63. const getTenantIdApi = async () => {
  64. const { data } = await userApi.getTenantID(getTopUrl())
  65. const { id } = await setUserTenantInfo(getObjVal(data))
  66. return id
  67. }
  68. //设置租户信息
  69. const setLoginByTokenData = async (token, tenant_id) => {
  70. const tenantIds = await getTenantIdApi()
  71. const tenantId = tenant_id ? tenant_id : tenantIds
  72. await loginByTokenApi({ token, tenantId })
  73. }
  74. //请求授权登录
  75. const loginByTokenApi = async (form) => {
  76. console.log(form,'form');
  77. const { error, code, data } = await userApi.loginByToken(form)
  78. const res = getObjVal(data)
  79. if (!error && code === 200 && res) {
  80. await setUserAppInfo(res)
  81. setTimeout(() => {
  82. loading.value = false
  83. isErrorShow.value = false
  84. window?.$message?.success('授权登录成功')
  85. router.push({ path: toUrl.value })
  86. }, 1500)
  87. } else {
  88. window.$message?.error('授权登录失败')
  89. isErrorShow.value = true
  90. loading.value = false
  91. }
  92. }
  93. //跳转登陆
  94. // const toLoginTap = () => {
  95. // router.push({ path: '/login-main' })
  96. // }
  97. // 定义响应式变量
  98. const loading = ref(false); // 控制加载状态
  99. const isErrorShow = ref(false); // 控制错误页面显示
  100. const svg403 = ref('/static/403.svg'); // 403错误图片路径
  101. // 跳转到登录页的方法
  102. const toLoginTap = () => {
  103. uni.navigateTo({
  104. url: '/pages/login/login',
  105. });
  106. };
  107. </script>
  108. <style scoped>
  109. .hc-body-loading {
  110. display: flex;
  111. justify-content: center;
  112. align-items: center;
  113. height: 100vh;
  114. font-size: 16px;
  115. color: #999;
  116. }
  117. .error-page {
  118. display: flex;
  119. flex-direction: column;
  120. align-items: center;
  121. justify-content: center;
  122. height: 100vh;
  123. text-align: center;
  124. }
  125. .img {
  126. width: 200px;
  127. height: 200px;
  128. background-size: cover;
  129. background-position: center;
  130. }
  131. .content {
  132. margin-top: 20px;
  133. }
  134. .title {
  135. font-size: 32px;
  136. font-weight: bold;
  137. color: #333;
  138. }
  139. .desc {
  140. font-size: 16px;
  141. color: #666;
  142. margin-top: 10px;
  143. }
  144. .actions {
  145. margin-top: 20px;
  146. }
  147. .uni-btn {
  148. width: 100%;
  149. }
  150. </style>