avue-router.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. let RouterPlugin = function () {
  2. this.$router = null;
  3. this.$store = null;
  4. };
  5. RouterPlugin.install = function (vue, option = {}) {
  6. this.$router = option.router;
  7. this.$store = option.store;
  8. this.$vue = new vue({ i18n: option.i18n });
  9. // 这个的作用是 为了检查出网页链接,因为本项目用到了 iframe
  10. function isURL(s) {
  11. if (s.includes('html')) return true;
  12. return /^http[s]?:\/\/.*/.test(s)
  13. }
  14. // 将参数处理为参数的形式拼接
  15. function objToform(obj) {
  16. let result = [];
  17. Object.keys(obj).forEach(ele => {
  18. result.push(`${ele}=${obj[ele]}`);
  19. })
  20. return result.join('&');
  21. }
  22. this.$router.$avueRouter = {
  23. //全局配置
  24. $website: this.$store.getters.website,
  25. group: '',
  26. meta: {},
  27. safe: this,
  28. // 设置标题
  29. setTitle: (title) => {
  30. const defaultTitle = this.$vue.$t('title');
  31. let titleName=JSON.parse(localStorage.getItem('user-Information')).deptName
  32. title = title ? `${title}-${titleName}` : defaultTitle;
  33. document.title = title;
  34. },
  35. closeTag: (value) => {
  36. let tag = value || this.$store.getters.tag;
  37. if (typeof value === 'string') {
  38. tag = this.$store.getters.tagList.filter(ele => ele.value === value)[0]
  39. }
  40. this.$store.commit('DEL_TAG', tag)
  41. },
  42. generateTitle: (title, key) => {
  43. if (!key) return title;
  44. const hasKey = this.$vue.$te('route.' + key)
  45. if (hasKey) {
  46. // $t :this method from vue-i18n, inject in @/lang/index.js
  47. const translatedTitle = this.$vue.$t('route.' + key)
  48. return translatedTitle
  49. }
  50. return title
  51. },
  52. //处理路由
  53. getPath: function (params) {
  54. let {src} = params;
  55. let result = src || '/';
  56. if (isURL(src)) {
  57. result = `/myiframe/urlPath?${objToform(params)}`;
  58. }
  59. return result;
  60. },
  61. //正则处理路由
  62. vaildPath: function (list, path) {
  63. let result = false;
  64. list.forEach(ele => {
  65. if (new RegExp("^" + ele + ".*", "g").test(path)) {
  66. result = true
  67. }
  68. })
  69. return result;
  70. },
  71. //设置路由值
  72. getValue: function (route) {
  73. let value = "";
  74. if (route.query.src) {
  75. value = route.query.src;
  76. } else {
  77. value = route.path;
  78. }
  79. return value;
  80. },
  81. //动态路由
  82. // 路由是专门的一个接口获取
  83. /**
  84. * aMenu: 接受到的动态路由数据 menu的结构外层有父级path 里面有一个childen 记录页面的路由
  85. * first: 为了区分外界 调用formatRoutes 和 当前文件调用 formatRoutes
  86. */
  87. formatRoutes: function (aMenu = [], first) {
  88. // window.console.log('aMenu')
  89. // window.console.log(aMenu)
  90. const aRouter = []
  91. // 获取到全局配置中的 props
  92. const propsConfig = this.$website.menu.props;
  93. // 设置 props默认值 作用就是将字段设置成配置的
  94. const propsDefault = {
  95. label: propsConfig.label || 'name',
  96. path: propsConfig.path || 'path',
  97. icon: propsConfig.icon || 'icon',
  98. children: propsConfig.children || 'children',
  99. meta: propsConfig.meta || 'meta',
  100. }
  101. // 如果没有权限菜单就结束
  102. if (aMenu.length === 0) return;
  103. // 开始处理menu
  104. for (let i = 0; i < aMenu.length; i++) {
  105. // 取到当前要处理的一项
  106. const oMenu = aMenu[i];
  107. // 这一块的赋值 也就是取到返回的值
  108. let path = (() => {
  109. if (first) {
  110. // 将 '/index' 替换为 ''
  111. return oMenu[propsDefault.path].replace('/index', '')
  112. } else {
  113. return oMenu[propsDefault.path]
  114. }
  115. })(),
  116. //特殊处理组件 执行完这个 component 也就是精确到具体的文件了 views文件夹下面就是具体的页面代码
  117. component = 'views' + oMenu.path,
  118. name = oMenu[propsDefault.label],
  119. icon = oMenu[propsDefault.icon],
  120. children = oMenu[propsDefault.children],
  121. meta = oMenu[propsDefault.meta] || {};
  122. // meta中 keepalive 的处理
  123. meta = Object.assign(meta, (function () {
  124. if (option.keepAlive === true) {
  125. return {
  126. keepAlive: true
  127. }
  128. }
  129. })());
  130. //是否有子路由
  131. const isChild = children.length !== 0;
  132. const oRouter = {
  133. path: path,
  134. component(resolve) {
  135. // 判断是否为首路由
  136. if (first) {
  137. require(['../page/index'], resolve)
  138. return
  139. // 判断是否为多层路由
  140. } else if (isChild && !first) {
  141. require(['../page/index/layout'], resolve)
  142. return
  143. // 判断是否为最终的页面视图
  144. } else {
  145. require([`../${component}.vue`], resolve)
  146. }
  147. },
  148. name: name,
  149. icon: icon,
  150. meta: meta,
  151. redirect: (() => {
  152. // 第一次进来但是没有子路由的 需要添加redirect
  153. if (!isChild && first && !isURL(path)) return `${path}/index`
  154. else return '';
  155. })(),
  156. // 整理子路由的route 配置
  157. // 处理是否为一级路由
  158. children: !isChild ? (() => {
  159. if (first) {
  160. // 这里的isURL判断,因为这个网站有使用 iframe。所以需要判断是否为网页链接
  161. if (!isURL(path)) oMenu[propsDefault.path] = `${path}/index`;
  162. return [{
  163. component(resolve) {
  164. require([`../${component}.vue`], resolve)
  165. },
  166. icon: icon,
  167. name: name,
  168. meta: meta,
  169. path: 'index'
  170. }]
  171. }
  172. return [];
  173. })() : (() => {
  174. /**
  175. * 这里是重点,当有子路由的时候 会再去执行 formatRoutes 方法,然后又会有一个新的 aMenu for循环。
  176. * 最后返回的是一个数组 aRouter 这个数组就会作为 childen的值被 return
  177. */
  178. return this.formatRoutes(children, false)
  179. })()
  180. }
  181. aRouter.push(oRouter)
  182. }
  183. // for循环结束
  184. // 这个first 卡的其实就是首路由
  185. if (first) {
  186. this.safe.$router.addRoutes(aRouter)
  187. } else {
  188. // 这里返回的是子组件
  189. return aRouter
  190. }
  191. }
  192. }
  193. }
  194. export default RouterPlugin;