avue-router.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. let RouterPlugin = function() {
  2. this.$router = null;
  3. this.$store = null;
  4. };
  5. RouterPlugin.install = function(router, store) {
  6. this.$router = router;
  7. this.$store = store;
  8. function objToform(obj) {
  9. let result = [];
  10. Object.keys(obj).forEach(ele => {
  11. result.push(`${ele}=${obj[ele]}`);
  12. })
  13. return result.join('&');
  14. }
  15. this.$router.$avueRouter = {
  16. //全局配置
  17. $website: this.$store.getters.website,
  18. // 设置标题
  19. setTitle: function(title) {
  20. title = title ? `${title}——Avue 通用管理 系统快速开发框架` : 'Avue 通用管理 系统快速开发框架';
  21. document.title = title;
  22. },
  23. closeTag: (value) => {
  24. const tag = value || this.$store.getters.tag;
  25. this.$store.commit('DEL_TAG', tag)
  26. },
  27. //处理路由
  28. getPath: function(params) {
  29. let { src } = params;
  30. let result = src || '/';
  31. if (src.includes("http") || src.includes("https")) {
  32. result = `/myiframe/urlPath?${objToform(params)}`;
  33. }
  34. return result;
  35. },
  36. //正则处理路由
  37. vaildPath: function(list, path) {
  38. let result = false;
  39. list.forEach(ele => {
  40. if (new RegExp("^" + ele + ".*", "g").test(path)) {
  41. result = true
  42. }
  43. })
  44. return result;
  45. },
  46. //设置路由值
  47. getValue: function(route) {
  48. let value = "";
  49. if (route.query.src) {
  50. value = route.query.src;
  51. } else {
  52. value = route.path;
  53. }
  54. return value;
  55. },
  56. //动态路由
  57. formatRoutes: function(aMenu, first) {
  58. const aRouter = []
  59. const propsConfig = this.$website.menu.props;
  60. const propsDefault = {
  61. label: propsConfig.label || 'label',
  62. path: propsConfig.path || 'path',
  63. icon: propsConfig.icon || 'icon',
  64. children: propsConfig.children || 'children'
  65. }
  66. aMenu.forEach(oMenu => {
  67. const path = oMenu[propsDefault.path],
  68. component = oMenu.component,
  69. name = oMenu[propsDefault.label],
  70. icon = oMenu[propsDefault.icon],
  71. children = oMenu[propsDefault.children];
  72. if (component) {
  73. const isChild = children.length !== 0;
  74. const oRouter = {
  75. path: path,
  76. component(resolve) {
  77. // 判断是否为首路由
  78. if (first) {
  79. require(['../page/index'], resolve)
  80. return
  81. // 判断是否为多层路由
  82. } else if (isChild && !first) {
  83. require(['../page/index/layout'], resolve)
  84. return
  85. // 判断是否为最终的页面视图
  86. } else {
  87. require([`../${component}.vue`], resolve)
  88. }
  89. },
  90. name: name,
  91. icon: icon,
  92. redirect: (() => {
  93. if (!isChild && first) return `${path}/index`
  94. else return '';
  95. })(),
  96. // 处理是否为一级路由
  97. children: !isChild ? (() => {
  98. if (first) {
  99. return [{
  100. component(resolve) { require([`../${component}.vue`], resolve) },
  101. icon: icon,
  102. name: name,
  103. path: 'index'
  104. }]
  105. }
  106. return [];
  107. })() : (() => {
  108. return this.formatRoutes(children, false)
  109. })()
  110. }
  111. aRouter.push(oRouter)
  112. }
  113. })
  114. return aRouter
  115. }
  116. }
  117. }
  118. export default RouterPlugin;