后台
This commit is contained in:
133
src/api/login.js
133
src/api/login.js
@@ -1,80 +1,75 @@
|
||||
import axios from 'axios'
|
||||
import axiosInstance from './axiosConfig';
|
||||
import { loginSuccess } from '../utils/jwt';
|
||||
|
||||
const API_BASE_URL = 'http://zybdatasupport.online:8000'
|
||||
// const API_BASE_URL = 'http://zybdatasupport.online:8000' // 不再需要
|
||||
|
||||
// 创建 axios 实例
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
timeout: 10000 // 添加超时设置
|
||||
})
|
||||
// // 创建 axios 实例 // 不再需要
|
||||
// const axiosInstance = axios.create({
|
||||
// baseURL: API_BASE_URL,
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json',
|
||||
// 'Accept': 'application/json',
|
||||
// 'X-Requested-With': 'XMLHttpRequest'
|
||||
// },
|
||||
// timeout: 10000 // 添加超时设置
|
||||
// })
|
||||
|
||||
// 设置请求拦截器,自动添加 token
|
||||
axiosInstance.interceptors.request.use(
|
||||
config => {
|
||||
const token = localStorage.getItem('access_token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
// // 设置请求拦截器,自动添加 token // 不再需要
|
||||
// axiosInstance.interceptors.request.use(
|
||||
// config => {
|
||||
// const token = localStorage.getItem('access_token')
|
||||
// if (token) {
|
||||
// config.headers.Authorization = `bearer ${token}`
|
||||
// }
|
||||
// return config
|
||||
// },
|
||||
// error => {
|
||||
// return Promise.reject(error)
|
||||
// }
|
||||
// )
|
||||
|
||||
// 添加响应拦截器
|
||||
axiosInstance.interceptors.response.use(
|
||||
response => response,
|
||||
error => {
|
||||
if (error.response) {
|
||||
// 服务器返回错误状态码
|
||||
console.error('请求错误:', {
|
||||
status: error.response.status,
|
||||
data: error.response.data,
|
||||
config: error.config
|
||||
})
|
||||
} else if (error.request) {
|
||||
// 请求已发出但没有收到响应
|
||||
console.error('网络错误:', error.request)
|
||||
} else {
|
||||
// 请求配置出错
|
||||
console.error('请求配置错误:', error.message)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
// // 添加响应拦截器 // 不再需要
|
||||
// axiosInstance.interceptors.response.use(
|
||||
// response => response,
|
||||
// error => {
|
||||
// if (error.response) {
|
||||
// // 服务器返回错误状态码
|
||||
// console.error('请求错误:', {
|
||||
// status: error.response.status,
|
||||
// data: error.response.data,
|
||||
// config: error.config
|
||||
// })
|
||||
// } else if (error.request) {
|
||||
// // 请求已发出但没有收到响应
|
||||
// console.error('网络错误:', error.request)
|
||||
// } else {
|
||||
// // 请求配置出错
|
||||
// console.error('请求配置错误:', error.message)
|
||||
// }
|
||||
// return Promise.reject(error)
|
||||
// }
|
||||
// )
|
||||
|
||||
export const userLogin = async (username, password, server, token) => {
|
||||
try {
|
||||
console.log('登录请求参数:', { username, password, server, token })
|
||||
// console.log('登录请求参数:', { username, password, server, token }); // 保留此调试日志以备将来使用,或按需移除
|
||||
const response = await axiosInstance.post('/user/login', {
|
||||
username,
|
||||
password,
|
||||
server,
|
||||
token
|
||||
})
|
||||
});
|
||||
|
||||
// 保存 token 到 localStorage
|
||||
if (response.data.access_token) {
|
||||
localStorage.setItem('access_token', response.data.access_token)
|
||||
loginSuccess(response.data.access_token, username); // 使用 username 作为 userId
|
||||
}
|
||||
|
||||
return response.data
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('登录失败:', {
|
||||
status: error.response?.status,
|
||||
data: error.response?.data,
|
||||
message: error.message,
|
||||
config: error.config
|
||||
})
|
||||
throw error
|
||||
// 错误将由响应拦截器统一处理和记录,这里可以直接抛出
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const userRegister = async (qq_code, password, server, token) => {
|
||||
try {
|
||||
@@ -83,20 +78,14 @@ export const userRegister = async (qq_code, password, server, token) => {
|
||||
password,
|
||||
server,
|
||||
token
|
||||
}
|
||||
console.log('注册请求URL:', `${API_BASE_URL}/user/register/`)
|
||||
};
|
||||
// console.log('注册请求参数:', requestData); // 保留此调试日志以备将来使用,或按需移除
|
||||
|
||||
const response = await axiosInstance.post('/user/register', requestData)
|
||||
console.log('注册响应数据:', response.data)
|
||||
return response.data
|
||||
const response = await axiosInstance.post('/user/register', requestData);
|
||||
// console.log('注册响应数据:', response.data); // 保留此调试日志以备将来使用,或按需移除
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('注册请求失败:', {
|
||||
status: error.response?.status,
|
||||
data: error.response?.data,
|
||||
message: error.message,
|
||||
url: error.config?.url
|
||||
})
|
||||
throw error
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user