routerBefore.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // 路由前置操作
  2. import store from '@/store/index.js'
  3. import { Message } from 'element-ui'
  4. import { getCookie, setCookie, dgTree } from '@/utils/util.js'
  5. import axios from '@/config/axios.js'
  6. import { stDev, stProd, proPath } from '@/config/config.js'
  7. // 免登录白名单
  8. const whiteList = [
  9. '/',
  10. '/login',
  11. '/download',
  12. '/page404',
  13. '/dingtalkTaskMobileEnd',
  14. '/dingtalkWorkFlowMobileEnd',
  15. '/luckDraw',
  16. '/printScan',
  17. '/printScan1',
  18. '/printScan2',
  19. '/printReceipt',
  20. '/printReceipt1',
  21. '/printReceipt2'
  22. ]
  23. /**
  24. * 当前路由取标题
  25. * @param {Object} to 当前路由
  26. * @param {Array} constantRouterMap 路由map
  27. * @param {Array} menu 菜单树
  28. * @returns {String} 标题
  29. */
  30. const titleFn = function(to, menu) {
  31. let title = ''
  32. if (to.meta && to.meta.title) {
  33. return to.meta.title
  34. }
  35. //
  36. if (to.meta && to.meta.code) {
  37. // 查找标题
  38. let code = to.meta.code
  39. dgTree(menu, '', item => {
  40. if (item.menuCode === code) {
  41. title = item.menuLabel
  42. }
  43. })
  44. }
  45. return title
  46. }
  47. /**
  48. * matched 添加标题名称
  49. * @param {Array} matched
  50. * @param {Array} menu 菜单树
  51. * @returns {Array} matched
  52. */
  53. const breadcrumbFn = function(to, menu) {
  54. let code = to.meta.code
  55. let list = []
  56. let item = null
  57. let data = []
  58. let getPid = function(pId) {
  59. for (let ii of list) {
  60. if (ii.menuId === pId) {
  61. return ii
  62. }
  63. }
  64. return null
  65. }
  66. dgTree(menu, '', leaf => {
  67. list.push(leaf)
  68. if (leaf.menuCode === code) {
  69. item = leaf
  70. }
  71. })
  72. let init = function(pId) {
  73. let itemPid = getPid(pId)
  74. if (itemPid) {
  75. data.push(itemPid)
  76. init(itemPid.pId)
  77. }
  78. }
  79. //
  80. if (item) {
  81. data.push(item)
  82. init(item.pId)
  83. }
  84. if (data.length > 1) {
  85. return data.reverse()
  86. }
  87. //
  88. to.matched.forEach(item => {
  89. dgTree(menu, '', leaf => {
  90. if (item.meta && item.meta.code === leaf.menuCode) {
  91. data.push(leaf)
  92. }
  93. })
  94. })
  95. return data
  96. }
  97. // 路由前置操作
  98. const routerBefore = function(router, constantRouterMap) {
  99. let flag = false
  100. router.beforeEach((to, from, next) => {
  101. // 面包屑
  102. // console.log(from, 'from')
  103. // localStorage.setItem('fromRoute', from)
  104. if (document.domain.indexOf('steerinfo.com') > -1) {
  105. document.domain = 'steerinfo.com'
  106. }
  107. store.commit('breadcrumb', breadcrumbFn(to, store.state.routes))
  108. // 标题
  109. if (to.meta && !to.meta.title) {
  110. document.title =
  111. titleFn(to, store.state.routes) +
  112. ' ' +
  113. document.title.substr(document.title.indexOf('-'))
  114. } else {
  115. document.title =
  116. to.meta.title + ' ' + document.title.substr(document.title.indexOf('-'))
  117. }
  118. if (to.query.ticket) {
  119. console.log('开始了2')
  120. setCookie('ticket', to.query.ticket, '', '/')
  121. let res = axios.get('pass/auth/ticket', {
  122. params: {
  123. ticket: to.query.ticket || '-5696372145848366561'
  124. }
  125. })
  126. res
  127. .then(res => {
  128. if (res.succeed) {
  129. DoneCookie(to, from, next, res.data.accessToken, flag)
  130. }
  131. })
  132. .catch(err => {
  133. console.log(err)
  134. Message('登陆账户异常, 请联系管理员')
  135. })
  136. } else {
  137. console.log('开始了3')
  138. DoneCookie(to, from, next, to.query.accessToken, flag)
  139. }
  140. })
  141. }
  142. function DoneCookie(to, from, next, accessToken, flag) {
  143. if (accessToken) {
  144. if (to.query.menuLeave) {
  145. setCookie('menuLeave', to.query.menuLeave, '', '/', '', '', true)
  146. } else {
  147. setCookie('menuLeave', '', '', '/', '', '', true)
  148. }
  149. setCookie('accessToken', accessToken, '', '/')
  150. store
  151. .dispatch('getLoginInfo')
  152. .then(res => {
  153. // 拉取info
  154. if (res.code === '0') {
  155. setCookie('userId', res.data.user.userId)
  156. setCookie('loginId', res.data.user.userId) // 为配合bms取值添加
  157. setCookie('loginName', res.data.user.userCode)
  158. let info = JSON.parse(JSON.stringify(res.data.user))
  159. for (let k in info) {
  160. if (k === 'sysGroup' || k === 'sysCompanys' || k === 'sysRoles') {
  161. delete info[k]
  162. }
  163. }
  164. setCookie('userInfo', JSON.stringify(info))
  165. // 判断用户集团公司情况跳转
  166. let userInfo = res.data.user
  167. // 超级管理员
  168. if (userInfo.userCode === 'admin') {
  169. if (userInfo.sysCompanys) {
  170. // 公司列表过多时cookie存放失败,存至localStorage
  171. // setCookie('companys', JSON.stringify(userInfo.sysCompanys));
  172. window.top.localStorage.setItem(
  173. 'sysGroup',
  174. JSON.stringify(userInfo.sysGroup)
  175. )
  176. window.top.localStorage.setItem(
  177. 'companys',
  178. JSON.stringify(userInfo.sysCompanys)
  179. )
  180. if (userInfo.hasOwnProperty('sysCompanys')) {
  181. window.top.localStorage.setItem(
  182. 'companyId',
  183. userInfo.sysCompanys[0].id
  184. )
  185. }
  186. }
  187. next({
  188. path: '/default'
  189. })
  190. // 普通用户
  191. } else {
  192. if (
  193. userInfo.hasOwnProperty('sysGroup') &&
  194. userInfo.sysGroup !== '' &&
  195. userInfo.sysGroup !== null &&
  196. JSON.stringify(userInfo.sysGroup) !== '{}' &&
  197. JSON.stringify(userInfo.sysGroup) !== '[]'
  198. ) {
  199. window.top.localStorage.setItem(
  200. 'sysGroup',
  201. JSON.stringify(userInfo.sysGroup)
  202. )
  203. if (userInfo.hasOwnProperty('sysCompanys')) {
  204. window.top.localStorage.setItem(
  205. 'companys',
  206. JSON.stringify(userInfo.sysCompanys)
  207. )
  208. window.top.localStorage.setItem(
  209. 'companyId',
  210. userInfo.sysCompanys[0].id
  211. )
  212. if (userInfo.sysCompanys.length > 1) {
  213. // 跳转选择集团公司页面
  214. next({
  215. path: '/selectCompany'
  216. })
  217. } else {
  218. next({
  219. path: '/default'
  220. })
  221. }
  222. } else {
  223. this.$message.error(
  224. '后台返回数据缺少company信息,请联系开发人员!'
  225. )
  226. }
  227. } else {
  228. this.$message.error(
  229. '必须隶属一个集团!请先联系管理员添加集团信息!'
  230. )
  231. }
  232. }
  233. } else {
  234. /**
  235. Message('登录已过期,请重新登录')
  236. setCookie('accessToken', '', -1, '/')
  237. setCookie('refreshToken', '', -1, '/')
  238. setCookie('workDate', '', -1)
  239. setCookie('appId', '', -1, '/')
  240. setCookie('ticket', '', -1, '/')
  241. setTimeout(() => {
  242. window.top.location.href = store.getters.ctx
  243. }, 500)*/
  244. }
  245. })
  246. .catch(err => {
  247. console.log(err)
  248. Message('登录已过期,请重新登录')
  249. setCookie('accessToken', '', -1, '/')
  250. setCookie('refreshToken', '', -1, '/')
  251. setCookie('workDate', '', -1)
  252. setCookie('appId', '', -1, '/')
  253. setCookie('ticket', '', -1, '/')
  254. window.top.location.href = store.getters.ctx
  255. })
  256. } else if (getCookie('accessToken')) {
  257. // 判断是否有token
  258. if (
  259. window.top.document.URL === window.document.URL &&
  260. !window.top.localStorage.getItem('allPrivilege')
  261. ) {
  262. let userInfo = JSON.parse(getCookie('userInfo'))
  263. setCookie('userId', userInfo.userId)
  264. let companyId = window.top.localStorage.getItem('companyId')
  265. let menuId = window.top.localStorage.getItem('activeMenu')
  266. // 查询所有注释掉
  267. // store.dispatch('getAllMenuUrl', { companyId: companyId });
  268. // store.dispatch('getOwnMenuUrl', { userId: userId, menuId: menuId });
  269. store.dispatch('getOwnMenuUrl', {
  270. companyId: companyId
  271. })
  272. }
  273. console.log('to.path: ' + to.path)
  274. if (to.path === '/login' || to.path === '/') {
  275. // 判断非本地模式登录页面不可访问
  276. if (document.location.origin.indexOf('steerinfo.com') !== -1) {
  277. next({
  278. path: '/page404'
  279. })
  280. } else {
  281. console.log('to.path:======= ')
  282. /**
  283. next({
  284. path: '/'
  285. }); */
  286. next()
  287. }
  288. } else {
  289. if (
  290. window.top.document.URL === window.document.URL &&
  291. store.state.userInfo === null
  292. ) {
  293. // 判断当前用户是否已拉取完路由信息
  294. store
  295. .dispatch('getUserInfo')
  296. .then(res => {
  297. // 拉取info
  298. if (res.code === '0') {
  299. const userInfo = res.data
  300. let appId = getCookie('appId')
  301. userInfo.appId = appId
  302. store
  303. .dispatch('generateRoutes', {
  304. userInfo
  305. })
  306. .then(() => {
  307. // 生成可访问的路由表
  308. // router.addRoutes(store.state.routes) // 动态添加可访问路由表
  309. if (flag) {
  310. next()
  311. } else {
  312. next({
  313. ...to,
  314. replace: true
  315. })
  316. flag = true
  317. }
  318. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  319. console.log('这是个什么东西', {
  320. ...to,
  321. replace: true
  322. })
  323. })
  324. } else {
  325. Message('登录已过期,请重新登录')
  326. // next('/login'); // 否则全部重定向到登录页
  327. // window.location.href = '/views/index.html';
  328. setCookie('accessToken', '', -1, '/')
  329. setCookie('refreshToken', '', -1, '/')
  330. setCookie('appId', '', -1, '/')
  331. setCookie('workDate', '', -1)
  332. setCookie('ticket', '', -1, '/')
  333. window.top.location.href = store.getters.ctx
  334. }
  335. })
  336. .catch(err => {
  337. console.log(err)
  338. Message('登录已过期,请重新登录')
  339. // next('/login'); // 否则全部重定向到登录页
  340. // window.location.href = '/views/index.html';
  341. setCookie('accessToken', '', -1, '/')
  342. setCookie('refreshToken', '', -1, '/')
  343. setCookie('appId', '', -1, '/')
  344. setCookie('workDate', '', -1)
  345. setCookie('ticket', '', -1, '/')
  346. window.top.location.href = store.getters.ctx
  347. })
  348. } else {
  349. next() // 当有用户权限的时候,说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
  350. }
  351. }
  352. } else {
  353. if (whiteList.indexOf(to.path) !== -1) {
  354. // 在免登录白名单,直接进入
  355. // 判断非本地模式登录页面不可访问
  356. if (
  357. (to.path === '/login' || to.path === '/') &&
  358. document.location.origin.indexOf('steerinfo.com') !== -1
  359. ) {
  360. next({
  361. path: '/page404'
  362. })
  363. } else {
  364. next()
  365. }
  366. } else {
  367. Message('未登录')
  368. // next('/login'); // 否则全部重定向到登录页
  369. // window.location.href = '/views/index.html';
  370. setCookie('accessToken', '', -1, '/')
  371. setCookie('refreshToken', '', -1, '/')
  372. setCookie('workDate', '', -1)
  373. setCookie('appId', '', -1, '/')
  374. setCookie('ticket', '', -1, '/')
  375. window.top.location.href = store.getters.ctx
  376. }
  377. }
  378. }
  379. export default routerBefore