Эх сурвалжийг харах

refactor: add work-frame class

zhangyuhan 3 жил өмнө
parent
commit
cd7ff4f32d
3 өөрчлөгдсөн 115 нэмэгдсэн , 10 устгасан
  1. 18 1
      README.md
  2. 87 0
      packages/core/index.js
  3. 10 9
      packages/index.js

+ 18 - 1
README.md

@@ -1,5 +1,5 @@
 # @jianyu/work-bench-frame
-> 管理应用框架, 项目示例已移除,本地开发需搭配 @jianyu/work-bench-template 理解使用
+> 管理应用框架, 项目示例已移除,近期本地开发需搭配 @jianyu/work-bench-template 使用
 
 ## Install
 ```
@@ -7,3 +7,20 @@ yarn add @jianyu/work-bench-frame
 ```
 
 ## Waiting
+
+* [ ] 更优的方案同步示例模版
+* [ ] 完善、同步文档
+
+
+## Hooks
+```
+openDialog
+onBeforeSelect
+onOpenMenu
+setMenuType
+update-message-count
+goSiteMessage
+goSiteHome
+read-message
+click-nav-customer
+```

+ 87 - 0
packages/core/index.js

@@ -0,0 +1,87 @@
+import { $BRIDGE } from './bridge'
+import { interceptorNotFindPage } from '../router/router-interceptor'
+import RouterUtils from '../router/utils'
+
+class WorkBenchFrame {
+  /**
+   * 初始化配置
+   * @param options
+   */
+  constructor (options) {
+    const {
+      debug = false,
+      title = '工作台',
+      route = {}
+    } = options
+
+    // 初始化基础配置
+    this.debug = debug
+    this.title = title
+
+    // 初始化路由配置
+    this.route = Object.assign({
+      base: '/work-bench',
+      404: '/404'
+    }, route)
+  }
+
+  /**
+   * 注入对应钩子
+   * @param Vue
+   * @param router
+   * @param subApps
+   */
+  inject ({ Vue, router, subApps } = options) {
+    this.injectBridge(options)
+    this.injectRouter(router)
+  }
+
+  /**
+   * 注入工具函数到原型 及 window
+   */
+  injectBridge ({ Vue, router, subApps }) {
+    const routerUtils = new RouterUtils({
+      router,
+      subApps
+    })
+
+
+    const $BRACE = {
+      methods: {
+        open: routerUtils.open,
+        reload: routerUtils.reload,
+        currentRoute: routerUtils.currentRoute
+      },
+      $on: $BRIDGE.on,
+      $off: $BRIDGE.off,
+      $emit: $BRIDGE.emit,
+      $promise: $BRIDGE.promise
+    }
+    $BRIDGE.on('routerUtils', () => {
+      return routerUtils
+    })
+
+    window.$BRACE = $BRACE
+    Vue.prototype.$BRACE = $BRACE
+  }
+
+  /**
+   * 注入路由钩子
+   * @param router
+   */
+  injectRouter (router) {
+    interceptorNotFindPage.call(this, router)
+  }
+
+  /**
+   * debug log
+   * @param args
+   */
+  log (...args) {
+    if (this.debug) {
+      console.info.apply(void 0,['%c [work-bench-frame]:', 'color: #24C0D7', args])
+    }
+  }
+}
+
+export default WorkBenchFrame

+ 10 - 9
packages/index.js

@@ -1,22 +1,23 @@
+import WorkBenchFrame from './core'
 import service from './api'
-import routerModules, { addRouter } from './router'
+import routerModules, { addRoutes } from './router'
 import storeModules from './store'
-import { setRouter } from './router/router'
-import { openLink } from './micro-frame'
-import frameEventBus from './event-bus'
 
-const install = function (Vue, { router }) {
+const install = function (Vue, { options = {}, callback, router, subApps = {} }) {
   if (install.installed) return
-  setRouter(router)
+  const frame = new WorkBenchFrame(options)
+  typeof callback === 'function' ? callback(frame) : frame.inject({
+    Vue,
+    router,
+    subApps
+  })
 }
 
 export {
   service,
   routerModules,
   storeModules,
-  addRouter,
-  openLink,
-  frameEventBus
+  addRoutes,
 }
 
 export default {