Skip to content

Commit f1f8d2c

Browse files
yaojp123yaojiping
andauthored
chore: add micro adaptation (#596)
Co-authored-by: yaojiping <yaojiping@infini.ltd>
1 parent 951b7c5 commit f1f8d2c

File tree

36 files changed

+270
-55
lines changed

36 files changed

+270
-55
lines changed

web/packages/axios/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import type {
1414
ResponseType
1515
} from './type';
1616

17+
axios.defaults.withCredentials = true;
18+
1719
function createCommonRequest<ResponseData = any>(
1820
axiosConfig?: CreateAxiosDefaults,
1921
options?: Partial<RequestOption<ResponseData>>

web/packages/utils/src/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import localforage from 'localforage';
33
/** The storage type */
44
export type StorageType = 'local' | 'session';
55

6-
export function createStorage<T extends object>(type: StorageType, storagePrefix: string) {
7-
const stg = type === 'session' ? window.sessionStorage : window.localStorage;
6+
export function createStorage<T extends object>(type: StorageType, storagePrefix: string, proxy?: any) {
7+
const stg = proxy ? proxy : (type === 'session' ? window.sessionStorage : window.localStorage);
88

99
const storage = {
1010
clear() {

web/src/App.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { WatermarkProps } from 'antd';
44

55
import { info } from '@/constants/app';
66
import { router } from '@/router';
7-
import { getLocale } from '@/store/slice/app';
7+
import { changeLocale, getLocale } from '@/store/slice/app';
88
import { getDarkMode, getThemeSettings, themeColors } from '@/store/slice/theme';
99
import { getAntdTheme, setupThemeVarsToHtml, toggleCssDarkMode } from '@/store/slice/theme/shared';
1010
import { localStg } from '@/utils/storage';
@@ -48,6 +48,13 @@ const App = () => {
4848
const locale = useAppSelector(getLocale);
4949

5050
const { antdTheme, themeSettings } = useTheme();
51+
const dispatch = useAppDispatch();
52+
53+
useEffect(() => {
54+
if (window.$wujie?.props?.locale) {
55+
dispatch(changeLocale(window.$wujie?.props?.locale));
56+
}
57+
}, [window.$wujie?.props?.locale]);
5158

5259
return (
5360
<AConfigProvider
@@ -63,7 +70,7 @@ const App = () => {
6370
{...watermarkProps}
6471
>
6572
<RouterProvider
66-
fallback={<GlobalLoading />}
73+
fallback={<GlobalLoading className={window.__POWERED_BY_WUJIE__ ? 'absolute' : ''} />}
6774
router={router}
6875
/>
6976
</AWatermark>

web/src/assets/svg-icon/folder.svg

Lines changed: 2 additions & 0 deletions
Loading

web/src/assets/svg-icon/home.svg

Lines changed: 2 additions & 0 deletions
Loading

web/src/assets/svg-icon/puzzle.svg

Lines changed: 2 additions & 0 deletions
Loading

web/src/assets/svg-icon/robot.svg

Lines changed: 2 additions & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

web/src/components/common/icon.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import React from 'react';
33

44
import FontIcon from '@/components/common/font_icon';
55

6-
const Icon = ({ className, src, style, ...rest }) => {
6+
import normalizeUrl from 'normalize-url';
7+
import { getProxyEndpoint } from '@/components/micro/utils'
8+
9+
const Icon = ({ className, src, style, server, ...rest }) => {
710
if (!src) {
811
return null;
912
}
@@ -17,9 +20,13 @@ const Icon = ({ className, src, style, ...rest }) => {
1720
/>
1821
);
1922
}
23+
let formatSrc = src
24+
if (!src.startsWith('http')) {
25+
formatSrc = normalizeUrl(`${getProxyEndpoint() || server}/${src}`)
26+
}
2027
return (
2128
<Image
22-
src={src}
29+
src={formatSrc}
2330
style={style}
2431
{...rest}
2532
preview={false}

web/src/components/micro/index.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export function setupMicro(root, renderRoot) {
2+
3+
window.__WUJIE_MOUNT = () => {
4+
renderRoot(root)
5+
};
6+
window.__WUJIE_UNMOUNT = () => {
7+
root.unmount()
8+
};
9+
window.__WUJIE.mount()
10+
11+
setupIcons();
12+
setupHistoryHooks();
13+
}
14+
15+
function setupIcons() {
16+
const parentDoc = window.$wujie?.props?.parentDocument;
17+
if (!parentDoc) return;
18+
19+
const existingMicroIcon = parentDoc.getElementById('__MICRO__SVG_ICON_LOCAL__');
20+
if (existingMicroIcon) return;
21+
22+
const sourceIcon = document.getElementById('__SVG_ICON_LOCAL__');
23+
if (!sourceIcon) return;
24+
25+
const clonedIcon = sourceIcon.cloneNode(true) as any;
26+
clonedIcon.id = '__MICRO__SVG_ICON_LOCAL__';
27+
28+
parentDoc.body.appendChild(clonedIcon);
29+
}
30+
31+
function setupHistoryHooks() {
32+
let lastUrl = window.location.href;
33+
34+
function handleUrlChange() {
35+
const currentUrl = window.location.href;
36+
if (currentUrl !== lastUrl) {
37+
lastUrl = currentUrl;
38+
window.$wujie?.props?.onRouteChange({
39+
url: currentUrl,
40+
})
41+
}
42+
}
43+
44+
const originalPushState = window.history.pushState;
45+
window.history.pushState = function(...args) {
46+
originalPushState.apply(this, args);
47+
handleUrlChange();
48+
};
49+
50+
const originalReplaceState = window.history.replaceState;
51+
window.history.replaceState = function(...args) {
52+
originalReplaceState.apply(this, args);
53+
handleUrlChange();
54+
};
55+
}

0 commit comments

Comments
 (0)