fly
fly copied to clipboard
小程序处理token失效
import {
tokenName,
timestamp,
clientID,
clientVersion,
baseUrl,
urlNotNeedToken
} from './consts'
import { store } from './store'
import Fly from 'flyio/dist/npm/wx'
const fly = new Fly()
fly.config.baseURL = `${baseUrl}`
fly.config.headers.clientID = `${clientID}`
fly.config.headers.timestamp = `${timestamp}`
fly.config.headers.clientVersion = `${clientVersion}`
fly.interceptors.request.use(config => {
if (typeof config.body === 'object') {
let data = ''
for (let key in config.body) {
if (data) {
data += '&'
}
data +=
encodeURIComponent(key) + '=' + encodeURIComponent(config.body[key])
}
config.body = data
}
if (urlNotNeedToken.includes(config.url)) {
return config
} else {
let token
token = wx.getStorageSync(tokenName)
if (token) {
config.headers[tokenName] = `${token}`
}
}
return config
})
fly.interceptors.response.use(
function(response) {
if (response.data.error === 1010) {
this.lock()
return store.dispatch('login').then(res => {
this.unlock()
wx.setStorageSync(tokenName, res)
return fly.request(response.request)
})
} else {
return response.data
}
},
function(error) {
console.log('error', error)
throw error
}
)
export const wxLogin = params => fly.post('/wxlogin', params)
export const updateUser = params => fly.post('/user/update', params)
代码如上,在拦截请求时,用了lock,但不知道为什么解不了锁。
嗯?没人回复吗?