-
Notifications
You must be signed in to change notification settings - Fork 7.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vmicro #4938
Open
Gocas
wants to merge
2
commits into
vbenjs:main
Choose a base branch
from
Gocas:vmicro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Vmicro #4938
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
# public path | ||
VITE_BASE=/ | ||
|
||
|
||
# Basic interface address SPA | ||
VITE_GLOB_API_URL=/api | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 子服务设置 | ||
|
||
// useUserStore | ||
import { useAccessStore } from '@vben/stores'; | ||
// preferences | ||
import { updatePreferences } from '@vben/preferences'; | ||
|
||
import { type EventCenterForMicroApp } from '@micro-zoe/micro-app'; | ||
|
||
import { router } from './router'; | ||
|
||
declare global { | ||
interface Window { | ||
// 关闭沙箱时,不存在window.eventCenterForAppNameVite对象 | ||
// eventCenterForAppNameVite: any; | ||
microApp: { | ||
dispatch(data: { AppName: string; data: unknown }): void; | ||
getData(): Record<string, unknown>; | ||
} & EventCenterForMicroApp; | ||
__MICRO_APP_NAME__: string; | ||
__MICRO_APP_ENVIRONMENT__: string; | ||
__MICRO_APP_BASE_APPLICATION__: string; | ||
} | ||
} | ||
// const MAIN_ROUTER = window.microApp.router.getBaseAppRouter(); | ||
|
||
/** | ||
* 与基座进行数据交互 | ||
* 在不关闭沙箱的模式下,子应用中的window为代理对象,和真正的window对象(windows.rawWindow)不是一个对象. | ||
* 重要属性: | ||
* window.rawWindow 原始window对象-基座的window对象 | ||
* window.microApp<EventCenterForMicroApp>.getData() 获得基座传入的参数 | ||
* windows.__MICRO_APP_BASE_APPLICATION__ | ||
* windows.rawWindow.eventCenterForAppNameVite==undefined | ||
* @param {IMicroAppData} data | ||
*/ | ||
interface IMicroAppData { | ||
accessToken?: string /* token */; | ||
hash?: string /* 路由地址 */; | ||
mainPreferences?: Record<string, unknown> /* 系统配置项 */; | ||
} | ||
function handleDataListener(data: IMicroAppData & Record<string, any>) { | ||
// 以下代码为开启沙箱时的代码 | ||
try { | ||
if (!data || typeof data !== 'object') { | ||
throw new Error('Invalid data received from base application'); | ||
} | ||
const { | ||
accessToken = undefined, | ||
hash = '/', | ||
mainPreferences = undefined, | ||
} = data; | ||
|
||
// Object.keys(data).forEach((key) => { | ||
// console.warn(`子应用获得基座数据 ${key}: `, data[key]); | ||
// }); | ||
|
||
const accessStore = useAccessStore(); | ||
if (accessToken) accessStore.setAccessToken(accessToken); | ||
if (mainPreferences) updatePreferences(mainPreferences); | ||
|
||
// 当基座下发path时进行跳转 | ||
if (hash === router.currentRoute.value.hash) { | ||
console.warn('基座下发的path为空或者与当前路由相同'); | ||
} else { | ||
router.push(hash as string); | ||
console.warn('router.currentRoute:::', router.currentRoute); | ||
// history.replaceState(history.state, '', hash); | ||
} | ||
// // 向基座发送数据 | ||
// setTimeout(() => { | ||
// // MAIN_ROUTER.push('/'); | ||
// window.microApp.dispatch({ | ||
// AppName: data.AppName, | ||
// data: '向基座发送数据' + data.hash, | ||
// }); | ||
// }, 3000); | ||
} catch (error) { | ||
const errorMessage = | ||
error instanceof Error ? error.message : 'Unknown error'; | ||
console.error(`[${data.AppName}] 数据处理失败:`, errorMessage); | ||
window.microApp?.dispatch({ | ||
type: 'error', | ||
AppName: data.AppName, | ||
data: { message: errorMessage }, | ||
}); | ||
} | ||
} | ||
Gocas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export { handleDataListener }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
-moz-osx-font-smoothing: grayscale; */ | ||
} | ||
|
||
.app, | ||
#app, | ||
body, | ||
html { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add environment check before clearing data listeners
To prevent potential runtime errors when
window.microApp
is undefined outside of the micro-frontend environment, consider adding a check forwindow.__MICRO_APP_ENVIRONMENT__
before callingwindow.microApp.clearDataListener()
.Apply the following change to ensure safe execution:
📝 Committable suggestion