You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress' // progress bar
  5. import 'nprogress/nprogress.css'// progress bar style
  6. import { getToken,setToken } from '@/utils/auth' // getToken from cookie
  7. NProgress.configure({ showSpinner: false })// NProgress Configuration
  8. function getQueryVariable(variable,url){
  9. var query =url;
  10. if(url==null || url==undefined || url==''){
  11. query=window.location.href;
  12. }
  13. //alert(query);
  14. var query2=query.split("?");
  15. if(query2.length>1){
  16. query=query2[1];
  17. }else{
  18. query=""
  19. return null;
  20. }
  21. var vars = query.split("&");
  22. for (var i=0;i<vars.length;i++) {
  23. var pair = vars[i].split("=");
  24. if(pair[0] == variable){return pair[1];}
  25. }
  26. return null;
  27. }
  28. var accessToken=getQueryVariable('accessToken');
  29. // permissiom judge function
  30. function hasPermission(roles, permissionRoles) {
  31. if(!roles || roles.length==0){
  32. return true;
  33. }
  34. if (!permissionRoles) return true
  35. if (roles.some(role => role.roleid==='superAdmin')) return true // admin permission passed directly
  36. return roles.some(role => permissionRoles.indexOf(role.roleid) >= 0)
  37. }
  38. //免登录白名单
  39. const whiteList = [
  40. ]
  41. router.beforeEach((to, from, next) => {
  42. NProgress.start() // start progress bar
  43. ;
  44. if(to.path==='/' || to.path.indexOf('/404')>=0 || to.path.indexOf('/401')>=0 || to.path.indexOf('/login')>=0 ||to.path.indexOf('/logout')>=0){
  45. next()
  46. NProgress.done()
  47. return;
  48. }
  49. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  50. next()
  51. NProgress.done()
  52. return;
  53. }
  54. var outUrl="";
  55. if(to.meta && to.meta.openTab==true && to.meta.outUrl){
  56. outUrl=to.meta.outUrl;
  57. if(to.query){
  58. var querys='';
  59. Object.keys(to.query).forEach(function(key){
  60. if(outUrl.indexOf(key+"=")<=0){
  61. if(querys==''){
  62. querys=key+"="+to.query[key]
  63. }else{
  64. querys=querys+"&"+key+"="+to.query[key]
  65. }
  66. }
  67. });
  68. if(querys!=''){
  69. if(outUrl.indexOf("?")>0){
  70. outUrl=outUrl+"&"+querys;
  71. }else{
  72. outUrl=outUrl+"?"+querys;
  73. }
  74. }
  75. }
  76. if(outUrl.indexOf("${router.path}")>=0){
  77. outUrl=outUrl.replace("${router.path}",to.path);
  78. }
  79. if(outUrl.indexOf("${curlDomain}")>=0){
  80. var curlDomain=window.location.protocol+"//"+window.location.host; // 返回https://mp.csdn.net
  81. outUrl=outUrl.replace("${curlDomain}",curlDomain);
  82. }
  83. var indexOfHttp=outUrl.indexOf("://");
  84. if(indexOfHttp>0){
  85. outUrl=outUrl.substr(0,indexOfHttp+3)+outUrl.substr(indexOfHttp+3,outUrl.length).replace("//","/");
  86. }else{
  87. outUrl=outUrl.replace("//","/")
  88. }
  89. window.open(outUrl,to.meta.title,null,true)
  90. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  91. return;
  92. }
  93. if (!to.meta || !to.meta.roles) {
  94. next()//
  95. NProgress.done()
  96. return;
  97. }
  98. if (getToken()) { // determine if there has token
  99. /* has token*/
  100. if (to.path.indexOf('/login')>=0) {
  101. next()//
  102. NProgress.done() // if current page is dashboard will not trigger afterEach hook, so manually handle it
  103. return;
  104. } else {
  105. if(store.getters.isLoadOk==false ){
  106. store.dispatch('GetUserInfo').then(res=>{
  107. if(!res.data.tips.isOk){
  108. store.dispatch('FedLogOut').then(() => {
  109. Message.error('请重新登陆')
  110. if(accessToken && accessToken.length>0){
  111. //window.open('/#/login',null,null,true)
  112. next({ path: '/login',replace:true })
  113. }else{
  114. next({ path: '/login' })
  115. }
  116. return
  117. })
  118. }else{
  119. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  120. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  121. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  122. }).catch(() => {
  123. store.dispatch('FedLogOut').then(() => {
  124. Message.error('路由处理出错,请重新登陆')
  125. if(accessToken && accessToken.length>0){
  126. //window.open('/#/login',null,null,true)
  127. next({ path: '/login',replace:true })
  128. }else{
  129. next({ path: '/login' })
  130. }
  131. return
  132. })
  133. })
  134. }
  135. });
  136. }else if (store.getters.added==false ) { // 判断当前用户是否已拉取完user_info信息并且已经计算完毕动态路由
  137. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  138. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  139. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  140. return
  141. }).catch(() => {
  142. store.dispatch('FedLogOut').then(() => {
  143. Message.error('路由处理出错,请重新登陆')
  144. if(accessToken && accessToken.length>0){
  145. //window.open('/#/login',null,null,true)
  146. next({ path: '/login',replace:true })
  147. }else{
  148. next({ path: '/login' })
  149. }
  150. return
  151. })
  152. })
  153. } else {
  154. // 没有动态改变权限的需求可直接next() 删除下方权限判断 ↓
  155. if (!to.meta || !to.meta.roles || !store.getters.roles || hasPermission(store.getters.roles, to.meta.roles)) {
  156. next()//
  157. return
  158. } else {
  159. next({ path: '/401', replace: true, query: { noGoBack: true }})
  160. return
  161. }
  162. // 可删 ↑
  163. }
  164. }
  165. } else {
  166. /* has no token*/
  167. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  168. next()
  169. return
  170. } else {
  171. next({path:'/login'}) // 否则全部重定向到登录页
  172. NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
  173. return
  174. }
  175. }
  176. })
  177. /**
  178. * 防止禁用弹框 _self模式
  179. * @param {} url
  180. */
  181. function newWin(url) {
  182. var id='toOpenWindow'
  183. var a = document.createElement('a');
  184. a.setAttribute('href', url);
  185. a.setAttribute('target', '_self');
  186. a.setAttribute('id', id);
  187. // 防止反复添加
  188. if(!document.getElementById(id)) document.body.appendChild(a);
  189. a.click();
  190. }
  191. function setIndexPath() {
  192. var indexPath=null
  193. var url=window.location.href;
  194. var indexName="index-path-"+process.env.CONTEXT;
  195. if(url.indexOf("/login")<=0){
  196. var indexOf=url.indexOf("#/")
  197. if(indexOf > 0){
  198. indexPath=url.substring(indexOf+1)
  199. sessionStorage.setItem(indexName,indexPath);
  200. }else{
  201. sessionStorage.removeItem(indexName);
  202. }
  203. }
  204. }
  205. setIndexPath();
  206. if(accessToken && accessToken.length>10){
  207. //alert(access_token);
  208. setToken(accessToken);
  209. store.dispatch('GetUserInfo').then(res=>{
  210. if(!res.data.tips.isOk){
  211. store.dispatch('FedLogOut').then(() => {
  212. Message.error('请重新登陆')
  213. newWin('/#/login')
  214. return
  215. })
  216. }else{
  217. store.dispatch('GenerateRoutes', {roles:store.getters.roles ,menus:store.getters.myMenus} ).then(() => { // 根据roles权限生成可访问的路由表
  218. router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
  219. var indexName="index-path-"+process.env.CONTEXT;
  220. var indexPath=sessionStorage.getItem(indexName);
  221. if(indexPath && indexPath.length>0){
  222. newWin('/#'+indexPath)
  223. }else{
  224. newWin('/')
  225. }
  226. }).catch(() => {
  227. store.dispatch('FedLogOut').then(() => {
  228. Message.error('路由处理出错,请重新登陆')
  229. newWin('/#/login')
  230. return
  231. })
  232. })
  233. }
  234. });
  235. }
  236. router.afterEach(() => {
  237. NProgress.done() // finish progress bar
  238. })