let config = require('./config.js');
|
|
import functions from './functions.js';
|
|
function request(url, param = {}, way = 'POST', contentType = true) {
|
|
// 获取保存的token
|
|
let token = uni.getStorageSync("token");
|
|
let data = param || {} ;
|
|
let header = {};
|
|
|
|
if (contentType == true) {
|
|
header = {
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
"X-Access-Token": token
|
|
}
|
|
} else {
|
|
header = {
|
|
"content-type": "application/json;charset=UTF-8",
|
|
"X-Access-Token": token
|
|
}
|
|
}
|
|
console.log(token)
|
|
// console.info("config",config)
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: config.API_URL + url,
|
|
data: data,
|
|
header: header,
|
|
method: way,
|
|
success: (response) => {
|
|
let result = response.data;
|
|
if (result.code == 200) {
|
|
resolve(result);
|
|
}else if (result.code == 401) {
|
|
if(url == 'info/info' && data.pages == 'home'){
|
|
// resolve(result);
|
|
}else{
|
|
uni.showToast({
|
|
icon:'none',
|
|
title:'登录过期,请重新登陆'
|
|
})
|
|
setTimeout(()=>{
|
|
uni.clearStorageSync()
|
|
uni.navigateTo({
|
|
url: "/pages/login/login"
|
|
})
|
|
},1000)
|
|
}
|
|
}else if (result.code == 500) {
|
|
functions.showToast(result.message)
|
|
if(result.message.indexOf("token") > -1){
|
|
setTimeout(()=>{
|
|
uni.clearStorageSync()
|
|
uni.navigateTo({
|
|
url: "/pages/login/login"
|
|
})
|
|
},1000)
|
|
}
|
|
resolve(result);
|
|
} else {
|
|
reject(result);
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
functions.showToast('数据加载异常!');
|
|
}
|
|
});
|
|
})
|
|
}
|
|
export default request
|