routerBefore.js 12 KB

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