一现象:
utils.js
import fly from './config'
let util = {
fly: fly,
urlConfig: {
'getShop': '/apy/getShop'
}
};
util.post = async function(url, param) {
// fly.config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
// console.log("fly.config.headers1",fly.config.headers);
return await fly.post(this.urlConfig[url], param);
}
export default util;
config.js
/*
fly配置文件
by:hem
*/
//引入 fly
var Fly = require("flyio/dist/npm/wx")
var fly = new Fly;
// import config from '@/config'
//配置请求基地址
// //定义公共headers
// fly.config.headers={xx:5,bb:6,dd:7}
fly.config.timeout = 50000;
//设置请求基地址
fly.config.baseURL = 'http://localhost:8004';
fly.config.withCredentials = true;
// wx.login({
// success: res => {
// var code = res.code;
// console.log("res.code--->" +res.code);
// }
// });
fly.config.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
//添加请求拦截器
fly.interceptors.request.use((request) => {
//给所有请求添加自定义header
let token = uni.getStorageSync("Authorization");
request.headers["Authorization"] = token;
//打印出请求体
// console.log(request.body)
uni.showLoading({
title: '加载中'
});
return request;
// request.data = request.data;
//可以显式返回request, 也可以不返回,没有返回值时拦截器中默认返回request
})
//添加响应拦截器,响应拦截器会在then/catch处理之前执行
fly.interceptors.response.use(
async function(response) {
uni.hideLoading()
//只将请求结果的data字段返回
if (response.data.code == 1000) {
//token 失效
console.log("token 失效!");
return null;
} else if (response.data.code == 1002) {
wx.navigateTo({
url: '/pages/public/login'
})
}else if (response.data.code != 1) {
// window.location.href= '/pages/user/main';
// this.$router.push('/pages/user/main');
// this.$Toast.fail(response.data.msg);
setTimeout(() => {
uni.showModal({
title: '温馨提示',
content: response.data.msg
})
}, 1000);
wx.removeStorageSync("token");
return null;
} else {
return response.data.data;
}
},
(err) => {
//发生网络错误后会走到这里
uni.hideLoading()
uni.showToast({title:'服务异常,请稍后重试',icon:'none'});
return Promise.resolve(null);
}
)
// Vue.prototype.$http=fly //将fly实例挂在vue原型上
export default fly