routes.js 8.56 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1
import { transformURL } from './utils';
邓晓峰's avatar
邓晓峰 committed
2 3 4 5
import LoadingComponent from '@ant-design/pro-layout/es/PageLoading';
import {
    dynamic
} from '@wisdom-utils/runtime';
6
import { matchPath, Route, Switch} from 'react-router';
邓晓峰's avatar
邓晓峰 committed
7
import React, { Suspense, Fragment } from 'react';
邓晓峰's avatar
邓晓峰 committed
8 9 10
export const isURL = function(url) {
  // eslint-disable-next-line no-useless-escape
  return /^(http|https)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(url);
11 12 13
};

const DEFAULT_APPLICATION = 'civweb4';
邓晓峰's avatar
邓晓峰 committed
14
/* eslint-disable */
邓晓峰's avatar
邓晓峰 committed
15
export const guid = function(prefix) {
邓晓峰's avatar
邓晓峰 committed
16 17 18 19
  let date = new Date().getTime();
  if (window.performance && typeof window.performance.now === 'function') {
    date += performance.now();
  }
邓晓峰's avatar
邓晓峰 committed
20
  const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(
邓晓峰's avatar
邓晓峰 committed
21 22
    c,
  ) {
邓晓峰's avatar
邓晓峰 committed
23 24
    const r = (date + Math.random() * 16) % 16 | 0;
    date = Math.floor(date / 16);
邓晓峰's avatar
邓晓峰 committed
25
    return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
邓晓峰's avatar
邓晓峰 committed
26
  });
邓晓峰's avatar
邓晓峰 committed
27
  return prefix + uuid.replace(/-/g, '.') + new Date().getTime();
邓晓峰's avatar
邓晓峰 committed
28
};
邓晓峰's avatar
邓晓峰 committed
29

邓晓峰's avatar
邓晓峰 committed
30 31 32 33 34 35 36 37 38 39 40 41 42
function getParams(pathname) {
  let ret = {};
  const url = pathname ? pathname : decodeURIComponent(window.location.pathname);
  if (url.indexOf('|') > -1) {
      const params = url.split('|')[1];

      params.split('&').map(item => ret[item.split("=")[0]] = item.split("=")[1])
      return ret;
  }
  return ret;

}

43 44
function isAbsoluteURL(url) {
  return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
邓晓峰's avatar
邓晓峰 committed
45
};
46

邓晓峰's avatar
邓晓峰 committed
47
const level = 0;
邓晓峰's avatar
邓晓峰 committed
48 49
const getURL = url => {
  const i = url.indexOf('#');
邓晓峰's avatar
邓晓峰 committed
50 51
  const href = i >= 0 ? url.slice(0, i) : url;
  return href;
邓晓峰's avatar
邓晓峰 committed
52
};
邓晓峰's avatar
邓晓峰 committed
53

