43 lines
861 B
JavaScript
43 lines
861 B
JavaScript
![]() |
// src/utils/ai_request.js
|
||
|
import axios from 'axios'
|
||
|
import {
|
||
|
getToken
|
||
|
} from './auth'
|
||
|
|
||
|
const service = axios.create({
|
||
|
// baseURL: 'http://localhost:9090/dev-api/aitutor/aichat',
|
||
|
baseURL: 'http://localhost:8088/aitutor/aichat',
|
||
|
// baseURL: 'http://localhost:8080/aitutor/aichat',
|
||
|
timeout: 15000,
|
||
|
})
|
||
|
|
||
|
// 请求拦截器:统一加 token
|
||
|
service.interceptors.request.use(
|
||
|
config => {
|
||
|
const token = getToken()
|
||
|
if (token) {
|
||
|
config.headers.Authorization = `Bearer ${token}`
|
||
|
}
|
||
|
return config
|
||
|
},
|
||
|
error => Promise.reject(error)
|
||
|
)
|
||
|
|
||
|
// 响应拦截器
|
||
|
service.interceptors.response.use(
|
||
|
res => res.data,
|
||
|
err => {
|
||
|
if (err.response?.status === 401) {
|
||
|
uni.showToast({
|
||
|
title: '未登录或 token 失效',
|
||
|
icon: 'none'
|
||
|
})
|
||
|
uni.navigateTo({
|
||
|
url: '/pages/login/index'
|
||
|
})
|
||
|
}
|
||
|
return Promise.reject(err)
|
||
|
}
|
||
|
)
|
||
|
|
||
|
export default service
|