bug
jiangping
2023-11-07 64b432916af9c9218ab3f3eca614e26c542142ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index'
import AppLayout from '@/layouts/AppLayout'
import * as dd from "dingtalk-jsapi"
import { getUserInfo } from '@/api/system/common'
import { getCode } from '@/utils/dLogin'
const Login = () => import('@/views/login')
const autoLogin = () => import('@/views/autoLogin')
const autoLoginDemo = () => import('@/views/autoLoginDemo')
const autoLoginEdgp = () => import('@/views/autoLoginEdgp')
const autoLoginDemoEdgp = () => import('@/views/autoLoginDemoEdgp')
const ErrorNoPermissions = () => import('@/views/no-permissions')
const Error404 = () => import('@/views/not-found')
 
Vue.use(VueRouter)
 
const router = new VueRouter({
  base: process.env.VUE_APP_CONTEXT_PATH + (process.env.VUE_APP_ROUTER_MODE === 'hash' ? '#' : ''),
  mode: process.env.VUE_APP_ROUTER_MODE,
  routes: [
    // 登录
    {
      name: 'login',
      path: '/login',
      component: Login
    },
    // 自动登录
    {
      name: 'autoLogin',
      path: '/autoLogin',
      component: autoLogin
    },
    // 自动登录
    {
      name: 'autoLoginDemo',
      path: '/autoLoginDemo',
      component: autoLoginDemo
    },
    // 自动登录
    {
      name: 'autoLoginEdgp',
      path: '/autoLoginEdgp',
      component: autoLoginEdgp
    },
    // 自动登录
    {
      name: 'autoLoginDemoEdgp',
      path: '/autoLoginDemoEdgp',
      component: autoLoginDemoEdgp
    },
    // 无权限
    {
      name: 'no-permissions',
      path: '/no-permissions',
      component: ErrorNoPermissions
    },
    // 404
    {
      name: 'not-found',
      path: '/not-found',
      component: Error404
    },
    // 内容页(动态加载)
    {
      name: 'layout',
      path: '',
      component: AppLayout,
      children: []
    }
  ]
})
router.beforeEach(async(to, from, next) => {
  // alert(to.query.companyId)
  // console.log('router.beforeEach', to, from);
 
  // 判断路径是否有companyId有就存储起来
  if (to.query.companyId) {
    // alert(to.query.companyId)
    await store.commit('setCompanyId', to.query.companyId)
  }
  // 无权访问&404页面可直接访问
  if (to.name === 'no-permissions' || to.name === 'not-found' || to.name === 'autoLogin' || to.name === 'autoLoginDemo' || to.name === 'autoLoginEdgp' || to.name === 'autoLoginDemoEdgp') {
    next()
    return
  }
  // 如果访问的是layout(回退时可能存在该情况),直奔index
  if (to.name === 'layout') {
    next({ name: 'index' })
    return
  }
  // 验证用户是否登录
  const userInfo = router.app.$options.store.state.userInfo
  if (userInfo != null) {
    // 如果用户不存在权限
    if (userInfo.permissions.length === 0) {
      next({ name: 'no-permissions' })
      return
    }
    // 如果访问的是登录页面,则直接跳转至首页
    if (to.name === 'login') {
      next({ name: 'index' })
      return
    }
    next()
    return
  }
  getUserInfo()
    .then(userInfo => {
      // console.log('返回信息', userInfo)
      // 如果用户不存在权限
      if (userInfo.permissions.length === 0) {
        next({ name: 'no-permissions' })
        return
      }
      // 已登录,存储userInfo
      router.app.$store.commit('setUserInfo', userInfo)
      router.app.$store.commit('setCompanyId', userInfo.company && userInfo.company.id)
      // console.log(router.app.$store.companyId);
      // 如果访问的是登录页面,则直接跳转至首页
      if (to.name === 'login') {
        next({ name: 'index' })
        return
      }
      next()
    })
    .catch(e => {
      // const login = process.env.VUE_APP_ROUTER_MODE === 'history' ? 'login' : '/doumeeplant_web/#/login'
      console.log('未登录', store.state.companyId)
      if (dd.env.platform !== "notInDingTalk") {
        getCode(store.state.companyId, userInfo => {
          if (userInfo.permissions.length === 0) {
            next({ name: 'no-permissions' })
            return
          }
          // 已登录,存储userInfo
          router.app.$store.commit('setUserInfo', userInfo)
          // 如果访问的是登录页面,则直接跳转至首页
          if (to.name === 'login') {
            next({ name: 'index' })
            return
          }
          next()
        })
        // getCode(userInfo=>{
        //   if (userInfo.permissions.length === 0) {W
        //     next({ name: 'no-permissions' })
        //     return
        //   }
        //   // 已登录,存储userInfo
        //   router.app.$store.commit('setUserInfo', userInfo)
        //   // 如果访问的是登录页面,则直接跳转至首页
        //   if (to.name === 'login') {
        //     next({ name: 'index' })
        //     return
        //   }
        //   next()
        // })
      } else {
        const login = 'login'
        if (to.name === login) {
          next()
        } else {
          next({ name: login })
        }   
      }
      // 如果访问的是登录页面,则直接放行
         
    })
})
 
export default router