Get the App
SLTechnology News&Howtos  ›  Development  › 

How to encapsulate WeChat Mini Programs http interceptor

Shulou Source: shulou.com Published: 2022-05-31 23:21:08 09月15日 Update

This article mainly introduces the relevant knowledge of "how to encapsulate WeChat Mini Programs http interceptor". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to encapsulate WeChat Mini Programs http interceptor" can help you solve the problem.

Step 1: create a request.js file

Step 2: determine the http, upload, and websocket prefixes

Step 3: encapsulate wx.request

Processing the http address, request headers and parameters before the request is made, parsing the return value after the response, and making basic logical judgments, with an emphasis on using the Promise object.

Step 4: export the module

Step 5: use request

Const Request = require ("/ utils/request"); / / Import module Request.post ("/ api/xcxWxLogin", {/ / call method code: res.code, encryptedData: resp.encryptedData, iv: resp.iv, shareId: share.shareId | | ", salesmanId: share.salesmanId | |", source: share.source | | "}) .then (res = > {/ / successful callback / / todo}) .catch (err = > {}); / / exception callback

Step 6: complete code for interceptor

Const apiHttp = "https://*****.com";const socketHttp =" wss://*.com/wss "; function fun (url, method, data, header) {data = data | | {}; header = header | | {}; let sessionId = wx.getStorageSync (" UserSessionId "); if (sessionId) {if (! header | |! header [" SESSIONID "]) {header [" SESSIONID "] = sessionId;}} wx.showNavigationBarLoading () Let promise = new Promise (function (resolve, reject) {wx.request ({url: apiHttp + url, header: header, data: data, method: method, success: function (res) {if (typeof res.data = = "object") {if (res.data.status) {if (res.data.status = =-200) {wx.showToast ({title: "to ensure that the most accurate service is provided to you Please exit application reauthorization ", icon:" none "}) Reject ("Please login again");} else if (res.data.status = =-201) {wx.showToast ({title: res.data.msg, icon: "none"}); setTimeout (function () {wx.navigateTo ({url: "/ pages/user/supplement/supplement"});}, 1000); reject (res.data.msg) } resolve (res);}, fail: reject, complete: function () {wx.hideNavigationBarLoading ();}});}); return promise;} function upload (url, name, filePath) {let header = {}; let sessionId = wx.getStorageSync ("UserSessionId"); / / take the information if (sessionId) {if (! header | |! header ["SESSIONID"]) {header ["SESSIONID"] = sessionId from the cache / add to the request header}} wx.showNavigationBarLoading (); let promise = new Promise (function (resolve, reject) {wx.uploadFile ({url: apiHttp + url, filePath: filePath, name: name, header: header, success: function (res) {resolve (res);}, fail: reject, complete: function () {wx.hideNavigationBarLoading ();}}); return promise } module.exports = {apiHttp: apiHttp,socketHttp: socketHttp, "get": function (url, data, header) {return fun (url, "GET", data, header);}, "post": function (url, data, header) {return fun (url, "POST", data, header);}, upload: function (url, name, filePath) {return upload (url, name, filePath);}} This is the end of the introduction on "how to encapsulate WeChat Mini Programs http interceptor". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Interceptor encapsulation program knowledge method module industry different practical successful code information content preprocessing prefix parameter address practicality actual object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Shulou Tech Info OPPO Reno NVidia