router.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import {
  2. RouterMount,
  3. createRouter,
  4. runtimeQuit
  5. } from 'uni-simple-router';
  6. //配置白名单
  7. let WHiTE_LIST=['login','my-camera','auth']
  8. //员工页面,只可以员工权限的用户访问
  9. let STAFF=['auth','editPhone']
  10. //企业页面,只可以企业权限的用户访问
  11. let ENTERPRISE=['staffAuditList',
  12. 'staffAuditDetail',
  13. 'staff-temperature',
  14. 'smoke-alarm',
  15. 'gas-alarm',
  16. 'fire-hydrant',
  17. 'changePass'
  18. ]
  19. let first = null;
  20. const router = createRouter({
  21. platform: process.env.VUE_APP_PLATFORM,
  22. APP:{
  23. animation:{
  24. animationType:'slide-in-top',
  25. animationDuration:300
  26. }
  27. },
  28. routerBeforeEach:(to, from, next) => {
  29. //如果是白名单的页面,直接放行
  30. if (WHiTE_LIST.includes(to.name)) {
  31. next();
  32. return
  33. }
  34. //如果是从登陆页进入的认证页,放行;若是从其他页面进入的认证页,则需要验证是否登陆验证
  35. if (from.name=='login'&&to.name=='auth') {
  36. next()
  37. return
  38. }
  39. //拦截类型,方便提示
  40. //0:未登陆认证拦截
  41. //1:允许通行
  42. //2:权限不足拦截(员工类型访问企业类型的页面)
  43. //3: 权限不足拦截(企业类型访问员工类型的页面)
  44. let flag=0
  45. //登陆类型
  46. let loginType =uni.getStorageSync("loginType")
  47. if (loginType) {
  48. if (loginType=='staff'&&ENTERPRISE.includes(to.name)) {
  49. //员工类型访问企业类型的页面,拦截
  50. flag=2
  51. }else if(loginType=='enterprise'&&STAFF.includes(to.name)){
  52. //企业类型访问员工类型的页面,拦截
  53. flag=3
  54. }else{
  55. //允许通行
  56. flag=1
  57. }
  58. }else{
  59. flag=0
  60. }
  61. switch (flag){
  62. case 0: //未登录认证
  63. next({
  64. name: 'login',
  65. params: { fullPath: to.fullPath },
  66. NAVTYPE: 'replaceAll'
  67. });
  68. break;
  69. case 1://允许通行
  70. next()
  71. break;
  72. case 2: //员工类型访问企业类型的页面
  73. uni.showModal({
  74. title: '提示',
  75. content: '权限不足,员工类型无法访问企业类型的页面',
  76. showCancel:false,
  77. success: (res)=>{
  78. next({
  79. name: 'index',
  80. NAVTYPE: 'pushTab'
  81. });
  82. }
  83. });
  84. break;
  85. case 3://企业类型访问员工类型的页面
  86. uni.showModal({
  87. title: '提示',
  88. content: '权限不足,企业类型无法访问员工类型的页面',
  89. showCancel:false,
  90. success: (res)=>{
  91. next({
  92. name: 'index',
  93. NAVTYPE: 'pushTab'
  94. });
  95. }
  96. });
  97. break;
  98. default:
  99. break;
  100. }
  101. },
  102. routerAfterEach:(to, from) => {
  103. console.log('--------routerAfterEach----')
  104. },
  105. routerErrorEach:({type,msg})=>{
  106. console.log({type,msg})
  107. // #ifdef APP-PLUS
  108. if(type===3){
  109. router.$lockStatus=false;
  110. runtimeQuit();
  111. }
  112. // #endif
  113. },
  114. routes: [
  115. ...ROUTES,
  116. {
  117. path: '*',
  118. redirect:(to)=>{
  119. return {name:'404'}
  120. }
  121. },
  122. ]
  123. });
  124. let count=0;
  125. router.beforeEach((to, from, next) => {
  126. console.log("from:",from)
  127. console.log("to:",to)
  128. // if(count==0){
  129. // next({
  130. // path:'/pages/login/login',
  131. // NAVTYPE:'replaceAll'
  132. // })
  133. // }else{
  134. // next();
  135. // }
  136. next();
  137. count++;
  138. });
  139. router.afterEach((to, from, next) => {
  140. console.log('afterEach---跳转结束')
  141. });
  142. export {
  143. router,
  144. RouterMount
  145. }