From b1c71f62d3ed5b720e820a5fbcb138129b9fdb24 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Mon, 22 Apr 2024 08:56:11 +0800 Subject: [PATCH 1/2] fix chart resize --- .../admin/ana/usage-by-model-chart.tsx | 34 +++++++++++++++++-- app/app/(admin)/admin/layout.tsx | 1 + 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/app/(admin)/admin/ana/usage-by-model-chart.tsx b/app/app/(admin)/admin/ana/usage-by-model-chart.tsx index dbe591814..31c5ba05e 100644 --- a/app/app/(admin)/admin/ana/usage-by-model-chart.tsx +++ b/app/app/(admin)/admin/ana/usage-by-model-chart.tsx @@ -141,6 +141,21 @@ function EchartsComponent({ currentDate, setCurrentDate }: ComponentProps) { }; }, [currentDate, searchDate]); // 空数组作为第二个参数,表示仅在组件挂载和卸载时执行 + // useEffect(() => { + // const handleResize = () => { + // console.log("窗口大小变化"); + // let chartDom = document.getElementById("usage-by-model-chart"); + // if (!chartDom) return; + // const myChart = echarts.getInstanceByDom(chartDom); + // myChart?.resize(); + // }; + // window.addEventListener("resize", handleResize); + // return () => { + // window.removeEventListener("resize", handleResize); + // }; + // }, []); + + // #admin-page-content useEffect(() => { const handleResize = () => { console.log("窗口大小变化"); @@ -149,9 +164,24 @@ function EchartsComponent({ currentDate, setCurrentDate }: ComponentProps) { const myChart = echarts.getInstanceByDom(chartDom); myChart?.resize(); }; - window.addEventListener("resize", handleResize); + const targetNode = document.getElementById("admin-page-content"); + // 创建一个观察器实例并传入回调函数,该函数在观察到变化时执行 + const resizeObserver = new ResizeObserver((entries) => { + for (let entry of entries) { + const { width, height } = entry.contentRect; + // console.log(`Element's size: ${width}px x ${height}px`); + handleResize(); + } + }); + + // const config = { attributes: true, childList: true, subtree: true }; + targetNode && resizeObserver.observe(targetNode); + + // window.addEventListener("resize", handleResize); return () => { - window.removeEventListener("resize", handleResize); + resizeObserver.disconnect(); + + // window.removeEventListener("resize", handleResize); }; }, []); diff --git a/app/app/(admin)/admin/layout.tsx b/app/app/(admin)/admin/layout.tsx index 0ceca648e..9e737eaa0 100644 --- a/app/app/(admin)/admin/layout.tsx +++ b/app/app/(admin)/admin/layout.tsx @@ -69,6 +69,7 @@ function MainLayout({ children }: { children: ReactNode }) { /> Date: Mon, 22 Apr 2024 16:14:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=B4=A7=E5=8F=8A=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/auth.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/auth.ts b/lib/auth.ts index e58227197..2c884c102 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -94,9 +94,9 @@ export const authOptions: NextAuthOptions = { let existingUser = await existUser(user); // await insertUser(user) if (!existingUser) { // 如果不存在,则报错 - throw new Error("用户名或密码不正确") + // throw new Error("用户名或密码不正确") // 如果不存在,则创建 - // existingUser = await insertUser(user); + existingUser = await insertUser(user); } // 有密码就校验密码,没有就直接返回用户 (password || existingUser.password) && validatePassword(password, existingUser.password);