Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | // src/api/auth.js
|
| 2 | import axios from 'axios';
|
| 3 |
|
| 4 | // 创建并导出 axios 实例
|
| 5 | export const api = axios.create({
|
| 6 | baseURL: 'http://localhost:8088',
|
| 7 | timeout: 5000,
|
| 8 | });
|
| 9 |
|
| 10 | // 请求拦截器
|
| 11 | api.interceptors.request.use(config => {
|
| 12 | const token = localStorage.getItem('token');
|
| 13 | if (token) {
|
| 14 | config.headers.Authorization = `Bearer ${token}`;
|
| 15 | }
|
| 16 | return config;
|
| 17 | });
|
| 18 |
|
| 19 | export const login = async (username, password) => {
|
| 20 | const response = await api.post('/user/login', null, {
|
| 21 | params: { username, password }
|
| 22 | });
|
| 23 | if (response.data.code === 200 && response.data.data.token) {
|
| 24 | localStorage.setItem('token', response.data.data.token);
|
| 25 | }
|
| 26 | return response;
|
| 27 | };
|
| 28 |
|
| 29 | export const register = (username, password, code) => {
|
| 30 | return api.post('/user/regist', null, {
|
| 31 | params: { username, password, code }
|
| 32 | });
|
| 33 | };
|
| 34 |
|
22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame^] | 35 |
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 36 | export const getUserInfo = (token) => {
|
22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame^] | 37 | return api.get('/user/info', { params: { token } });
|
| 38 | };
|
| 39 |
|
| 40 | // // 修改你的 API 请求
|
| 41 | // export const getUserInfo = () => {
|
| 42 | // const token = localStorage.getItem('token');
|
| 43 | // if (!token) {
|
| 44 | // throw new Error("Token 不存在");
|
| 45 | // }
|
| 46 |
|
| 47 | // return api.get('/user/info', {
|
| 48 | // headers: {
|
| 49 | // 'Authorization': `Bearer ${token}` // 必须带 Bearer 前缀
|
| 50 | // }
|
| 51 | // });
|
| 52 | // };
|