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 manage session developed by WeChat Mini Programs

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to manage session developed by WeChat Mini Programs". In daily operation, I believe many people have doubts about how to manage session developed by WeChat Mini Programs. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to manage session developed by WeChat Mini Programs". Next, please follow the editor to study!

In the development of WeChat Mini Programs, each request initiated by wx.request () is a different session for the server. WeChat Mini Programs will not bring the session information back to the server, that is, the different session of the corresponding server. Because session is used in the project to store user information, subsequent requests are equivalent to non-login.

Note that the session here is not the session maintained by Mini Program through the wx.login () method, but our own server-side session.

Because under normal circumstances, when the client initiates a request to the server, the session information is stored in cookie and brought back to the server in the form of a request header, and the specific information in the request header is the id of session

The red mark is that we need to bring the request header back to the server on the request, so what we need is the value of this JESSIONID. As it happens, WeChat Mini Programs also provided support for the request header.

Solution: 1. When the user logs in, the server returns the user's sessionId.

  2 and Mini Program save sessionId and carry sessionId in the request header in each subsequent request

Server key code:

The method of getting sessionId, which can be easily obtained as long as you get the request object.

Sting sessionId = request.getSession () .getId ()

Return sessionId

. / / the business code at login

Response.getWriter.write (sessionId); / / return the sessionId to the front desk

After logging in on Mini Program, sessionId is stored in global variables. App.js is a good place to store global variables.

. / / logical processing after login

GetApp () .globalData.header.Cookie =\ 'JSESSIONID=\' + _ data.sessionId

Code in app.js

GlobalData: {

Header: {\ 'Cookie\':\'\'} / / you can also add other required request headers here, such as\ 'x-requested-with\':\ 'XMLHttpRequest\' means ajax submission, which will not be included in Wechat's request.

}

Bring sessionId to the request from Mini Program.

Var header = getApp () .globalData.header; / / get the request header in app.js

Wx.request ({

Url: localhost:8080/xx/xx

Header: header, / / bring this request header to the request

Success:function (res) {

  }

}

It is recommended to encapsulate your own request information and deal with some general logic.

At this point, the study on "how to manage session developed by WeChat Mini Programs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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