微信小程式 request請求封裝(包括登入)
阿新 • • 發佈:2019-01-11
這段時間都在開發小程式。封裝是少不了的部分。經過三輪的修改修改再修改之後,得到了下面現在一直在用的這版。如果小夥伴你只需要封裝,不考慮需不需要重新登入的話可以把if(res.data.code ==5000)這段去掉。下面wxLogin也可以去掉了(強迫症,用不到的都喜歡去掉)。由於考慮到授權問,所以又加多了詢問,這是我整個專案的都需要用的request函式。
希望對你有幫助module.exports = { formatTime: formatTime, HttpRequst: HttpRequst, } const baseUrl = "https://hi.xxxx.com/Ticket/";//測試環境 const dataUrl = "https://wp.xxxx.com/wechat/";//資料統計測試環境 //sessionChoose 1是帶sessionID的GET方法 2是不帶sessionID的GET方法, 3是帶sessionID的Post方法,4是不帶sessionID的Post方法 //ask是是否要進行詢問授權,true為要,false為不要 //sessionChoose為1,2,3,4,所以paramSession下標為0的則為空 function HttpRequst(loading, url, sessionChoose, sessionId, params, method,ask,callBack) { if (loading == true) { wx.showToast({ title: '資料載入中', icon: 'loading' }) } var paramSession = [{}, { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + sessionId }, { 'content-type': 'application/json' }, { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'JSESSIONID=' + sessionId}, { 'content-type': 'application/x-www-form-urlencoded'}] wx.request({ url: baseUrl + url, data: params, dataType: "json", header: paramSession[sessionChoose], method: method, success: function (res) { if (loading == true) { wx.hideToast();//隱藏提示框 } if (res.data.code == 5000) { wxLogin(loading, url, sessionChoose, sessionId, params, method,ask, callBack); } callBack(res.data); }, complete: function () { if (loading == true) { wx.hideToast();//隱藏提示框 } } }) } function wxLogin(loading, url, sessionChoose, sessionId, params, method,ask, callBack) { wx.login({ success: function (res) { var code = res.code;//得到code HttpRequst(true, "ztc/product/login", false, "", { "code": code }, "GET",false, function (res) { if (res.code == 200) { wx.setStorageSync('sessionId', res.sessionId); if (res.isNeedUserInfo == true) { wx.getUserInfo({ success: function (res) { HttpRequst(true, "ztc/product/saveUser", 3, wx.getStorageSync("sessionId"), { "encryptedData": res.encryptedData, "iv": res.iv }, "POST", false, function (res) { HttpRequst(loading, url, sessionChoose, wx.getStorageSync("sessionId"), params, method,ask, callBack); }) }, fail: function (res) { console.log("我還沒有授權"); if (ask == true) { wx.showModal({ title: '提示', confirmText: "授權", content: '若不授權微信登陸,則無法正常使用xxx小程式的功能;點選重新授權,則重新使用;若點選不授權,後期還使用小程式,需在微信【發現】--【小程式】--刪除【xxxx】,重新搜尋授權登陸,方可使用', success: function (res) { if (res.confirm) { console.log('使用者點選確定'); wx.openSetting({ success: function (res) { console.log(res) if (!res.authSetting["scope.userInfo"] || !res.authSetting["scope.userLocation"]) { //這裡是授權成功之後 填寫你重新獲取資料的js wx.getUserInfo({ withCredentials: false, success: function (data) { HttpRequst(true, "ztc/product/saveUser", 3, wx.getStorageSync("sessionId"), { "encryptedData": res.encryptedData, "iv": res.iv }, "POST", false, function (res) { HttpRequst(loading, url, sessionChoose, wx.getStorageSync("sessionId"), params, method, ask, callBack); }) }, fail: function () { console.info("3授權失敗返回資料"); } }); } } }) } } }) } } }) } else { HttpRequst(loading, url, sessionChoose, wx.getStorageSync("sessionId"), params, method,ask,callBack); } } }) } }) }