add sms
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
import { Navigate, useLocation } from 'react-router-dom';
|
||||
|
||||
export default function AuthGuard({ children }) {
|
||||
const location = useLocation();
|
||||
const token = localStorage.getItem('token');
|
||||
//判断环境变量中的AUTH_ACCESS是否为1开启权限校验
|
||||
const authAccess = import.meta.env.AUTH_ACCESS;
|
||||
if (authAccess === '1') {
|
||||
const location = useLocation();
|
||||
const token = localStorage.getItem('token');
|
||||
|
||||
if (!token && location.pathname !== '/login') {
|
||||
return <Navigate to="/login" state={{ from: location }} replace />;
|
||||
}
|
||||
|
||||
if (!token && location.pathname !== '/login') {
|
||||
return <Navigate to="/login" state={{ from: location }} replace />;
|
||||
if (token && location.pathname === '/login') {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
}
|
||||
|
||||
if (token && location.pathname === '/login') {
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
|
||||
return children;
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";
|
||||
|
||||
import { request } from '@/utils/request';
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
@@ -137,8 +137,6 @@ const KaTeXStyle = () => (
|
||||
`}} />
|
||||
);
|
||||
|
||||
// Vite环境变量访问方式
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '';
|
||||
|
||||
const ChatUI = () => {
|
||||
//获取url参数
|
||||
@@ -182,7 +180,7 @@ const ChatUI = () => {
|
||||
|
||||
const initData = async () => {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/init`);
|
||||
const response = await request(`/api/init`);
|
||||
if (!response.ok) {
|
||||
throw new Error('初始化数据失败');
|
||||
}
|
||||
@@ -297,7 +295,7 @@ const ChatUI = () => {
|
||||
}));
|
||||
let selectedGroupAiCharacters = groupAiCharacters;
|
||||
if (!isGroupDiscussionMode) {
|
||||
const shedulerResponse = await fetch(`${API_BASE_URL}/api/scheduler`, {
|
||||
const shedulerResponse = await request(`/api/scheduler`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -325,7 +323,7 @@ const ChatUI = () => {
|
||||
setMessages(prev => [...prev, aiMessage]);
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/chat`, {
|
||||
const response = await request(`/api/chat`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import React, { useState } from 'react';
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
|
||||
import { request } from '@/utils/request';
|
||||
interface PhoneLoginProps {
|
||||
onLogin: (phone: string, code: string) => void;
|
||||
}
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '';
|
||||
|
||||
const PhoneLogin: React.FC<PhoneLoginProps> = ({ onLogin }) => {
|
||||
const PhoneLogin: React.FC<PhoneLoginProps> = ({ handleLoginSuccess }) => {
|
||||
const [phone, setPhone] = useState('');
|
||||
const [code, setCode] = useState('');
|
||||
const [countdown, setCountdown] = useState(0);
|
||||
@@ -22,7 +21,7 @@ const PhoneLogin: React.FC<PhoneLoginProps> = ({ onLogin }) => {
|
||||
|
||||
//setIsLoading(true);
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/sendcode`, {
|
||||
const response = await fetch(`/api/sendcode`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -65,7 +64,7 @@ const PhoneLogin: React.FC<PhoneLoginProps> = ({ onLogin }) => {
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}/api/login`, {
|
||||
const response = await request(`/api/login`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -80,9 +79,8 @@ const PhoneLogin: React.FC<PhoneLoginProps> = ({ onLogin }) => {
|
||||
throw new Error(data.message || '登录失败');
|
||||
}
|
||||
|
||||
// 保存 token 到 localStorage
|
||||
localStorage.setItem('token', data.data.token);
|
||||
localStorage.setItem('phone', data.data.phone);
|
||||
//执行登录成功回调
|
||||
handleLoginSuccess(data.data.token);
|
||||
|
||||
} catch (error) {
|
||||
console.error('登录失败:', error);
|
||||
|
||||
@@ -11,7 +11,7 @@ export default function Login() {
|
||||
|
||||
return (
|
||||
<div className="login-container">
|
||||
<PhoneLogin onSuccess={handleLoginSuccess} />
|
||||
<PhoneLogin handleLoginSuccess={handleLoginSuccess} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || '';
|
||||
export async function request(url: string, options: RequestInit = {}) {
|
||||
const token = localStorage.getItem('token');
|
||||
|
||||
@@ -10,8 +11,9 @@ export async function request(url: string, options: RequestInit = {}) {
|
||||
headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
const response = await fetch(`${API_BASE_URL}${url}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
@@ -27,7 +29,7 @@ export async function request(url: string, options: RequestInit = {}) {
|
||||
throw new Error('Request failed');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
return response;
|
||||
} catch (error) {
|
||||
// 如果是网络错误或其他错误,也可以处理
|
||||
console.error('Request error:', error);
|
||||
|
||||
Reference in New Issue
Block a user