邓晓峰's avatar
邓晓峰 committed
54
const generRotes = (widgets, parent, level = 0) => {
邓晓峰's avatar
邓晓峰 committed
55

邓晓峰's avatar
邓晓峰 committed
56
  const ret = [];
57
  if(!widgets || widgets.length === 0){
58 59 60
    return
  }
  (widgets).forEach((item, index) => {
61 62

    if (item.hasOwnProperty('widgets') && item.widgets !== null && Array.isArray(item.widgets) && item.widgets.length > 0) {
邓晓峰's avatar
邓晓峰 committed
63 64 65
      const _level_ = level + 1;
      const path = `/civweb/${guid('web_console')}`;
      const subKey = guid('panda');
66
      const alias = DEFAULT_APPLICATION;
67 68 69 70 71
      const convertURL = transformURL(item.icon.replace(/\s*/g, ''));
      const icon = (item && item.icon && item.icon !== null &&
        isAbsoluteURL(convertURL) ? convertURL: /(\\|\/)/.test(convertURL) ?
          window.globalConfig.transformDevAssetsBaseURL(transformURL(item.icon.replace(/\s*/g, ''))): item.icon
      ) || '';
邓晓峰's avatar
邓晓峰 committed
72 73
      ret.push({
        name: item.label,
邓晓峰's avatar
邓晓峰 committed
74
        level: _level_,
邓晓峰's avatar
邓晓峰 committed
75
        path,
邓晓峰's avatar
邓晓峰 committed
76
        component: 'BasicLayout',
邓晓峰's avatar
邓晓峰 committed
77
        parent,
78
        alias: alias,
79
        hideInMenu: item.hideInMenu || false,
邓晓峰's avatar
邓晓峰 committed
80 81 82 83
        routes: generRotes(
          item.widgets,
          Object.assign({}, item, { path, key: subKey }),
          _level_,
邓晓峰's avatar
邓晓峰 committed
84
          item.label
邓晓峰's avatar
邓晓峰 committed
85 86 87
        ),
        extData: {
          ...item,
88
          icon: icon,
邓晓峰's avatar
邓晓峰 committed
89
        },
邓晓峰's avatar
邓晓峰 committed
90 91
      });
    } else {
92
      const baseURL = item.product || DEFAULT_APPLICATION;
93 94
      if(item.hasOwnProperty('url') &&  item.url === '') return;
      let url = item.hasOwnProperty('url') && item.url !== undefined && item.url !== '' ? `/${baseURL}/${item.url.replace(/\s*/g, '')}`
邓晓峰's avatar
邓晓峰 committed
95
        : !item.hasOwnProperty('url')
邓晓峰's avatar
邓晓峰 committed
96 97 98 99
          ? guid('web_console')
          : '';
      if (url.includes('url=')) {
        url = url.split('url=')[1];
邓晓峰's avatar
邓晓峰 committed
100 101
        url = getURL(url);
      }
邓晓峰's avatar
邓晓峰 committed
102

邓晓峰's avatar
邓晓峰 committed
103
      if(isURL(item.url)) {
邓晓峰's avatar
邓晓峰 committed
104 105
        url = item.url
      }
106
      url = transformURL(url);
107
      //url = url.replace(/[(\\|)|(&)]widget=[0-9]*/, '')
108
      const convertURL = item.icon && transformURL(item.icon.replace(/\s*/g, ''));
109 110 111 112
      const icon = (item && item.icon && item.icon !== null &&
        isAbsoluteURL(convertURL) ? convertURL: /(\\|\/)/.test(convertURL) ?
          window.globalConfig.transformDevAssetsBaseURL(transformURL(item.icon.replace(/\s*/g, ''))): item.icon
      ) || '';
邓晓峰's avatar
邓晓峰 committed
113

邓晓峰's avatar
邓晓峰 committed
114
      const l = level ? level + 1 : -1;
邓晓峰's avatar
邓晓峰 committed
115
      let common = {
邓晓峰's avatar
邓晓峰 committed
116
        name: item.label,
邓晓峰's avatar
邓晓峰 committed
117
        level: l,
邓晓峰's avatar
邓晓峰 committed
118
        component: 'BasicLayout',
邓晓峰's avatar
邓晓峰 committed
119
        path: baseURL !== '' ? transformURL(url) : '',
邓晓峰's avatar
邓晓峰 committed
120
        href: url,
邓晓峰's avatar
邓晓峰 committed
121
        target: isURL(url) ? '_blank' : '',
邓晓峰's avatar
邓晓峰 committed
122
        key: guid('panda'),
邓晓峰's avatar
邓晓峰 committed
123 124
        hideInMenu: item.label === '系统菜单组' ? true: item.hideInMenu,
        //l > 3 ? true: item.hideInMenu || false,
125
        alias: item.product || DEFAULT_APPLICATION,
邓晓峰's avatar
邓晓峰 committed
126 127
        extData: {
          ...item,
128
          icon,
邓晓峰's avatar
邓晓峰 committed
129 130
        },
        parent,
邓晓峰's avatar
邓晓峰 committed
131
      };
邓晓峰's avatar
邓晓峰 committed
132 133
      if(/iframe/.test(url)) {
        url = url.replace(/\/report/, '');
邓晓峰's avatar
邓晓峰 committed
134

邓晓峰's avatar
邓晓峰 committed
135
        common = {
邓晓峰's avatar
邓晓峰 committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
          name: item.label,
          level: l,
          component: dynamic({ loader: () =>
            import ( /* webpackChunkName: "p__[request]" */ `@/pages/${component}.js`), loading: LoadingComponent }),
          path: baseURL !== '' ? transformURL(url) : '',
          href: url,
          target: isURL(url) ? '_blank' : '',
          key: guid('panda'),
          hideInMenu: item.label === '系统菜单组' ? true: item.hideInMenu,
          //l > 3 ? true: item.hideInMenu || false,
          alias: item.product || DEFAULT_APPLICATION,
          extData: {
            ...item,
            icon,
          },
          params: getParams(url),
          parent,
邓晓峰's avatar
邓晓峰 committed
153
        }
邓晓峰's avatar
邓晓峰 committed
154
      }
邓晓峰's avatar
邓晓峰 committed
155
      ret.push(common);
邓晓峰's avatar
邓晓峰 committed
156 157
    }
  });
邓晓峰's avatar
邓晓峰 committed
158
  return ret;
邓晓峰's avatar
邓晓峰 committed
159
};
160 161 162 163 164 165 166 167 168
export const transformWidgets = (widgets) => {
  if(!widgets || widgets.length === 0) {
    return []
  }
  return widgets.map(item => {
    const widgets = item.widgets;
    const homepage = window.globalConfig.homepage;
    const homePageConvertArray = homepage.split("/");
    const findIndex = widgets && widgets.findIndex(item => item.label === '系统菜单组');
169
    if(findIndex === -1 && homePageConvertArray[0] !== 'civweb4') {
邓晓峰's avatar
邓晓峰 committed
170 171 172 173 174 175 176 177 178 179 180
      const index = widgets.findIndex(item => item.label === '首页');
      if(index === -1) {
        widgets.push({
          label: "首页",
          icon: '',
          product: homePageConvertArray[0],
          shortName: "首页",
          hideInMenu: true,
          url: homePageConvertArray.slice(1, homePageConvertArray.length).join("/")
        });
      }
邓晓峰's avatar
邓晓峰 committed
181
      item.widgets = Array.from(new Set(widgets));
182
    }
邓晓峰's avatar
邓晓峰 committed
183

184 185 186
    return item;
  });
}
邓晓峰's avatar
邓晓峰 committed
187
export function simpleNormalizeChildren(children) {
邓晓峰's avatar
邓晓峰 committed
188 189
  for (let i = 0; i < children.length; i++) {
    if (Array.isArray(children[i])) {
邓晓峰's avatar
邓晓峰 committed
190
      return Array.prototype.concat.apply([], children);
邓晓峰's avatar
邓晓峰 committed
191 192
    }
  }
邓晓峰's avatar
邓晓峰 committed
193
  return children;
邓晓峰's avatar
邓晓峰 committed
194
}
邓晓峰's avatar
邓晓峰 committed
195

