routerBefore.js 12 KB

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