perf: 优化应用参数

This commit is contained in:
kuaifan 2025-07-18 09:43:48 +08:00
parent 943941e0f6
commit 4cc0c85a6c

View File

@ -114,9 +114,30 @@ export default {
const {type, message} = e.data; const {type, message} = e.data;
switch (type) { switch (type) {
case 'MICRO_APP_READY': case 'MICRO_APP_READY':
this.handleMessageOfReady(message);
break
case 'MICRO_APP_METHOD':
this.handleMessageOfMethod(message)
break
case 'MICRO_APP_BEFORE_CLOSE':
this.handleMessageOfBeforeClose(message)
break
default:
break
}
},
// (MICRO_APP_READY)
handleMessageOfReady(message) {
this.handleLoad() this.handleLoad()
this.isLoading = false this.isLoading = false
if (message && message.supportBeforeClose) {
if (!message?.supportBeforeClose) {
return
}
this.$store.commit('microApps/update', { this.$store.commit('microApps/update', {
name: this.name, name: this.name,
data: { data: {
@ -135,40 +156,48 @@ export default {
} }
} }
}) })
} },
break
case 'MICRO_APP_METHOD': // (MICRO_APP_METHOD)
handleMessageOfMethod(message) {
if (!this.data || !this.data.methods) { if (!this.data || !this.data.methods) {
return return
} }
const {id, method, args} = message; const {id, method, args} = message;
if (this.data.methods[method]) { if (!this.data.methods[method]) {
return;
}
const postMessage = (result) => { const postMessage = (result) => {
this.$refs.iframe.contentWindow.postMessage({ this.$refs.iframe.contentWindow.postMessage({
type: 'MICRO_APP_METHOD_RESULT', type: 'MICRO_APP_METHOD_RESULT',
message: {id, result: $A.cloneJSON(result)} message: {id, result: $A.cloneJSON(result)}
}, '*') }, '*')
} }
const postError = (error) => {
this.$refs.iframe.contentWindow.postMessage({
type: 'MICRO_APP_METHOD_RESULT',
message: {id, result: null, error: error?.message || error}
}, '*')
}
const before = this.data.methods[method](...args) const before = this.data.methods[method](...args)
if (before && before.then) { if (before && before.then) {
before.then(postMessage).catch(postMessage) before.then(postMessage).catch(postError)
} else { } else {
postMessage(before) postMessage(before)
} }
} },
break
case 'MICRO_APP_BEFORE_CLOSE': // (MICRO_APP_BEFORE_CLOSE)
if (this.onBeforeClose[message.id]) { handleMessageOfBeforeClose(message) {
if (!this.onBeforeClose[message.id]) {
return
}
this.onBeforeClose[message.id]() this.onBeforeClose[message.id]()
delete this.onBeforeClose[message.id] delete this.onBeforeClose[message.id]
}
break
default:
break
}
}, },
// iframe // iframe