1. 程式人生 > 實用技巧 >【小程式】---- 封裝請求

【小程式】---- 封裝請求

// 不需要 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 }