index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import Vue from "vue"
  2. import store from "@/store"
  3. import VueRouter from "vue-router"
  4. // 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
  5. const originalPush = VueRouter.prototype.push
  6. VueRouter.prototype.push = function push (location, onResolve, onReject) {
  7. if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  8. return originalPush.call(this, location).catch(err => err)
  9. }
  10. import {
  11. query
  12. } from "@/utils/query"
  13. import {
  14. getPvUv
  15. } from "@/api/public.js"
  16. Vue.use(VueRouter)
  17. const routes = [
  18. // {
  19. // path: '/',
  20. // redirect: '/home/overview', // 只有区域,及区域以上用户才能进入总部首页这一套
  21. // },
  22. {
  23. path: "/404",
  24. name: "404页面",
  25. component: () => import("../components/404"),
  26. },
  27. {
  28. path: "/group",
  29. name: "集团首页",
  30. component: () => import("../views/statistics/index"),
  31. },
  32. // home
  33. {
  34. path: "/home",
  35. name: "home",
  36. component: () => import("../views/index"),
  37. // TODO: 1.默认跳转到项目概览
  38. redirect: "/home/overview",
  39. children: [{
  40. path: "homepage",
  41. name: "项目首页",
  42. component: () => import("../views/homepage"),
  43. },
  44. //概览
  45. {
  46. path: "overview",
  47. name: "项目概览",
  48. component: () => import("../views/overview"),
  49. },
  50. // 楼层功能
  51. {
  52. path: "floorFunc",
  53. name: "楼层功能",
  54. component: () => import("../views/floorFunc"),
  55. },
  56. // 设备设施
  57. {
  58. path: "equipment",
  59. name: "设备设施",
  60. component: () => import("../views/equipment"),
  61. },
  62. // 其他功能
  63. {
  64. path: "other",
  65. name: "其他事项",
  66. component: () => import("../views/other"),
  67. },
  68. // 分析
  69. {
  70. path: "analysis",
  71. name: "分享报表",
  72. component: () => import("../views/analysis"),
  73. },
  74. //图例库管理
  75. {
  76. path: "legendLibrary",
  77. name: "图例管理",
  78. component: () => import("../views/legendLibrary"),
  79. },
  80. //图例绘制规则
  81. {
  82. path: "legendRules",
  83. name: "绘制规则",
  84. component: () => import("../views/legendRules"),
  85. },
  86. ],
  87. },
  88. ]
  89. const router = new VueRouter({
  90. mode: "history",
  91. base: process.env.BASE_URL,
  92. routes,
  93. })
  94. const ignore = ["/404"]
  95. router.beforeEach(async (to, from, next) => {
  96. // 如果路径上有plazaId就保存
  97. let plazaId = to.query.plazaId ?to.query.plazaId:''
  98. if (plazaId) {
  99. localStorage.setItem('PLAZAID', plazaId)
  100. store.commit('STOREPLAZAID', plazaId);
  101. }
  102. let previewUrl = to.query.isPreview ? to.query.isPreview : '';
  103. if (previewUrl) {
  104. sessionStorage.setItem("SETISPREVIEW", previewUrl)
  105. store.commit('SETISPREVIEW', previewUrl)
  106. }
  107. // 取出刷新时的 ssoToken, 路由拦截时app.vue 生命周期还没有执行,store中还没有数据,
  108. let beforeunload = localStorage.getItem("beforeunload"),
  109. __token = null
  110. beforeunload && (__token = JSON.parse(beforeunload).ssoToken)
  111. if (!ignore.includes(to.path)) {
  112. //都是需要token检验的页面 首先优先获取路由上带的token
  113. let token = null
  114. if (to.query.token) {
  115. store.commit("SETSSOTOKEN", to.query.token) //存vuex
  116. sessionStorage.setItem("SSOTOKEN", to.query.token)
  117. token = to.query.token
  118. } else {
  119. if (store.getters["ssoToken"]) {
  120. token = store.getters["ssoToken"]
  121. } else if (sessionStorage.getItem("SSOTOKEN")) {
  122. token = sessionStorage.getItem("SSOTOKEN")
  123. store.commit("SETSSOTOKEN", sessionStorage.getItem("SSOTOKEN")) //存vuex
  124. } else {
  125. redirectGetToken(to,from)
  126. }
  127. }
  128. // 如果有token
  129. if (token) {
  130. // 有token 将数据 isPreview赋给 vuex方便取值
  131. store.commit('SETISPREVIEW', sessionStorage.getItem("SETISPREVIEW"))
  132. // 如果在跳转获取token之前保存了指定的路径,则后去token后需要跳转到相关页面
  133. if(to.path !='/group' && to.path !='/home/homepage' && to.path !='/' ){
  134. if(!store.state.permissions || store.state.permissions.length ==0){
  135. // 如果没有请求permissions则请求getUserInfo
  136. await store.dispatch("getUserInfoNoPath", router)
  137. }
  138. }
  139. if (sessionStorage.getItem("TOURL")) {
  140. // 如果去的地址等于缓存的地址
  141. if (sessionStorage.getItem("TOURL") == to.path) {
  142. sessionStorage.setItem("TOURL", '')
  143. if (to.path == "/") {
  144. next("/group")
  145. }
  146. if (to.path == "/group" || to.path == "/home/homepage") {
  147. if (store.state.accessLevel == -1) {
  148. await store.dispatch("getUserInfo", router)
  149. await store.dispatch("getBrand")
  150. // await store.dispatch('getFact')
  151. }
  152. }
  153. await store.dispatch("getFloors")
  154. await store.dispatch("getBrand")
  155. next()
  156. } else {
  157. next(sessionStorage.getItem("TOURL"))
  158. }
  159. } else {
  160. if (to.path == "/") {
  161. next("/group")
  162. }
  163. if (to.path == "/group" || to.path == "/home/homepage") {
  164. if (store.state.accessLevel == -1) {
  165. await store.dispatch("getUserInfo", router)
  166. console.log('getUserInfo', 4)
  167. await store.dispatch("getBrand")
  168. // await store.dispatch('getFact')
  169. }
  170. }
  171. await store.dispatch("getFloors")
  172. await store.dispatch("getBrand")
  173. next()
  174. }
  175. } else {
  176. redirectGetToken(to,from)
  177. }
  178. } else {
  179. next()
  180. return
  181. }
  182. })
  183. // 跳入路由后要对发送pvuv
  184. router.afterEach((to) => {
  185. let postParams = {
  186. type: "router",
  187. target: to.name,
  188. parameter: to.path,
  189. app:'PC'
  190. }
  191. const data = {
  192. plazaId: store.getters["plazaId"],
  193. }
  194. getPvUv(data, postParams)
  195. .then((res) => {
  196. console.log("pvuv", res)
  197. })
  198. .catch((res) => {
  199. console.log("error", res)
  200. })
  201. })
  202. // 重定向获取token
  203. function redirectGetToken (to) {
  204. let lastRoute = {
  205. path: to.path,
  206. params: to.params,
  207. query: to.query,
  208. }
  209. store.commit("SETLASTROUTER", lastRoute)
  210. let ssoServer = "http://oauth.wanda-dev.cn"
  211. if (process.env.NODE_ENV == "wanda_build") {
  212. ssoServer = "http://oauth.wanda.cn"
  213. }
  214. let systemcode = "CAD156",
  215. signal = new Date().getTime(),
  216. version = "1.0.0";
  217. // 获取token前需要保存跳转前路径
  218. if (!ignore.includes(to.path)) {
  219. let previewUrl = to.query.isPreview ? to.query.isPreview : '';
  220. let plazaId = to.query.plazaId ?to.query.plazaId:''
  221. if (previewUrl) {
  222. sessionStorage.setItem("SETISPREVIEW", previewUrl)
  223. store.commit('SETISPREVIEW', previewUrl)
  224. }
  225. // else{
  226. // sessionStorage.setItem("SETISPREVIEW", 'false')
  227. // store.commit('SETISPREVIEW', 'false')
  228. // }
  229. if (plazaId) {
  230. localStorage.setItem('PLAZAID', plazaId)
  231. store.commit('STOREPLAZAID', plazaId)
  232. }
  233. sessionStorage.setItem("TOURL", to.path)
  234. } else {
  235. // 如果重定向前为404则跳入/
  236. sessionStorage.setItem("TOURL", '/')
  237. }
  238. window.location.href = `${ssoServer}/login?systemcode=${systemcode}&signal=${signal}&version=${version}`
  239. }
  240. export default router