permission.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import Vue from 'vue'
  2. import router from './router'
  3. import store from './store'
  4. import { getToken, getWxToken } from '@/utils/auth'
  5. //课程免登录名单
  6. const whiteList = ['/courseHome', '/courseLogin','/courseUser','/courseError','/courseAuth','/courseDeal', '/course/adlist', '/course/limitChange', '/course/courseManage', '/course/courseCreate', '/course/dealerManage']
  7. const whiteList2 = ['/courseDeal/', '/courseDetail/','/courseTeam/', '/courseAdDetail/', '/course/limitChange/', '/course/transfer/', '/course/video/', '/course/dealOrder/']
  8. function filterWhite(path) {
  9. return whiteList2.some(item => {
  10. return path.includes(item)
  11. })
  12. }
  13. router.beforeEach((to, from, next) => {
  14. if (Vue.$httpRequestList.length > 0) { //强行中断时才向下执行
  15. Vue.$httpRequestList.forEach(item => {
  16. item('interrupt'); //给个标志,中断请求
  17. })
  18. }
  19. if (getToken() && whiteList.indexOf(to.path) == -1 && !filterWhite(to.path)) {
  20. if (to.path == '/login' || to.path == '/') { // 在免登录白名单,直接进入
  21. next({ name: 'home' })
  22. return false
  23. }
  24. if (typeof (window.$routes_map[to.name]) != 'undefined') {
  25. window.document.title = window.$routes_map[to.name].label
  26. } else {
  27. window.document.title = '管理中心'
  28. }
  29. // 初始一些数据
  30. init(next);
  31. // next();
  32. } else if (whiteList.indexOf(to.path) != -1 || filterWhite(to.path)) {
  33. if ((getWxToken()&&localStorage.getItem('wx_user_info')) || to.path == '/courseAuth') {
  34. if (typeof (window.$routes_map[to.name]) != 'undefined') {
  35. window.document.title = window.$routes_map[to.name].label
  36. } else {
  37. window.document.title = '功道云课程'
  38. }
  39. next()
  40. } else{
  41. if(to.path.includes('/courseDetail/')&&to.query&&to.query.code){
  42. next({
  43. path:`/courseAuth`,
  44. query:{
  45. code:`${to.query.code}-${to.params.id}`
  46. }
  47. })
  48. }else if(to.fullPath.includes('/courseLogin')&&to.query&&to.query.pid != undefined){
  49. next({
  50. path:`/courseAuth`,
  51. query:{
  52. pid:to.query.pid
  53. }
  54. })
  55. }else{
  56. // sessionStorage.setItem('cur_path',to.path)
  57. next({
  58. path:`/courseAuth`,
  59. })
  60. }
  61. }
  62. }else if(((to.path == '/verify') || (to.path == '/accountVf') || (to.path == '/iosIntercept')) && localStorage.getItem('a-token-temp')){
  63. next()
  64. } else {
  65. if (!window.$routes_map[to.name].need_login) { // 在免登录白名单,直接进入
  66. if (typeof (window.$routes_map[to.name]) != 'undefined') {
  67. window.document.title = window.$routes_map[to.name].label
  68. }
  69. next()
  70. } else {
  71. window.document.title = '用户登录'
  72. next('/login')
  73. }
  74. }
  75. })
  76. /* 路由异常错误处理,尝试解析一个异步组件时发生错误,重新渲染目标页面 */
  77. router.onError((error) => {
  78. const pattern = /Loading chunk (\d)+ failed/g;
  79. const isChunkLoadFailed = error.message.match(pattern);
  80. const targetPath = router.history.pending.fullPath;
  81. if (isChunkLoadFailed) {
  82. router.replace(targetPath);
  83. }
  84. });
  85. function init(next) {
  86. store.dispatch('get_user_info').then((res) => { //获取用户信息
  87. if (router.history.current.name == 'login') {
  88. router.push({ name: 'home' })
  89. return false
  90. }
  91. next()
  92. }).catch(() => {
  93. next()
  94. })
  95. store.dispatch('get_point_types').then(res => { }); //获取积分类型
  96. // 数据更新,每两分钟更新一次
  97. store.dispatch('get_employee_map').then((res) => { }); //获取人员列表
  98. store.dispatch('get_account_info').then((res) => { }); //获取公司信息
  99. store.dispatch('get_site_info').then((res) => { })//公司信息
  100. }