From 16821599779992bb58682935dce849bc835c4327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B0=8F=E8=81=AA?= Date: Thu, 10 Jan 2019 14:54:09 +0800 Subject: [PATCH] Dynamic pursuit authority from server --- mock/route.js | 5 +++++ src/app.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 mock/route.js create mode 100644 src/app.js diff --git a/mock/route.js b/mock/route.js new file mode 100644 index 00000000..418d10f1 --- /dev/null +++ b/mock/route.js @@ -0,0 +1,5 @@ +export default { + '/api/auth_routes': { + '/form/advanced-form': { authority: ['admin', 'user'] }, + }, +}; diff --git a/src/app.js b/src/app.js new file mode 100644 index 00000000..7de5a69e --- /dev/null +++ b/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(); + }); +} -- GitLab