no message

This commit is contained in:
kuaifan 2025-05-07 12:21:09 +08:00
parent 1c5b856800
commit 3018f3653c
2 changed files with 165 additions and 135 deletions

View File

@ -1,45 +1,52 @@
<template> <template>
<div <div class="micro-app-wrapper">
v-if="appConfig.transparent" <template v-for="app in apps">
v-transfer-dom <div
:data-transfer="true"> v-if="app.transparent"
<micro-app v-transfer-dom
v-if="appConfig.isOpen" :data-transfer="true">
:name="appConfig.appName" <micro-app
:url="appConfig.appUrl" v-if="app.isOpen"
:keep-alive="appConfig.keepAlive" :name="app.appName"
:data="appData" :url="app.appUrl"
@created="created" :keep-alive="app.keepAlive"
@beforemount="beforemount" :data="appData(app.appName)"
@mounted="mounted" @created="created"
@unmount="unmount" @beforemount="beforemount"
@error="error"/> @mounted="mounted"
</div> @unmount="unmount"
<DrawerOverlay @error="error"/>
v-else <div v-if="app.isLoading" class="micro-app-loader spinner">
v-model="appConfig.isOpen" <Loading/>
modal-class="micro-app-modal" </div>
drawer-class="micro-app-drawer"
placement="right"
:beforeClose="onBeforeClose"
:size="1200">
<template>
<micro-app
v-if="appConfig.isOpen"
:name="appConfig.appName"
:url="appConfig.appUrl"
:keep-alive="appConfig.keepAlive"
:data="appData"
@created="created"
@beforemount="beforemount"
@mounted="mounted"
@unmount="unmount"
@error="error"/>
<div v-if="loadIng > 0" class="micro-app-loading">
<Loading/>
</div> </div>
<DrawerOverlay
v-else
v-model="app.isOpen"
modal-class="micro-app-modal"
drawer-class="micro-app-drawer"
placement="right"
:beforeClose="async () => { await onBeforeClose(app.appName) }"
:size="1200">
<template>
<micro-app
v-if="app.isOpen"
:name="app.appName"
:url="app.appUrl"
:keep-alive="app.keepAlive"
:data="appData(app.appName)"
@created="created"
@beforemount="beforemount"
@mounted="mounted"
@unmount="unmount"
@error="error"/>
<div v-if="app.isLoading" class="micro-app-loader">
<Loading/>
</div>
</template>
</DrawerOverlay>
</template> </template>
</DrawerOverlay> </div>
</template> </template>
<style lang="scss"> <style lang="scss">
@ -55,15 +62,21 @@
} }
} }
.micro-app-loading { .micro-app-loader {
position: absolute; position: absolute;
top: 0; top: 0;
left: 0; left: 0;
right: 0; right: 0;
bottom: 0; bottom: 0;
align-items: center;
display: flex; display: flex;
align-items: center;
justify-content: center; justify-content: center;
&.spinner {
position: fixed;
z-index: 9999;
background-color: rgba(255, 255, 255, 0.6);
}
} }
</style> </style>
@ -80,8 +93,6 @@ import DrawerOverlay from "./DrawerOverlay/index.vue";
import emitter from "../store/events"; import emitter from "../store/events";
import TransferDom from "../directives/transfer-dom"; import TransferDom from "../directives/transfer-dom";
const appMaps = new Map();
export default { export default {
name: "MicroApps", name: "MicroApps",
directives: {TransferDom}, directives: {TransferDom},
@ -89,8 +100,7 @@ export default {
data() { data() {
return { return {
loadIng: 0, apps: [],
appConfig: {},
} }
}, },
@ -109,7 +119,11 @@ export default {
watch: { watch: {
userToken(token) { userToken(token) {
!token && microApp.unmountAllApps({destroy: true}) if (token) {
return
}
this.apps = [];
microApp.unmountAllApps({destroy: true})
}, },
}, },
@ -118,9 +132,61 @@ export default {
'userInfo', 'userInfo',
'themeName', 'themeName',
]), ]),
},
methods: {
//
created() {
},
//
beforemount() {
},
//
mounted(e) {
this.finish(e)
},
//
unmount() {
},
//
error(e) {
this.finish(e)
$A.modalError({
language: false,
title: this.$L('应用加载失败'),
content: e.detail.error,
onOk: () => {
this.closeMicroApp(e.detail.name, true)
},
});
},
//
finish(e) {
const app = this.apps.find(({appName}) => appName == e.detail.name);
if (app) {
app.isLoading = false
}
},
/**
* 应用数据
* @param name
* @returns {*}
*/
appData(name) {
const app = this.apps.find(({appName}) => appName == name);
if (!app) {
return {};
}
appData() {
return { return {
type: 'init',
instance: { instance: {
Vue, Vue,
store, store,
@ -135,7 +201,7 @@ export default {
}, },
initialData: { initialData: {
...this.appConfig.initialData, ...app.initialData,
systemInfo: window.systemInfo, systemInfo: window.systemInfo,
baseUrl: $A.mainUrl(), baseUrl: $A.mainUrl(),
@ -156,7 +222,7 @@ export default {
}, },
handleClose: (destroy = false) => { handleClose: (destroy = false) => {
this.closeMicroApp(destroy) this.closeMicroApp(name, destroy)
}, },
nextModalIndex: () => { nextModalIndex: () => {
@ -178,115 +244,79 @@ export default {
this.$store.dispatch('openWebTabWindow', url); this.$store.dispatch('openWebTabWindow', url);
}, },
} }
}
},
methods: {
//
created(e) {
this.stateChange(e)
},
//
beforemount() {
},
//
mounted(e) {
this.stateChange(e, true)
},
//
unmount() {
},
//
error(e) {
this.stateChange(e, true)
$A.modalError({
language: false,
title: this.$L('应用加载失败'),
content: e.detail.error,
onOk: () => {
this.closeMicroApp(true)
},
});
},
//
stateChange(e, end = false) {
const appConfig = appMaps.get(e.detail.name)
if (appConfig?.isLoading) {
if (end) {
if (appConfig.transparent) {
this.$store.dispatch('hiddenSpinner');
} else {
this.loadIng--;
}
} else {
if (appConfig.transparent) {
this.$store.dispatch('showSpinner');
} else {
this.loadIng++;
}
}
}
}, },
/** /**
* 打开微应用 * 打开微应用
* @param appConfig * @param config
* - appName 微应用名称
* - appUrl 微应用地址
* - initialData 初始化数据
* - transparent 是否透明模式 (true/false)默认 false
* - keepAlive 是否开启微应用保活 (true/false)默认 true
*/ */
openMicroApp(appConfig) { openMicroApp(config) {
// //
appConfig = Object.assign({ config.appName = config.appName || 'micro-app'
appName: 'micro-app', // config.appUrl = config.appUrl || null
appUrl: null, // URL config.initialData = $A.isJson(config.initialData) ? config.initialData : {}
initialData: {}, // config.transparent = typeof config.transparent == 'boolean' ? config.transparent : false
config.keepAlive = typeof config.keepAlive == 'boolean' ? config.keepAlive : true
transparent: false, // (true/false)
keepAlive: true, // (true/false)
isLoading: true, // (true/false)
isOpen: false, // (true/false)
}, appConfig);
// //
const lastConfig = appMaps.get(appConfig.appName) const app = this.apps.find(({appName}) => appName == config.appName);
if (lastConfig) { if (app) {
if (lastConfig.displayMode != appConfig.displayMode || lastConfig.appUrl != appConfig.appUrl) { //
microApp.unmountApp(appConfig.appName, {destroy: true}) if (app.appUrl != config.appUrl) {
} else { microApp.unmountApp(app.appName, {destroy: true})
appConfig.isLoading = false; app.isLoading = true
} }
for (let key in config) {
app[key] = config[key]
}
this.$nextTick(_ => {
app.isOpen = true
})
} else {
//
config.isLoading = true
config.isOpen = false
this.apps.push(config)
this.$nextTick(_ => {
config.isOpen = true
})
} }
//
appMaps.set(appConfig.appName, this.appConfig = appConfig);
//
this.$nextTick(_ => {
this.appConfig.isOpen = true
})
}, },
/** /**
* 关闭微应用 * 关闭微应用
* @param name
* @param destroy * @param destroy
*/ */
closeMicroApp(destroy) { closeMicroApp(name, destroy) {
this.appConfig.isOpen = false const app = this.apps.find(({appName}) => appName == name);
if (!app) {
return;
}
app.isOpen = false
if (destroy) { if (destroy) {
microApp.unmountApp(this.appConfig.appName, {destroy: true}) microApp.unmountApp(app.appName, {destroy: true})
} }
}, },
/** /**
* 关闭之前判断 * 关闭之前判断
* @param name
* @returns {Promise<unknown>} * @returns {Promise<unknown>}
*/ */
onBeforeClose() { onBeforeClose(name) {
return new Promise(resolve => { return new Promise(resolve => {
resolve() microApp.forceSetData(name, {type: 'beforeClose'}, array => {
if (!array?.find(item => item === true)) {
resolve()
}
})
}) })
}, },
} }

View File

@ -1723,7 +1723,7 @@ const timezone = require("dayjs/plugin/timezone");
keyName = '__state:' + key + '__'; keyName = '__state:' + key + '__';
} }
if (typeof value === 'undefined') { if (typeof value === 'undefined') {
return this.__loadFromlSession(key, '', keyName); return this.__loadFromSession(key, '', keyName);
} else { } else {
this.__savaToSession(key, value, keyName); this.__savaToSession(key, value, keyName);
} }
@ -1744,7 +1744,7 @@ const timezone = require("dayjs/plugin/timezone");
} }
}, },
__loadFromlSession(key, def, keyName) { __loadFromSession(key, def, keyName) {
try { try {
if (typeof keyName === 'undefined') keyName = '__seller__'; if (typeof keyName === 'undefined') keyName = '__seller__';
let seller = window.sessionStorage.getItem(keyName); let seller = window.sessionStorage.getItem(keyName);