Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encapsulate WeChat Mini Programs http interceptor

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report