瀏覽代碼

Dynamic pursuit authority from server

陈小聪 7 年之前
父節點
當前提交
1682159977
共有 2 個文件被更改,包括 43 次插入0 次删除
  1. 5 0
      mock/route.js
  2. 38 0
      src/app.js

+ 5 - 0
mock/route.js

@@ -0,0 +1,5 @@
+export default {
+  '/api/auth_routes': {
+    '/form/advanced-form': { authority: ['admin', 'user'] },
+  },
+};

+ 38 - 0
src/app.js

@@ -0,0 +1,38 @@
+import fetch from 'dva/fetch';
+
+export const dva = {
+  config: {
+    onError(err) {
+      err.preventDefault();
+    },
+  },
+};
+
+let authRoutes = null;
+
+function ergodicRoutes(routes, authKey, authority) {
+  routes.forEach(element => {
+    if (element.path === authKey) {
+      Object.assign(element.authority, authority || []);
+    } else if (element.routes) {
+      ergodicRoutes(element.routes, authKey, authority);
+    }
+    return element;
+  });
+}
+
+export function patchRoutes(routes) {
+  Object.keys(authRoutes).map(authKey =>
+    ergodicRoutes(routes, authKey, authRoutes[authKey].authority)
+  );
+  window.g_routes = routes;
+}
+
+export function render(oldRender) {
+  fetch('/api/auth_routes')
+    .then(res => res.json())
+    .then(ret => {
+      authRoutes = ret;
+      oldRender();
+    });
+}