Skip to content
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

Replace firame to wujie #69

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions web/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
interface Window {
// 是否存在无界
__POWERED_BY_WUJIE__?: boolean;
// 子应用公共加载路径
__WUJIE_PUBLIC_PATH__: string;
// 原生的querySelector
__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR__: typeof Document.prototype.querySelector;
// 原生的querySelectorAll
__WUJIE_RAW_DOCUMENT_QUERY_SELECTOR_ALL__: typeof Document.prototype.querySelectorAll;
// 原生的window对象
__WUJIE_RAW_WINDOW__: Window;
// 子应用沙盒实例
__WUJIE: WuJie;
// 子应用mount函数
__WUJIE_MOUNT: () => void;
// 子应用unmount函数
__WUJIE_UNMOUNT: () => void;
// 注入对象
$wujie: {
bus: EventBus;
shadowRoot?: ShadowRoot;
props?: { [key: string]: string };
location?: object;
};
}
3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"swiper": "^11.0.7",
"swiper-react": "^1.0.0",
"vite-plugin-cdn-import": "^0.3.5",
"vite-plugin-compression": "^0.5.1"
"vite-plugin-compression": "^0.5.1",
"wujie-react": "^1.0.22"
},
"devDependencies": {
"@sj-distributor/eslint-plugin-react": "^0.7.1",
Expand Down
2 changes: 1 addition & 1 deletion web/public/appsetting.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"serverUrl": "https://testsmarties.yamimeal.ca",
"tokenKey": "token",
"userNameKey": "userName",
"cameraAIBackstageDomain": ""
"cameraAIBackstageDomain": "http://localhost:5173/"
}
66 changes: 66 additions & 0 deletions web/src/icon/main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,69 @@ export const LogoutIcon = () => {
</svg>
);
};

export const LoadingSvg = () => {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24px"
height="30px"
viewBox="0 0 24 30"
>
<rect x="0" y="13" width="4" height="5" fill="#909090">
<animate
attributeName="height"
attributeType="XML"
values="5;21;5"
begin="0s"
dur="0.6s"
repeatCount="indefinite"
></animate>
<animate
attributeName="y"
attributeType="XML"
values="13; 5; 13"
begin="0s"
dur="0.6s"
repeatCount="indefinite"
></animate>
</rect>
<rect x="10" y="13" width="4" height="5" fill="#909090">
<animate
attributeName="height"
attributeType="XML"
values="5;21;5"
begin="0.15s"
dur="0.6s"
repeatCount="indefinite"
></animate>
<animate
attributeName="y"
attributeType="XML"
values="13; 5; 13"
begin="0.15s"
dur="0.6s"
repeatCount="indefinite"
></animate>
</rect>
<rect x="20" y="13" width="4" height="5" fill="#909090">
<animate
attributeName="height"
attributeType="XML"
values="5;21;5"
begin="0.3s"
dur="0.6s"
repeatCount="indefinite"
></animate>
<animate
attributeName="y"
attributeType="XML"
values="13; 5; 13"
begin="0.3s"
dur="0.6s"
repeatCount="indefinite"
></animate>
</rect>
</svg>
);
};
76 changes: 76 additions & 0 deletions web/src/pages/camera-backstage/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { useAuth } from "@/hooks/use-auth";
import { LoadingSvg } from "@/icon/main";
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import WujieReact from "wujie-react";
import { None } from "../none";

