You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.9 KiB

const request = (options) => {
// 在这里可以对请求头进行一些设置
// 例如:
return new Promise((resolve, reject) => {
let token = uni.getStorageSync('token')
uni.request({
url: options.url || '',
method: options.method || 'GET',
data: options.data || {},
header: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + token
},
success: (res) => {
// console.log(res.data,'==========request========='); // 控制台显示数据信息
resolve(res)
if(res.data.code == 401){
uni.showToast({
title: '您未登录请登录操作',
duration: 2000,
icon: 'none',
success: () => {
// setTimeout(() => {
// uni.reLaunch({
// url: '/pages/userCenter/userCenter'
// })
// }, 1000)
}
});
return;
}else{
// uni.request({
// url: 'https://******', //检查token时间
// method: 'GET',
// data: {
// token: uni.getStorageSync('token_key')
// },
// success: (res) => {
// // console.log(res.data);
// console.log("time=>", res.data.data.expires_in)
// if (res.data.data.expires_in < 86400 ) {
// console.log('刷新token')
// // 刷新token
// uni.request({
// url: 'https://*******', //刷新token
// data: {
// token: uni.getStorageSync('token_key')
// },
// success: (res) => {
// console.log(res.data);
// uni.setStorageSync('token_key', res.data.data.token)
// console.log('刷新完无需跳转页面')
// }
// });
// }
// }
// });
}
},
fail: (err) => {
// 页面中弹框显示失败
uni.showToast({
title: '请求接口失败',
icon: "none"
})
// 返回错误消息
reject(err)
},
catch: (e) => {
console.log(e);
}
})
});
}
uni.$request=request