From de4a7113189939bbe51a31fb4d30545c6d6fd6b1 Mon Sep 17 00:00:00 2001 From: zyh Date: Thu, 10 Apr 2025 07:03:49 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=AE=A4=E8=AF=81API?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E5=9C=B0=E7=90=86=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81=EF=BC=8C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=80=BB=E8=BE=91=E4=BB=A5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E8=8E=B7=E5=8F=96=EF=BC=8C=E6=8F=90=E5=8D=87?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BD=93=E9=AA=8C=E5=92=8C=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=8F=AF=E7=BB=B4=E6=8A=A4=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/admin/api.ts | 16 ++++++++++------ client/admin/hooks_sys.tsx | 4 ++-- client/admin/pages_login_reg.tsx | 19 ++++++++++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/client/admin/api.ts b/client/admin/api.ts index b2aa65f..e5de79a 100644 --- a/client/admin/api.ts +++ b/client/admin/api.ts @@ -2,9 +2,8 @@ import axios from 'axios'; import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types'; import 'dayjs/locale/zh-cn'; import type { - User, FileLibrary, FileCategory, KnowInfo, - AuthContextType, ThemeContextType, Attachment, ThemeSettings, - SystemSetting, SystemSettingGroupData + User, FileLibrary, FileCategory, ThemeSettings, + SystemSetting, SystemSettingGroupData, LoginLocation, LoginLocationDetail } from '../share/types.ts'; @@ -40,7 +39,7 @@ interface AuthResponse { // 定义Auth API接口类型 interface AuthAPIType { - login: (username: string, password: string) => Promise; + login: (username: string, password: string, latitude?: number, longitude?: number) => Promise; register: (username: string, email: string, password: string) => Promise; logout: () => Promise; getCurrentUser: () => Promise; @@ -54,9 +53,14 @@ interface AuthAPIType { // Auth相关API export const AuthAPI: AuthAPIType = { // 登录API - login: async (username: string, password: string) => { + login: async (username: string, password: string, latitude?: number, longitude?: number) => { try { - const response = await axios.post(`${API_BASE_URL}/auth/login`, { username, password }); + const response = await axios.post(`${API_BASE_URL}/auth/login`, { + username, + password, + latitude, + longitude + }); return response.data; } catch (error) { throw error; diff --git a/client/admin/hooks_sys.tsx b/client/admin/hooks_sys.tsx index 01b095c..c0245f9 100644 --- a/client/admin/hooks_sys.tsx +++ b/client/admin/hooks_sys.tsx @@ -131,10 +131,10 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children }; }, [token]); - const handleLogin = async (username: string, password: string): Promise => { + const handleLogin = async (username: string, password: string, latitude?: number, longitude?: number): Promise => { try { // 使用AuthAPI登录 - const response = await AuthAPI.login(username, password); + const response = await AuthAPI.login(username, password, latitude, longitude); // 保存token和用户信息 const { token: newToken, user: newUser } = response; diff --git a/client/admin/pages_login_reg.tsx b/client/admin/pages_login_reg.tsx index 99ea7a5..55f45a7 100644 --- a/client/admin/pages_login_reg.tsx +++ b/client/admin/pages_login_reg.tsx @@ -25,7 +25,24 @@ export const LoginPage = () => { const handleSubmit = async (values: { username: string; password: string }) => { try { setLoading(true); - await login(values.username, values.password); + + // 获取地理位置 + let latitude: number | undefined; + let longitude: number | undefined; + + try { + if (navigator.geolocation) { + const position = await new Promise((resolve, reject) => { + navigator.geolocation.getCurrentPosition(resolve, reject); + }); + latitude = position.coords.latitude; + longitude = position.coords.longitude; + } + } catch (geoError) { + console.warn('获取地理位置失败:', geoError); + } + + await login(values.username, values.password, latitude, longitude); // 登录成功后跳转到管理后台首页 navigate('/admin/dashboard'); } catch (error: any) {