app.ts 1.03 KB
Newer Older
1
import fetch from 'dva/fetch';
何乐's avatar
何乐 committed
2
import { IRoute } from 'umi-types';
3 4 5

export const dva = {
  config: {
何乐's avatar
何乐 committed
6
    onError(err: ErrorEvent) {
7 8 9 10 11
      err.preventDefault();
    },
  },
};

Yu's avatar
Yu committed
12
let authRoutes = {};
13

何乐's avatar
何乐 committed
14
function ergodicRoutes(routes: IRoute[], authKey: string, authority: string | string[]) {
15 16
  routes.forEach(element => {
    if (element.path === authKey) {
Yu's avatar
Yu committed
17
      if (!element.authority) element.authority = []; // eslint-disable-line
18 19 20 21 22 23 24 25
      Object.assign(element.authority, authority || []);
    } else if (element.routes) {
      ergodicRoutes(element.routes, authKey, authority);
    }
    return element;
  });
}

何乐's avatar
何乐 committed
26
export function patchRoutes(routes: IRoute[]) {
27
  Object.keys(authRoutes).map(authKey =>
何乐's avatar
何乐 committed
28
    ergodicRoutes(routes, authKey, authRoutes[authKey].authority),
29
  );
30
  (window as any).g_routes = routes;
31 32
}

何乐's avatar
何乐 committed
33
export function render(oldRender: Function) {
34 35
  fetch('/api/auth_routes')
    .then(res => res.json())
Yu's avatar
Yu committed
36 37 38 39 40 41 42
    .then(
      ret => {
        authRoutes = ret;
        oldRender();
      },
      () => {
        oldRender();
何乐's avatar
何乐 committed
43
      },
Yu's avatar
Yu committed
44
    );
45
}