|
@@ -0,0 +1,54 @@
|
|
|
+import WujieVue from 'wujie-vue3'
|
|
|
+import { isMicro, microList } from '@/config/mainMicro'
|
|
|
+import router from '@/router'
|
|
|
+import type { App } from 'vue'
|
|
|
+
|
|
|
+export const useMainMicro = (microName?: string) => {
|
|
|
+ const getMicroByName = (name: string) => {
|
|
|
+ return microList.find(item => item.name === name)
|
|
|
+ }
|
|
|
+ const micro = getMicroByName(microName || '')
|
|
|
+
|
|
|
+ const initWatch = () => {
|
|
|
+ const route = useRoute()
|
|
|
+ watch(
|
|
|
+ () => route.path,
|
|
|
+ () => {
|
|
|
+ WujieVue.bus.$emit(microName + '-router-change', route.path)
|
|
|
+ },
|
|
|
+ {
|
|
|
+ immediate: true
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ const initMicro = (app: App) => {
|
|
|
+
|
|
|
+ if (!isMicro) {
|
|
|
+ const { setupApp, preloadApp, bus } = WujieVue
|
|
|
+
|
|
|
+ microList.forEach((item: any) => {
|
|
|
+ setupApp(item)
|
|
|
+ preloadApp({ name: item.name, url: item.url })
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ bus.$on('sub-route-change', (name: string, path: string) => {
|
|
|
+ const mainPath = `/${name}${path}`
|
|
|
+ const currentName = router.currentRoute.value.name
|
|
|
+ const currentPath = router.currentRoute.value.path
|
|
|
+ if (name === currentName && mainPath !== currentPath) {
|
|
|
+ router.push({ path: mainPath })
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ app.use(WujieVue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ micro,
|
|
|
+ initWatch,
|
|
|
+ initMicro
|
|
|
+ }
|
|
|
+}
|