From a3909bfca0f8857d70bd240debc580ad63e54a07 Mon Sep 17 00:00:00 2001 From: ZLY Date: Thu, 20 Nov 2025 10:01:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(login):=20=E6=9B=B4=E6=96=B0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=B5=81=E7=A8=8B=E4=BB=A5=E6=94=AF=E6=8C=81SSO?= =?UTF-8?q?=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改.env文件指向新的后端本地环境地址 - 在登录表单中引入ssoCallBack和verifyOld接口 - 实现登录成功后的token存储逻辑 - 调整登录验证逻辑,优先使用新SSO验证接口 - 添加SSO回调处理函数,用于获取并存储认证信息 --- .env | 5 ++++- src/api/sso.ts | 32 +++++++++++++++++++++++++++++++- src/pages/login/form.tsx | 16 ++++++++++++---- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/.env b/.env index 41e7780..0fd4e04 100644 --- a/.env +++ b/.env @@ -11,4 +11,7 @@ NEXT_PUBLIC_DEV_CODE_SERVER_HOST='http://192.168.5.119:8443' #NEXT_PUBLIC_DEV_SERVER_HOST='http://192.168.5.129:80' # 开发环境-119 -NEXT_PUBLIC_DEV_SERVER_HOST='http://192.168.5.119:80' \ No newline at end of file +#NEXT_PUBLIC_DEV_SERVER_HOST='http://192.168.5.119:80' + +# 后端本地环境 +NEXT_PUBLIC_DEV_SERVER_HOST='http://192.168.5.158:8103' \ No newline at end of file diff --git a/src/api/sso.ts b/src/api/sso.ts index dbd283b..93bce4b 100644 --- a/src/api/sso.ts +++ b/src/api/sso.ts @@ -33,7 +33,7 @@ export const ssoLogin = ({ username, password }: Record) => { }); }; -export const verify = ({ username, password }): Promise => { +export const verifyOld = ({ username, password }): Promise => { return fetch('/api/user-center/verify', { method: 'POST', headers: { @@ -48,4 +48,34 @@ export const verify = ({ username, password }): Promise => { } return data; }); +}; + +export const verify = ({ username, password }): Promise => { + return fetch('/api/sso/verify', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ username, password: password }) + }).then(async (res) => { + const data = await res.json(); + return data; + }); +}; + + +export const ssoCallBack = ({ ticket, redirectUrl }) => { + const params = new URLSearchParams({ ticket, redirectUrl }); + return fetch(`/api/sso/callback?${params.toString()}`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }).then(async (res) => { + const data = await res.json(); + if (data && res.status === 200) { + localStorage.setItem('userStatus', 'login'); + } + return data; + }); }; \ No newline at end of file diff --git a/src/pages/login/form.tsx b/src/pages/login/form.tsx index 9f0a9d8..f366e12 100644 --- a/src/pages/login/form.tsx +++ b/src/pages/login/form.tsx @@ -9,11 +9,12 @@ import { import { FormInstance } from '@arco-design/web-react/es/Form'; import { IconLock, IconUser } from '@arco-design/web-react/icon'; import React, { useEffect, useRef, useState } from 'react'; -import { ssoLogin, verify } from '@/api/sso'; +import { ssoCallBack, ssoLogin, verify, verifyOld } from '@/api/sso'; import useStorage from '@/utils/useStorage'; import useLocale from '@/utils/useLocale'; import locale from './locale'; import styles from './style/index.module.less'; +import { setToken } from '@/utils/auth'; export default function LoginForm() { const formRef = useRef(); @@ -36,8 +37,10 @@ export default function LoginForm() { } // 记录登录状态 localStorage.setItem('userStatus', 'login'); + // 保存Token + setToken(token as string); // 跳转首页 - // window.location.href = '/dashboard/workplace'; + window.location.href = '/dashboard/workplace'; } async function loginRequest(params) { @@ -46,8 +49,13 @@ export default function LoginForm() { try { const res = await verify(params as any); if (res.code === 200) { - await ssoLogin(params); - afterLoginSuccess(params); + if (res.data.code === 200) { + const tokenData = await ssoCallBack(res.data); + afterLoginSuccess(params, tokenData.data); + } + else { + setErrorMessage(res.msg); + } } else { setErrorMessage(res.msg);