permission.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import router from './router'
  2. import store from './store'
  3. import NProgress from 'nprogress' // progress bar
  4. import 'nprogress/nprogress.css' // progress bar style
  5. // import { getToken } from '@/utils/auth' // get token from cookie
  6. import Cookies from 'js-cookie'
  7. import { getToken } from '@/utils/auth'
  8. import { loginOut } from '@/api/user'
  9. NProgress.configure({ showSpinner: false }) // NProgress Configuration
  10. const whiteList = ['/login', '/resetpassword', '/help', '/echarts', '/login2'] // no redirect whitelist
  11. // eslint-disable-next-line prettier/prettier
  12. router.beforeEach(async(to, from, next) => {
  13. // start progress bar
  14. NProgress.start()
  15. // set page title
  16. // determine whether the user has logged in
  17. // const hasToken = getToken()
  18. const hasToken = store.getters.id || getToken()
  19. const isWeChat = Cookies.get('isWeChat')
  20. if (to.query.courseid !== '' && to.query.courseid !== undefined) {
  21. console.log(to.query.courseid)
  22. await store.commit('SET_COURSEID', to.query.courseid)
  23. }
  24. if (to.query.testid !== '' && to.query.testid !== undefined) {
  25. await store.commit('SET_TESTID', to.query.testid)
  26. }
  27. const shareCourseId =
  28. window.location.href.indexOf('?shareCourseId=') !== -1
  29. ? window.location.href.split('?shareCourseId=')[1].toString()
  30. : ''
  31. if (shareCourseId) {
  32. await store.commit('SET_SHARECOURSEID', shareCourseId)
  33. }
  34. if (hasToken) {
  35. await store.commit('user/SET_ID', hasToken)
  36. if (to.path === '/login' || to.path === '/login2') {
  37. // if is logged in, redirect to the home page
  38. store.commit('user/SET_ID', hasToken)
  39. if (isWeChat === '1') {
  40. next({ path: '/' })
  41. } else {
  42. // const queryParams = new URLSearchParams({
  43. // redirect_uri: to.query.url,
  44. // // 可以添加其他需要的参数
  45. // timestamp: Date.now().toString()
  46. // }).toString()
  47. const redirect_uri = to.query.url ? encodeURIComponent('url=' + encodeURIComponent(to.query.url)) : ''
  48. const isApp = to.query.isApp ? to.query.isApp : '2'
  49. if (isApp === '2' && redirect_uri) {
  50. // window.location.href = `weixin://dl/business/?appid=wxf8a72764a38a40b2&path=pages/index/index&query=${redirect_uri}`
  51. window.location.href = decodeURIComponent(redirect_uri)
  52. } else if (redirect_uri) {
  53. window.location.href = decodeURIComponent(redirect_uri)
  54. } else {
  55. next({ path: '/appStoreCopy' })
  56. }
  57. }
  58. NProgress.done()
  59. } else {
  60. const userinfo = store.getters.userinfo && Object.keys(store.getters.userinfo).length > 0
  61. console.log('store.getters', store.getters)
  62. console.log('store.getters.userinfo', store.getters.userinfo)
  63. console.log('isWeChat', isWeChat)
  64. console.log('userinfo', userinfo)
  65. // console.log('to', {...to} )
  66. if (userinfo) {
  67. console.log(store)
  68. if (isWeChat === '1' || to.path === '/appStoreCopy' || to.path === '/searchL' || to.path === '/userInfoPage') {
  69. next()
  70. } else if (isWeChat === '2') {
  71. const courseId = store.getters.courseId
  72. const testId = store.getters.testId
  73. const shareCourseId = store.getters.shareCourseId
  74. if (courseId || testId || shareCourseId) {
  75. Cookies.set('isWeChat', '1')
  76. loginOut().then(async res => {
  77. await store.dispatch('user/logout')
  78. await store.commit('user/SET_ID', '')
  79. next(`/login2?redirect=${to.path}`)
  80. })
  81. } else {
  82. Cookies.set('isWeChat', '2')
  83. next('/appStoreCopy')
  84. }
  85. } else {
  86. next()
  87. }
  88. NProgress.done()
  89. } else {
  90. try {
  91. // 获取用户信息
  92. console.log(store)
  93. const data = await store.dispatch('user/getInfo')
  94. await store.commit('user/SET_USERINFO', data)
  95. const courseId = store.getters.courseId
  96. const testId = store.getters.testId
  97. const shareCourseId = store.getters.shareCourseId
  98. await store.commit('SET_TESTID', '')
  99. await store.commit('SET_COURSEID', '')
  100. await store.commit('SET_SHARECOURSEID', '')
  101. // // 根据角色生成可访问的路线图
  102. if (courseId) {
  103. next({ path: '/course', query: { courseid: courseId, userid: hasToken }, replace: true })
  104. } else if (testId) {
  105. next({ path: '/testDetail', query: { courseid: testId, userid: hasToken }, replace: true })
  106. } else if (shareCourseId) {
  107. next({
  108. path: '/courseDetail',
  109. query: { courseid: shareCourseId, urlType: 1, userid: hasToken },
  110. replace: true
  111. })
  112. } else {
  113. next({ ...to, replace: true })
  114. }
  115. } catch (error) {
  116. // remove token and go to login page to re-login
  117. // await store.dispatch('user/resetToken')
  118. // Notify({ type: 'danger', message: error || '发生异常' })
  119. console.error(error)
  120. next(`/login?redirect=${to.path}`)
  121. NProgress.done()
  122. }
  123. }
  124. }
  125. } else {
  126. // next()
  127. /* has no token*/
  128. if (whiteList.indexOf(to.path) !== -1) {
  129. // in the free login whitelist, go directly
  130. next()
  131. } else {
  132. const courseId = store.getters.courseId
  133. const testId = store.getters.testId
  134. const shareCourseId = store.getters.shareCourseId
  135. // 检查是否是微信授权回调
  136. if (courseId || testId || shareCourseId) {
  137. next(`/login2?redirect=${to.path}`)
  138. NProgress.done()
  139. } else if (window.location.href.indexOf('login') !== -1 && window.location.href.indexOf('login2') === -1) {
  140. // 微信授权回调,允许访问
  141. next('/login')
  142. NProgress.done()
  143. } else {
  144. // // other pages that do not have permission to access are redirected to the login page.
  145. next(`/login`)
  146. NProgress.done()
  147. }
  148. }
  149. }
  150. })
  151. router.afterEach(() => {
  152. // finish progress bar
  153. NProgress.done()
  154. })