邓晓峰's avatar
邓晓峰 committed
196
export const generFlatRoutes = (widgets, parent, subSystem, modulePkg) => {
邓晓峰's avatar
邓晓峰 committed
197 198
  const treeKeys = [];
  const flatMenu = [];
199 200 201
  if(!widgets || Array.isArray(widgets) && widgets.length === 0) {
    return [];
  }
邓晓峰's avatar
邓晓峰 committed
202
  (widgets || []).forEach(item => {
邓晓峰's avatar
邓晓峰 committed
203
    if (item.hasOwnProperty('routes')) {
邓晓峰's avatar
邓晓峰 committed
204
      const route = generFlatRoutes(item.routes, item, item.name, modulePkg);
邓晓峰's avatar
邓晓峰 committed
205
      flatMenu.push(simpleNormalizeChildren(route));
邓晓峰's avatar
邓晓峰 committed
206 207 208
    } else {
      flatMenu.push({
        ...item,
邓晓峰's avatar
邓晓峰 committed
209 210
        origin: item,
        moduleName: modulePkg[item.alias],
邓晓峰's avatar
邓晓峰 committed
211
        treeKeys,
邓晓峰's avatar
邓晓峰 committed
212 213
        subSystem,
        parent,
邓晓峰's avatar
邓晓峰 committed
214
      });
邓晓峰's avatar
邓晓峰 committed
215
    }
邓晓峰's avatar
邓晓峰 committed
216 217 218
  });
  return simpleNormalizeChildren(flatMenu);
};
dengxiaofeng's avatar
dengxiaofeng committed
219

220 221
export function RouteWithSubRoutes(route) {
  return (
邓晓峰's avatar
邓晓峰 committed
222 223
    <Route path={route.path} render={props => (<route.component {...props} route={route} routes={route.routes} params={route.params}/>)}></Route>
  )
224
}
225 226 227 228
function matchRoutes(routes, pathname, branch) {
    if (branch === void 0) {
      branch = [];
    }
邓晓峰's avatar
邓晓峰 committed
229

230 231 232
    routes.some(function (route) {
      var match = route.path ? matchPath(pathname, route) : branch.length ? branch[branch.length - 1].match // use parent match
      : Router.computeRootMatch(pathname); // use default "root" match
邓晓峰's avatar
邓晓峰 committed
233

234 235 236 237 238
      if (match) {
        branch.push({
          route: route,
          match: match
        });
邓晓峰's avatar
邓晓峰 committed
239

240 241
        if (route.routes) {
          matchRoutes(route.routes, pathname, branch);
邓晓峰's avatar
邓晓峰 committed
242
        }
243
      }
邓晓峰's avatar
邓晓峰 committed
244

245 246 247 248
      return match;
    });
    return branch;
  }
邓晓峰's avatar
邓晓峰 committed
249

250 251 252 253
function renderRoutes(routes, extraProps, switchProps) {
    if (extraProps === void 0) {
      extraProps = {};
    }
邓晓峰's avatar
邓晓峰 committed
254

255 256
    if (switchProps === void 0) {
      switchProps = {};
邓晓峰's avatar
邓晓峰 committed
257
    }
邓晓峰's avatar
邓晓峰 committed
258

259 260 261 262 263 264 265 266 267 268
    return routes ? React.createElement(Switch, switchProps, routes.map(function (route, i) {
      return React.createElement(Route, {
        key: route.key || i,
        path: route.path,
        exact: route.exact,
        strict: route.strict,
        render: function render(props) {
          return route.render ? route.render(Object.assign({}, props, {}, extraProps, {
            routes: route.routes,
            params: route.params,
邓晓峰's avatar
邓晓峰 committed
269 270
            route: route,
            rootPrefix: 'panda-console-base'
271 272 273 274
          })) : React.createElement(route.component, Object.assign({}, props, extraProps, {
            route: route,
            routes: route.routes,
            params: route.params,
邓晓峰's avatar
邓晓峰 committed
275
            rootPrefix: 'panda-console-base'
276 277 278 279 280 281 282 283 284 285 286
          }));
        }
      });
    })) : null;
}

export {
  renderRoutes,
  matchPath
}

邓晓峰's avatar
邓晓峰 committed
287
export default generRotes;