import axios from 'axios'
let baseURL = 'http://localhost:8090'
const instance = axios.create({
baseURL: baseURL,
timeout:7000,
headers:{}
})
//请求拦截器
instance.interceptors.request.use(function(config){
config.headers.Authorization = localStorage.getItem('token') || ''
return config;
},function(error){
return Promise.reject(error);
})
//响应拦截器
instance.interceptors.response.use(function(response){
if(response.status === 200){
if(response.data && response.data.msg){
return response.data.data
}else{
alert('网络异常')
}
}
},function(error){
return Promise.reject(error)
})
export default instance
//封装api****************************
import axios from './fetch'
//首页商品列表(page,size)
export function fetchGoodList(params) {
return axios({
url:'/jd/getHotGoodList',
method:'GET',
params
})
}
//获取商品详情(good_id)
export function fetchGoodDetail(params){
return axios({
url: '/jd/getGoodDetail',
method:'GET',
params
})
}
//添加购物车
export function fetchAddToCart(data){
return axios({
url: '/jd/addToCart',
method: 'POST',
data
})
}
封装axios
最新推荐文章于 2025-05-01 19:33:49 发布