更新认证API,新增地理位置参数支持,优化登录逻辑以处理位置获取,提升用户体验和代码可维护性。
This commit is contained in:
@@ -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<AuthLoginResponse>;
|
||||
login: (username: string, password: string, latitude?: number, longitude?: number) => Promise<AuthLoginResponse>;
|
||||
register: (username: string, email: string, password: string) => Promise<AuthResponse>;
|
||||
logout: () => Promise<AuthResponse>;
|
||||
getCurrentUser: () => Promise<User>;
|
||||
@@ -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;
|
||||
|
||||
@@ -131,10 +131,10 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ children
|
||||
};
|
||||
}, [token]);
|
||||
|
||||
const handleLogin = async (username: string, password: string): Promise<void> => {
|
||||
const handleLogin = async (username: string, password: string, latitude?: number, longitude?: number): Promise<void> => {
|
||||
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;
|
||||
|
||||
@@ -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<GeolocationPosition>((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) {
|
||||
|
||||
Reference in New Issue
Block a user