authutils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import httputils from '@/utils/httputils'
  2. import store from '@/store'
  3. import menus from '@/data/menus'
  4. import session from "@/framework/utils/storage"
  5. import { MessageBox } from 'element-ui'
  6. function toLogin() {
  7. let ssoServer = process.env.SSO_SERVER
  8. let redirectUrl = window.location.protocol + '//' + window.location.host
  9. console.log('tologin ', `${ssoServer}/login?redirectUrl=${redirectUrl}/auth`)
  10. window.location.href = `${ssoServer}/login?redirectUrl=${redirectUrl}/auth`
  11. }
  12. function checkMenu(menu, ps) {
  13. let result = { name: menu.name, icon: menu.icon, path: menu.path }
  14. if (menu.children) {
  15. // 如果有下级菜单权限,则自动拥有上级菜单权限
  16. result.children = []
  17. menu.children.forEach(child => {
  18. let submenu = checkMenu(child, ps)
  19. if (submenu) {
  20. result.children.push(submenu)
  21. }
  22. })
  23. return result.children.length > 0 ? result : null
  24. } else if (menu.opts) {
  25. return menu.opts.some(opt => ps[opt.permission]) ? result : null
  26. } else {
  27. // 如果没有下级菜单且没有opts属性, 菜单可以直接访问,不需要权限
  28. return result
  29. }
  30. }
  31. export default {
  32. /**
  33. * 路由守卫, 每次路由跳转时验证登录
  34. * @param {*} to
  35. * @param {*} from
  36. * @param {*} next
  37. */
  38. routerBeforeEach: async function(to, from, next) {
  39. console.log('router before ', to)
  40. if (to.path == '/auth' || to.path == '/nouser') {
  41. next()
  42. } else {
  43. let userInfo = store.getters['layout/userInfo']
  44. console.log("user info ", userInfo)
  45. if (!userInfo) {
  46. // 本地是未登录状态, 保存目标页面地址, 去登录
  47. let lastRoute = { path: to.path, params: to.params, query: to.query }
  48. store.commit('setLastRoute', lastRoute)
  49. toLogin()
  50. } else {
  51. if (to.meta.breadcrumbs) {
  52. store.dispatch('setBreadcrumb', to.meta.breadcrumbs)
  53. }
  54. if(
  55. (from.path == "/ledger/rentadd" && session.get("rentAddData") && session.get("rentAddData").length) ||
  56. (from.path == "/ledger/cenoteadd" && session.get("cenoteAddData") && session.get("cenoteAddData").length) ||
  57. (from.path == "/ledger/deviceadd" && session.get("deviceAddData") && session.get("deviceAddData").length) ||
  58. (from.path == "/ledger/propertyadd" && session.get("propertyAddData") && session.get("propertyAddData").length) ||
  59. (from.path == "/ledger/partsadd" && session.get("partsAddData") && session.get("partsAddData").length) ||
  60. (from.path == "/ledger/systemadd" && session.get("systemAddData") && session.get("systemAddData").length)
  61. ) { //判断新增信息是否保存
  62. MessageBox.confirm("新增信息未保存,离开将丢失新增信息,是否继续?", "提示", {
  63. confirmButtonText: '确定',
  64. cancelButtonText: '取消',
  65. type: 'warning'
  66. }).then(() => {
  67. let arr = from.path.split("/")
  68. let srt = arr[arr.length - 1].slice(0,-3)
  69. session.remove(`${srt}AddData`)
  70. next()
  71. }).catch(() => {
  72. next(false)
  73. })
  74. } else {
  75. next()
  76. }
  77. }
  78. return true
  79. }
  80. },
  81. toNoUser(){
  82. console.log(this)
  83. this.$router.replace('/nouser')
  84. },
  85. getMenus(permissions) {
  86. let result = []
  87. let allMenus = []
  88. // 开发环境下展示demo页面
  89. // if (process.env.NODE_ENV === 'development') {
  90. // allMenus = menus.demoMenus
  91. // }
  92. allMenus = allMenus.concat(menus.menus)
  93. let ps = !permissions ? {} : permissions
  94. allMenus.forEach(item => {
  95. let menu = checkMenu(item, ps)
  96. if (menu) {
  97. result.push(menu)
  98. }
  99. })
  100. return result
  101. },
  102. toLogout() {
  103. // TODO
  104. store.commit('setSsoToken', null)
  105. let ssoServer = process.env.SSO_SERVER
  106. let redirectUrl = window.location.protocol + '//' + window.location.host + '/'
  107. window.location.href = `${ssoServer}/logout?redirectUrl=${redirectUrl}`
  108. },
  109. toLoginPage: toLogin
  110. }