export const CameraBackstage = () => {
const { bus } = WujieReact;

const navigate = useNavigate();

const { signOut, pagePermission, isGetPermission } = useAuth();

const tokenKey = (window as any).appsettings.tokenKey;

const userNameKey = (window as any).appsettings.userNameKey;

const [preloading, setPreLoading] = useState<boolean>(true);

useEffect(() => {
const handleStorageChange = () => {
try {
const newToken = localStorage.getItem(tokenKey);

const newUserName = localStorage.getItem(userNameKey);

bus.$emit("token_refresh", newToken, newUserName);
} catch (error) {
console.log(error);
}
};

window.addEventListener("storage", handleStorageChange);

return () => window.removeEventListener("storage", handleStorageChange);
}, []);

return (
<div>
{isGetPermission ? (
pagePermission.canSwitchCameraAiBackend ? (
<>
{preloading && (
<div className="absolute inset-0 flex justify-center items-center z-10 bg-white">
<LoadingSvg />
</div>
)}

<WujieReact
width="100%"
height="100%"
name="CameraBackstage"
url={(window as any).appsettings.cameraAIBackstageDomain}
sync={true}
alive={true}
fiber={true}
activated={() => setPreLoading(false)}
props={{
userName: localStorage.getItem(userNameKey),
token: localStorage.getItem(tokenKey),
signOut: () => signOut(() => navigate("/login")),
}}
/>
</>
) : (
<None />
)
) : (
<div className="absolute inset-0 flex justify-center items-center z-10 bg-white">
<LoadingSvg />
</div>
)}
</div>
);
};
21 changes: 16 additions & 5 deletions web/src/pages/login/hook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useDebounceFn } from "ahooks";
import { useState } from "react";
import { useEffect, useState } from "react";

import { IUserInfo } from "@/dtos";
import { useAuth } from "@/hooks/use-auth";
Expand Down Expand Up @@ -74,24 +74,35 @@ export const useAction = () => {
})
.catch(() => {
hanldeNoPermission();
})
.finally(() => {
setLoginLoading(false);
});
} else {
setLoginLoading(false);
}
})
.catch(() => {
setLoginLoading(false);

message.error("登录失败,请重试");
});
} else {
message.warning("请输入正确的用户名和密码");
}

setTimeout(() => {
setLoginLoading(false);
}, 1000);
};

const { run: handleOnLogin } = useDebounceFn(onLogin, {
wait: 300,
});

useEffect(() => {
const token = localStorage.getItem((window as any).appsettings?.tokenKey);

if (token) {
signIn(token, userInfo.userName);
}
}, []);

return { userInfo, loginLoading, updateUserInfo, handleOnLogin };
};
18 changes: 1 addition & 17 deletions web/src/pages/main/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,7 @@ export const useAction = () => {

const jumpToBackstage = () => {
if (pagePermission.canSwitchCameraAiBackend) {
var myIframe = document.getElementById("myIframe") as HTMLIFrameElement;

if (myIframe && myIframe.contentWindow) {
const token = localStorage.getItem(
(window as any).appsettings?.tokenKey
);

(myIframe as any).contentWindow.postMessage(
token,
(window as any).appsettings?.cameraAIBackstageDomain
);

window.open(
(window as any).appsettings?.cameraAIBackstageDomain,
"_blank"
);
}
window.open(`/backstage`, "_blank", "noopener,noreferrer");
} else {
message.warning("暫無權限切換後台");
}
Expand Down
5 changes: 0 additions & 5 deletions web/src/pages/main/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,6 @@ export const Main = () => {
<span className="text-sm">{item.name}</span>
</div>
))}
<iframe
id="myIframe"
src={(window as any).appsettings?.cameraAIBackstageDomain}
style={{ display: "none" }}
/>
</div>
</div>
)}
Expand Down
9 changes: 9 additions & 0 deletions web/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ReplayList } from "@/pages/replay/component/replay-list";
import { Warning } from "@/pages/warning";
import { Equipment } from "@/pages/warning/component/equipment";
import { WarningList } from "@/pages/warning/component/warning-list";
import { CameraBackstage } from "@/pages/camera-backstage";

export interface IRouteItem {
path: string;
Expand Down Expand Up @@ -138,6 +139,14 @@ export const Router = () => {
<Routes>
<Route path="/login" element={<Login />} />
<Route path="*" element={<Navigate to={"/home"} />} />
<Route
path="/backstage"
element={
<AuthStatus>
<CameraBackstage />
</AuthStatus>
}
/>
<Route
element={
<AuthStatus>
Expand Down