【小程序】---- 封装请求

// 不需要 token 的请求头
var headerWithoutJwt = {
  'content-type': 'application/json', // 默认值
  'x-request-from': 'wechat_applet'
}

// 当路径中带有以下字段时不添加 token
var notNeedJwtUrl = ['login']

// get 请求 function getRequest(url, data, success, fail) { wx.showLoading({ title: '请稍候...', mask: true }) wx.request({ method: 'GET', url: url, data: data, header: requestNeedJwt(url) ? headerWithJwt() : headerWithoutJwt, success: res => { wx.hideLoading() if (success && res.statusCode === 200) { success(res) } // 如果 statusCode 为 401,说明 token 失效需要刷新 if (res.statusCode === 401 && requestNeedJwt(url)) { refreshToken(function() { continueRequest('GET', url, data, success) }) } }, fail: res => { wx.hideLoading() if (fail) { fail(res) } } }) } // post 请求 function postRequest(url, data, success, fail) { wx.showLoading({ title: '请稍候...', mask: true }) wx.request({ method: 'POST', url: url, data: data, header: requestNeedJwt(url) ? headerWithJwt() : headerWithoutJwt, success: res => { wx.hideLoading() // 如果 statusCode 为 401,说明 token 失效需要刷新 if (res.statusCode === 401 && requestNeedJwt(url)) { refreshToken(function() { continueRequest('POST', url, data, success) }) return } if (success) { success(res) } }, fail: res => { wx.hideLoading() if (fail) { fail(res) } } }) } // put 请求 function putRequest(url, data, success, fail) { wx.showLoading({ title: '请稍候...', mask: true }) wx.request({ method: 'PUT', url: url, data: data, header: requestNeedJwt(url) ? headerWithJwt() : headerWithoutJwt, success: res => { wx.hideLoading() if (success && res.statusCode === 200) { success(res) } // 如果 statusCode 为 401,说明 token 失效需要刷新 if (res.statusCode === 401 && requestNeedJwt(url)) { refreshToken(function () { continueRequest('PUT', url, data, success) }) } }, fail: res => { wx.hideLoading() if (fail) { fail(res) } } }) }
// 刷新 token 成功后继续之前的请求 function continueRequest(method, url, data, continueSuccess) { wx.request({ method: method, url: url, data: data, header: requestNeedJwt(url) ? headerWithJwt() : headerWithoutJwt, success: res => { wx.hideLoading() if (continueSuccess) { continueSuccess(res) } }, fail: res => { if (fail) { fail(res) } } }) } // 调用刷新请求接口 function refreshToken(refreshSuccess) { wx.request({ method: '', url: '', data: {}, header: headerWithoutJwt, success: res => { // 需要明确这里的判断条件,导致 token 刷新仍然失败,需要重登录 if ('判断条件') { // 重新存储 token 到本地 // 继续按照回调执行接下来的步骤 if (refreshSuccess) { refreshSuccess() } } else { wx.hideLoading() // 跳转到登录页 wx.reLaunch({ url: '', }) } }, fail: res => {} }) } // 判断是否需要 token function requestNeedJwt(url) { var needJwt = true notNeedJwtUrl.forEach(keyword => { if (url.indexOf(keyword) >= 0) { needJwt = false } }) return needJwt } // 需要 token 的请求头 function headerWithJwt() { var header = { 'content-type': 'application/json', // 默认值 'x-request-from': 'wechat_applet', 'Authorization': 'Bearer ' + app.globalData.accessToken, } return header }
// 导出 module.exports
= { getRequest: getRequest, postRequest: postRequest, putRequest: putRequest }
---------------- 概括 ----------------

  • 需要token的头部和不需要token的头部
  • 判断是否需要 token
  • get/post/put请求
  • 刷新token的请求,并重新存储token
  • 刷新 token 成功后继续之前的请求
原文地址:https://www.cnblogs.com/pinkpinkc/p/13652188.html