Does WeChat Mini Programs have a global AJAX interceptor similar to Vue.http.interceptors.push?

  • recently there is a requirement to use token for login authentication of WeChat Mini Programs"s users.

  • I pass token to the backend through a custom field in req.header.

so after each AJAX, you need to determine whether a token? is returned in the response.header.

1. If it is not returned, it is not the first time that token has logged in and token has not expired
2. If a token is returned to me, I assign the token to me in a custom field in the request.header.

but I found that there are some AJAX interceptors in Vue or JQ, which can hijack AJAX, globally. If there is no similar interceptor in WeChat Mini Programs, will you have to write a lot of repetitive code?

I hope the seniors who already have some experience will give me some suggestions, thank you! Bow!

Mar.23,2021

just encapsulate a layer on the basis of wxrequest.
encapsulates the common logic you need into your own request module.
all requests are processed through this module. Those so-called interceptors do not let all requests be processed through it


I use localstorage, to intercept globally. After all, token is still timely. Update token


in localstorage through each ajax

you can package yourself with Wechat's request. If you don't want to repeat the wheel, you can directly introduce the fly http component. The axios, compatibility similar to vue Mini Program is still very small. Please refer to my article:
http://callmesoul.cn/xiao-che.


you can refer to the configuration in WePY and set it in app.wpy.
/ / Unified intercept request request configuration

    this.intercept('request', {
    // 
        config (p) {
             
            return p;
        },
    });
Menu