main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import Keycloak from 'keycloak-js'
  2. import Vue from 'vue'
  3. import 'element-ui/lib/theme-chalk/index.css'
  4. import './scss/index.scss'
  5. import Element, {
  6. MessageBox,
  7. InputNumber
  8. } from 'element-ui'
  9. import App from './App'
  10. import store from './store'
  11. import router from './router'
  12. import './icons'
  13. import './components'
  14. import './permission'
  15. import {
  16. showLoading,
  17. closeLoading
  18. } from '@/utils/pop'
  19. const initOptions = {
  20. url: process.env.VUE_APP_KEYCLOAK_OPTIONS_URL,
  21. realm: process.env.VUE_APP_KEYCLOAK_OPTIONS_REALM,
  22. clientId: process.env.VUE_APP_KEYCLOAK_OPTIONS_CLIENTID,
  23. onLoad: process.env.VUE_APP_KEYCLOAK_OPTIONS_ONLOAD
  24. }
  25. const keycloak = Keycloak(initOptions)
  26. keycloak
  27. .init({ onLoad: initOptions.onLoad })
  28. .then(auth => {
  29. if (!auth) {
  30. console.error('Authenticated Failed[403]')
  31. return
  32. }
  33. console.log(keycloak)
  34. refreshKeycloak()
  35. })
  36. .catch(e => console.error('Authenticated Failed', e))
  37. .finally(startApp)
  38. function refreshKeycloak () {
  39. // Token Refresh
  40. setInterval(() => {
  41. keycloak.updateToken(70).then(refreshed => {
  42. if (refreshed) {
  43. store.dispatch('user/refresh', keycloak).catch(() => {
  44. try {
  45. MessageBox.close()
  46. } finally {
  47. store.dispatch('user/clearToken')
  48. MessageBox.confirm(
  49. '账号信息发生变化,请重新登录',
  50. '系统提示',
  51. {
  52. type: 'warning',
  53. confirmButtonText: '重新登录',
  54. center: true,
  55. showClose: false,
  56. showCancelButton: false,
  57. closeOnClickModal: false,
  58. closeOnPressEscape: false,
  59. closeOnHashChange: false
  60. }
  61. ).then(() => {
  62. store.dispatch('user/logout')
  63. })
  64. }
  65. })
  66. } else {
  67. console.warn(`Token not refreshed, valid for ${Math.round(keycloak.tokenParsed.exp + keycloak.timeSkew - new Date().getTime() / 1000)} seconds`)
  68. }
  69. }).catch(e => console.error('Failed to refresh token', e))
  70. }, 6000)
  71. }
  72. async function startApp () {
  73. document.body.setAttribute('version', __VERSION__)
  74. InputNumber.methods.handleInputChange = function (value) {
  75. const newVal = value === '' ? this.min === -Infinity ? void 0 : this.min : Number(value)
  76. if (!isNaN(newVal) || value === '') {
  77. this.setCurrentValue(newVal)
  78. }
  79. this.userInput = null
  80. }
  81. Vue.use(Element)
  82. Vue.config.productionTip = false
  83. Vue.config.errorHandler = err => {
  84. closeLoading()
  85. throw (err || 'custom reject')
  86. }
  87. Vue.prototype.__PLACEHOLDER__ = __PLACEHOLDER__
  88. Vue.prototype.__WECHAT__ = __WECHAT__
  89. Vue.prototype.$keycloak = keycloak
  90. Vue.prototype.$showLoading = showLoading
  91. Vue.prototype.$closeLoading = closeLoading
  92. Vue.prototype.$designProgram = function (id) {
  93. window.open(this.$router.resolve({
  94. name: 'program',
  95. params: { id }
  96. }).href, '_blank')
  97. }
  98. window._AMapSecurityConfig = {
  99. securityJsCode: process.env.VUE_APP_GAODE_MAP_JSCODE
  100. }
  101. await store.dispatch('user/login', keycloak)
  102. store.dispatch('permission/generateRoutes')
  103. router.addRoutes(store.getters.permissionRoutes)
  104. new Vue({
  105. router,
  106. store,
  107. render: h => h(App)
  108. }).$mount('#app')
  109. }