main.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. import router from './router'
  4. import store from '@/store'
  5. import * as API_User from '@/apis/user'
  6. import {
  7. clear,
  8. } from '@/utils/storage'
  9. //引入mui
  10. import mui from '@/assets/js/mui.js'
  11. import '@/assets/css/mui.min.css'
  12. import Vconsole from 'vconsole'
  13. import '@/assets/css/icons-extra.css'
  14. window.mui = mui;
  15. window.mui.init();
  16. if (location.hostname === 'localhost') {
  17. Vue.prototype.HTTPLOCAT = ''
  18. } else {
  19. const http = window.location.protocol + '//' + location.hostname + ':' + location.port
  20. Vue.prototype.HTTPLOCAT = http + ''
  21. }
  22. Vue.config.productionTip = false
  23. //图片预览
  24. import VueDirectiveImagePreviewer from 'vue-directive-image-previewer'
  25. import 'vue-directive-image-previewer/dist/assets/style.css'
  26. Vue.use(VueDirectiveImagePreviewer, {
  27. background: {
  28. color: '#000'
  29. },
  30. // transition
  31. animate: {
  32. duration: 600,
  33. delay: 500
  34. },
  35. // loading (not supported)
  36. loading: {
  37. image: ''
  38. },
  39. // cursor(css)
  40. cursor: 'pointer',
  41. clickMethod: 'doubleClick'
  42. })
  43. import Viewer from 'v-viewer'
  44. import 'viewerjs/dist/viewer.css'
  45. Vue.use(Viewer)
  46. Viewer.setDefaults({
  47. Options: {
  48. "inline": true,
  49. "button": true,
  50. "navbar": false,
  51. "title": true,
  52. "toolbar": false,
  53. "tooltip": true,
  54. "movable": true,
  55. "zoomable": true,
  56. "rotatable": true,
  57. "scalable": true,
  58. "transition": true,
  59. "fullscreen": true,
  60. "keyboard": true,
  61. "url": "data-source"
  62. }
  63. });
  64. //获取纯权限路由数组,由函数每次获取
  65. function getAuthFunList() {
  66. let funList = [];
  67. let fun_list = store.state.auth_fun_list;
  68. if (fun_list && fun_list.length > 0) {
  69. fun_list.forEach(function(item) {
  70. funList.push(item)
  71. })
  72. }
  73. return funList;
  74. }
  75. //console.log(funList)
  76. const checkAuth = (to, from, next) => {
  77. if (to.meta != null && to.meta.requireAuth) {
  78. if (store.state.person_data || store.state.auth_fun_list) {
  79. if ((to.meta.form != null && to.meta.form) || (to.meta.mode != null && to.meta.mode)) {
  80. let funList = getAuthFunList();
  81. //验证
  82. if (funList.indexOf(to.name) > -1) {
  83. next();
  84. } else {
  85. //测试环境不验证权限
  86. if (process.env.VUE_APP_NODE_NAME == 'devlopment') {
  87. next();
  88. } else {
  89. mui.toast('无权访问');
  90. router.replace({
  91. name: 'Master'
  92. })
  93. }
  94. }
  95. } else {
  96. //不验证
  97. next();
  98. }
  99. } else {
  100. //window.location.href = '../home/#/user/login';
  101. // window.location.href = '#/user/login';
  102. router.push({
  103. name: 'UserLogin',
  104. query: {
  105. }
  106. })
  107. }
  108. } else {
  109. next();
  110. }
  111. }
  112. const checkRole = (to, from, next) => {
  113. //切换当前场景
  114. if (to.query.exchange == '1' && to.query.role_id) {
  115. API_User.exchangePopedom({
  116. id: to.query.role_id
  117. }).then(response => {
  118. checkFun(to, from, next)
  119. }).catch(error => {
  120. mui.toast(error);
  121. })
  122. } else {
  123. checkFun(to, from, next)
  124. }
  125. }
  126. const checkFun = (to, from, next) => {
  127. //如果funlist不存在,在更新版本号清空后,先重新请求再判断
  128. var token = to.query.token;
  129. if (token) {
  130. if (store.state.auth_fun_list) {
  131. checkAuth(to, from, next);
  132. } else {
  133. //这里token一定要存在,从微信模板消息带过来的
  134. API_User.getAuthFunList(token).then(response => {
  135. if (response && response.allIconRoute) {
  136. var allIconRoute = response.allIconRoute;
  137. store.commit('SET_AUTH_FUN_LIST', allIconRoute);
  138. checkAuth(to, from, next);
  139. } else {
  140. mui.toast('链接已失效,请重新登录');
  141. router.replace({
  142. name: 'Master'
  143. })
  144. }
  145. }).catch(error => {
  146. mui.toast(error);
  147. })
  148. }
  149. } else {
  150. checkAuth(to, from, next);
  151. }
  152. }
  153. router.beforeEach((to, from, next) => {
  154. if (to.query.test) {
  155. if (to.query.test == 'test') {
  156. let vConsole = new Vconsole()
  157. }
  158. }
  159. //更新版本号,清除本地数据
  160. if (store.state.app_version != process.env.VUE_APP_VERSION) {
  161. clear();
  162. //写入当前版本号
  163. store.commit('SET_APP_VERSION', process.env.VUE_APP_VERSION);
  164. }
  165. //如果传递过来了token则写入
  166. if (to.query.token) {
  167. console.log(to.query.token)
  168. store.commit('SET_TOKEN', to.query.token);
  169. }
  170. if (process.env.VUE_APP_NODE_NAME == 'production') {
  171. //window._hmt.push(['_trackEvent', '页面跳转', 'click', to.meta.title, 1]);
  172. }
  173. checkRole(to, from, next);
  174. })
  175. new Vue({
  176. router,
  177. store,
  178. render: h => h(App)
  179. }).$mount('#app')