49 lines
1.0 KiB
JavaScript
49 lines
1.0 KiB
JavaScript
// ajax.js
|
|
|
|
// 引入 uni-ajax 模块
|
|
import ajax from '@/uni_modules/u-ajax'
|
|
import store from '@/store/index.js';
|
|
// 创建请求实例
|
|
const instance = ajax.create({
|
|
// 初始配置
|
|
baseURL: 'https://dev.xixingwl.cn/api'
|
|
})
|
|
|
|
// 添加请求拦截器
|
|
instance.interceptors.request.use(
|
|
config => {
|
|
if (store.getters.getuserIsLogin) {
|
|
config.header['token'] = store.state.user.token
|
|
}
|
|
// 在发送请求前做些什么
|
|
return config
|
|
},
|
|
error => {
|
|
// 对请求错误做些什么
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
// 添加响应拦截器
|
|
instance.interceptors.response.use(
|
|
response => {
|
|
if (response.statusCode == 200) {
|
|
let res = response.data;
|
|
if (res.hasOwnProperty('token') && res.token!= "")
|
|
uni.setStorageSync('token', res.token);
|
|
return response.data
|
|
}
|
|
// 对响应数据做些什么
|
|
console.log(response)
|
|
|
|
return response.data
|
|
},
|
|
error => {
|
|
// 对响应错误做些什么
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
// 导出 create 创建后的实例
|
|
export default instance
|