request.js 2.11 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
export const BASE_UL=`http://192.168.0.15:8080/wechat`

function subtractMinutes(timestamp, minutes) {
// / 将时间戳转换为毫秒
let milliseconds = timestamp * 1000;
// 将分钟数转换为毫秒
let millisecondsToSubtract = minutes * 60 * 1000;
// 执行减法操作
let newTimestamp = milliseconds + millisecondsToSubtract;
// 将结果转换回秒
return Math.floor(newTimestamp / 1000);
}
export const request =(options)=>{
return new Promise((resolve,reject)=>{
let sess_token = uni.getStorageSync('token')
let openid = uni.getStorageSync('openid')
let newDate = uni.getStorageSync('newdata')
uni.request({
url:BASE_UL+options.url,
method: options.method||"GET",
header:{
Authorization: sess_token,
},
data:options.data||{},
success:(res)=>{
const data = res.data
if(data.errcode === 0){
const pageStack = getCurrentPages()
const currentPage = pageStack.pop()
uni.setStorageSync('currentPage',currentPage.route)
// 获取当前时间
const data1 = new Date().getTime()
console.log(data1,'data1');
console.log(newDate,'newDate');
if(newDate){
if(data1 > newDate){
uni.request({
url:BASE_UL+'/refreshToken',
method:"GET",
header: {
authorization: sess_token,
openid: openid
},
success(res) {
const data = res.data
uni.setStorageSync('token',`${'Bearer '+data.data}`)
const newdata = subtractMinutes(new Date().getTime(),300000000 / 1000 / 60 - 60)
uni.setStorageSync('newdata',newdata)
}
})
}
}
resolve(data.data)
} else {
uni.showToast({
icon:'error',
title: data.errmsg,

})
uni.navigateTo({
url:'/pages/index/index'
})
uni.clearStorageSync();
}
},
fail:(error) =>{
uni.showToast({
icon:'error',
title: error
})
uni.clearStorageSync();
uni.navigateTo({
url:'/pages/index/index'
})
reject(error)
}
})
})
}