util.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * 生产子应用路由
  3. * @param subConfig {Array.<Object>}
  4. * @param subConfig.name 子应用名称需要唯一
  5. * @param subConfig.url 子应用地址需要访问到html
  6. * @param subConfig.baseRoute 子应用的publicPath地址
  7. */
  8. export const generateSubRoutes = (subConfig = []) => {
  9. return [].concat(subConfig).map(sub => ({
  10. path: `${sub.baseRoute.replace(/\/$/, '')}/:subPath(.*)`,
  11. name: `${sub.name}Sub`,
  12. props: ({ params }) => ({
  13. baseRoute: sub.baseRoute,
  14. name: sub.name,
  15. url: sub.url,
  16. subPath: '/' + params.subPath,
  17. withoutFramework: sub.withoutFramework
  18. }),
  19. component: () => import('@cip/components/cip-subapp-container/micro-app')
  20. }))
  21. }
  22. export const microAppRender = (render) => {
  23. console.time('microAppRender')
  24. const props = {
  25. routerBase: window.__MICRO_APP_BASE_ROUTE__,
  26. ...window.microApp.getData()
  27. }
  28. const routerController = (router) => (data) => {
  29. // 基座传下来的subPath与当前页面的fullPath不一样时才进行转换
  30. if (router.currentRoute.value.fullPath !== data.subPath) {
  31. router.push(data.subPath)
  32. }
  33. }
  34. // 异步导致数据返回时间较长
  35. render(props).then((res) => {
  36. if (props.subPath) {
  37. res.router.push(props.subPath).then(() => {
  38. res.router.afterEach(to => {
  39. window.microApp.dispatch({ type: 'pathChange', data: to.fullPath })
  40. })
  41. })
  42. }
  43. window.microApp.addDataListener(routerController(res.router))
  44. window.addEventListener('unmount', () => {
  45. // 执行卸载相关操作
  46. window.microApp.clearDataListener()
  47. res.instance.unmount()
  48. res = null
  49. })
  50. })
  51. console.timeEnd('microAppRender')
  52. }
  53. export const isSubApp = (name) => /Sub$/.test(name)
  54. /**
  55. * 判断是否需要隐藏外出framework,值为字符串正则数组
  56. * @param withoutFramework {Array<RegExp|String>}
  57. * @param path {String}
  58. * @returns {boolean}
  59. */
  60. export const judgeHiddenFramework = (withoutFramework, path) => {
  61. const regxRules = withoutFramework.filter(v => Object.prototype.toString.call(v) === '[object RegExp]')
  62. if (withoutFramework.includes(path)) return true
  63. for (let i = 0; i < regxRules.length; i++) {
  64. const regx = regxRules[i]
  65. if (regx.test(path)) return true
  66. }
  67. return false
  68. }