⼩程序:FLYIO,获取Token完整代码安装
⾸先安装flyio,通过命令:
npm install flyio -S
例如在request.js中:
import Fly from'flyio/dist/npm/wx'
var fly =new Fly()
var newfly =new Fly()
请求配置选项
请求配置可以通过fly中的config来设置,例如⼀些通⽤配置如下:
//定义公共headers
'content-type':'application/json',
'token':'xxxxxx'
}
//设置超时
//设置请求基地址
import Fly from'flyio/dist/npm/wx'
var fly =new Fly()
var newfly =new Fly()
quest.use(async function(request){
request.timeout =30000
StorageSync('token')&& wx.getStorageSync('token_expiration')> Date.parse(new Date())){
request.headers ={
'content-type':'application/json',
'token': wx.getStorageSync('token')
}
}else{
fly.lock()
const login =await getToken()
wx.setStorageSync('token', login.data.data)
wx.setStorageSync('token_expiration', Date.parse(new Date())+50000)
request.headers ={
'content-type':'application/json',
'token': login.data.data
}
fly.unlock()
}
return request
})
sponse.use(async(response)=>{
return response
},(err)=>{
console.log(err)
var title =''
if(err.status ===0){
title ='⽹络连接异常'
}else if(err.status ===1){
}else if(err.status ===1){
title ='⽹络连接超时'
}else if(err.status ===401){
title ='登录超时,请重试'
}else{
title = ssage ? ssage :'请求数据失败,请稍后再试' }
return wx.showToast({
title: title,
icon:'none',
duration:2000
})
})
const getToken =async()=>{
return new Promise((resolve)=>{
error parse new
wx.login({
async success(loginRes){
console.log(loginRes)
de){
('login/getToken',{
code: de
}).then((flyReq)=>{
console.log('token请求成功'+ flyReq)
resolve(flyReq)
})
}else{
console.log('登录失败!'+ Msg)
}
}
})
})
}
export default fly

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。