From 6cbdb70b44bd27ca05203b53f97688d86d52bb0e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=87=E7=BA=AF?= <harrywan@webank.com>
Date: Mon, 14 Dec 2020 21:34:13 +0800
Subject: [PATCH] feat: plugin-access done

---
 .../src/template/runtime.tpl                  | 20 +++++++++++++------
 .../generateFiles/core/plugin/index.js        |  4 +++-
 packages/fes-plugin-model/src/index.js        |  3 ---
 packages/fes-runtime/package.json             |  2 +-
 yarn.lock                                     |  8 ++++----
 5 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/packages/fes-plugin-access/src/template/runtime.tpl b/packages/fes-plugin-access/src/template/runtime.tpl
index 72c950d7..d1104464 100644
--- a/packages/fes-plugin-access/src/template/runtime.tpl
+++ b/packages/fes-plugin-access/src/template/runtime.tpl
@@ -1,18 +1,26 @@
 import { access } from "./core";
+import { plugin, ApplyPluginsType } from "@@/core/coreExports";
+
+async function getInitialState() {
+    const appGetInitialState = plugin.applyPlugins({
+        key: "getInitialState",
+        type: ApplyPluginsType.modify,
+        initialValue: {},
+    });
+    return await appGetInitialState;
+}
 
 export function onRouterCreated({ router }) {
-    router.beforeEach(async (to, from, next) => {
+    router.beforeEach(async (to, from) => {
         let path;
         if (to.matched.length === 1) {
             path = to.matched[0].path;
         } else {
             path = to.path;
         }
+        // 等待初始化数据
+        await getInitialState();
         const canRoute = await access.hasAccess(path);
-        if (canRoute) {
-            next();
-        } else {
-            next(false);
-        }
+        return canRoute
     });
 }
diff --git a/packages/fes-plugin-built-in/src/plugins/generateFiles/core/plugin/index.js b/packages/fes-plugin-built-in/src/plugins/generateFiles/core/plugin/index.js
index 1eccdbed..9285f28b 100644
--- a/packages/fes-plugin-built-in/src/plugins/generateFiles/core/plugin/index.js
+++ b/packages/fes-plugin-built-in/src/plugins/generateFiles/core/plugin/index.js
@@ -25,7 +25,9 @@ export default function (api) {
                 // 修改路由
                 'patchRoutes',
                 // 生成router时触发
-                'onRouterCreated'
+                'onRouterCreated',
+                // 初始化数据
+                'getInitialState'
             ]
         });
         const plugins = await api.applyPlugins({
diff --git a/packages/fes-plugin-model/src/index.js b/packages/fes-plugin-model/src/index.js
index 0c6c03fe..a775a56d 100644
--- a/packages/fes-plugin-model/src/index.js
+++ b/packages/fes-plugin-model/src/index.js
@@ -83,9 +83,6 @@ export default (api) => {
         }
     ]);
 
-    // 注册 getInitialState 方法
-    api.addRuntimePluginKey(() => 'getInitialState');
-
     // 注册 initialStateConfig 方法
     api.addRuntimePluginKey(() => 'initialStateConfig');
 
diff --git a/packages/fes-runtime/package.json b/packages/fes-runtime/package.json
index bc0dee2f..d4080b60 100644
--- a/packages/fes-runtime/package.json
+++ b/packages/fes-runtime/package.json
@@ -23,7 +23,7 @@
     "vue": "^3.0.0"
   },
   "dependencies": {
-    "vue-router": "^4.0.0-rc.1"
+    "vue-router": "^4.0.1"
   },
   "module": "dist/index.esm.js"
 }
diff --git a/yarn.lock b/yarn.lock
index c40ea262..9694edd4 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -14711,10 +14711,10 @@ vue-loader@^16.0.0-rc.1:
     hash-sum "^2.0.0"
     loader-utils "^2.0.0"
 
-vue-router@^4.0.0-rc.1:
-  version "4.0.0-rc.1"
-  resolved "https://registry.npmjs.org/vue-router/-/vue-router-4.0.0-rc.1.tgz#42f41315849163a1243886c9aa6d7c14f24fd003"
-  integrity sha512-N3SSOIiRFo1/D6EkHGuahUSuyDvFhKizN5zVXkALX7wv0hYYndV49KwzRF5lKsAIt+OlDl7y+sNmwNewb7a4iw==
+vue-router@^4.0.1:
+  version "4.0.1"
+  resolved "https://registry.yarnpkg.com/vue-router/-/vue-router-4.0.1.tgz#309240e50524b1e7d4e82e27f0a0fa25fe3d1d71"
+  integrity sha512-2C2nRxA2nCusgJyUpvcbd9Bnc9kACp/VLUCK4drXtgeRXHjQliZJcgjjP268vkGvvEKun9jjp8Ic1PpzUgbYKg==
 
 vue-types@^3.0.0:
   version "3.0.1"