question
Since each request initiated by () is a different session for the server (() request first passes through the WeChat server and then arrives at our server), this will result in subsequent requests being equivalent to an unlogged state.
Solution
Save the session returned by the backend during login locally.
Then store the session in the cookie and bring it back to the server in the form of a request header
Implement code
1. Request the login interface to obtain the header["Set-Cookie"] and store it
// App({ onLaunch() { ({ // Log in // Send to the background to exchange for openId, sessionKey, unionId success: res => { ({ url: 'api/login', method: 'POST', data: { code: }, success(res) { //It must be cleared first, otherwise ['Set-Cookie'] will report an error ('sessionid') ; //Storage ['Set-Cookie'] ("sessionid", ["Set-Cookie"]) ; } }); } }); } })
2. The subsequent interface puts the stored sessionid in the cookie and brings it back to the server in the form of a request header.
// Page({ onLoad(options) { ({ url: api + '/list', method: 'GET', header: { //Put the sessionid in the cookie and bring it back to the server in the form of a request header 'cookie': ("sessionid") }, success(res) { (res); } }) } })
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.