auth.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { defineComponent } from 'vue'
  2. import styles from './auth.module.less'
  3. import { state, setLogin, setLogout, setLoginError } from '@/state'
  4. import { browser, setAuth } from '@/helpers/utils'
  5. import { postMessage } from '@/helpers/native-message'
  6. import { RouterView } from 'vue-router'
  7. import { Button, Icon } from 'vant'
  8. import request from '@/helpers/request'
  9. import OEmpty from '@/components/o-empty'
  10. const browserInfo = browser()
  11. export default defineComponent({
  12. name: 'Auth-loayout',
  13. data() {
  14. return {
  15. loading: false as boolean
  16. }
  17. },
  18. computed: {
  19. isExternal() {
  20. // 该路由在外部连接打开是否需要登录
  21. // 只判断是否在学生端打开
  22. return (this.$route.meta.isExternal && !browserInfo.isStudent) || false
  23. },
  24. isNeedView() {
  25. return (
  26. state.user.status === 'login' || this.$route.path === '/login' || (this as any).isExternal
  27. )
  28. }
  29. },
  30. mounted() {
  31. !this.isExternal && this.setAuth()
  32. },
  33. methods: {
  34. async setAuth() {
  35. const { query } = this.$route
  36. const token = query.userInfo || query.Authorization
  37. if (token) {
  38. setAuth(token)
  39. }
  40. if (this.loading) {
  41. return
  42. }
  43. if (state.user.status === 'init' || state.user.status === 'error') {
  44. this.loading = true
  45. try {
  46. const res = await request.get(state.platformApi + '/appLoginUser/getUserInfo', {
  47. initRequest: true, // 初始化接口
  48. requestType: 'form',
  49. hideLoading: true
  50. })
  51. // 初始化学校信息
  52. if (state.platformType === 'SCHOOL') {
  53. const schoolInfo = res.data.schoolInfos ? res.data.schoolInfos[0] : {}
  54. res.data.school = schoolInfo
  55. }
  56. setLogin(res.data)
  57. } catch (e: any) {
  58. // console.log(e, 'e')
  59. const message = e.message
  60. if (message.indexOf('5000') === -1 && message.indexOf('authentication') === -1) {
  61. setLoginError()
  62. } else {
  63. setLogout()
  64. }
  65. }
  66. this.loading = false
  67. }
  68. if (state.user.status === 'logout') {
  69. if (browser().isApp) {
  70. postMessage({ api: 'login' })
  71. } else {
  72. try {
  73. const route = this.$route
  74. const query = {
  75. returnUrl: this.$route.path,
  76. ...this.$route.query
  77. } as any
  78. if (route.meta.isRegister) {
  79. query.isRegister = route.meta.isRegister
  80. }
  81. this.$router.replace({
  82. path: '/login',
  83. query: query
  84. })
  85. } catch (error) {
  86. //
  87. }
  88. }
  89. }
  90. }
  91. },
  92. render() {
  93. return (
  94. <>
  95. {state.user.status === 'error' ? (
  96. <div class={styles.error}>
  97. <OEmpty
  98. type="notFond"
  99. classImgSize="CERT"
  100. tips="加载失败,请稍后重试"
  101. buttonText="重新加载"
  102. plain={true}
  103. btnStatus={true}
  104. onClick={this.setAuth}
  105. />
  106. </div>
  107. ) : this.isNeedView ? (
  108. <RouterView></RouterView>
  109. ) : null}
  110. </>
  111. )
  112. }
  113. })