Home >Web Front-end >JS Tutorial >Use of WeChat Attendance Cookies for WeChat Enterprise Account Development_javascript skills
In the previous article I introduced you to the Baidu map positioning of WeChat attendance for WeChat enterprise account development. Next, learn the use of WeChat attendance cookies for WeChat enterprise account development through this article. The specific content is as follows.
Use WeChat attendance, every time you use WeChat enterprise account development: The relationship between WeChat user information and the session of the web page. This method calls the WeChat interface, which is a bit slow. WeChat official also recommends the use of Cookies, but how? Using Cookies, I have never figured it out.
I originally thought that there are two ways to obtain client data on the server. One is to put the query string on the URL, and the other is to put it in the form and post submission. I have used it before but mainly It is used on the client side. It has never been possible to submit the data in Cookies directly to the server. Even if it is, it is by reading the data in Cookies and putting it into the hidden field in the form, and then posting it to the server.
Obviously, WeChat attendance is actually a URL. In the process of entering the URL, there is no process of posting data. Only after entering the URL can you submit it through the user or ajax. In short, it seems that there is no way to directly submit the data in Cookies to the server. There seems to be an impasse. So I studied Cookies again and found that Cookies seemed to be actively submitted to the server, but they were submitted in a different location than posts. Of course, I didn't find the relevant documentation, but I discovered it through testing. As long as you set cookies yourself, cookies will be submitted every time you enter the URL, and the value of cookies can naturally be read on the server. Only then did I really understand the real implementation principle of remembering passwords. It is not that reading the value of Cookies, putting it into a hidden field, and then submitting it to the server through ajax will eliminate the need to log in.
You can see that the Cookies data is sent to the server, and the session ID is also transmitted to the server through Cookies.
Front-end js reading, method of setting Cookies:
function setCookie(name, value) {//两个参数,一个是cookie的名子,一个是值 var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); //new Date("December 31, 9998"); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } function getCookie(name) {//取cookies函数 var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return unescape(arr[2]); return null; }
CSharp server operation Cookies:
Set Cookies
HttpCookie cookie = new HttpCookie("UserCode", username); cookie.Expires = DateTime.Now.AddDays(10);// (365 * 24 * 3600); this.Response.AppendCookie(cookie); HttpCookie cookieDeviceId = new HttpCookie("DeviceId", rt.DeviceId); cookieDeviceId.Expires = DateTime.Now.AddDays(10);// (365 * 24 * 3600); this.Response.AppendCookie(cookieDeviceId);
Read Cookies:
HttpCookie ttHttpCookie = this.Request.Cookies.Get("UserCode"); HttpCookie ttHttpCookieDeviceId = this.Request.Cookies.Get("DeviceId"); string code = Request.QueryString["code"]; if (ttHttpCookie == null || ttHttpCookieDeviceId == null) { WeiApi(code); } else { string username = ttHttpCookie.Value; string DeviceId = ttHttpCookieDeviceId.Value; if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(DeviceId)) { WeiApi(code); } else { new AppException("读取Cookies UserCode=" + username + ",DeviceId=" + DeviceId); initSession(username, DeviceId); } }
The above content gives Everyone introduced the use of WeChat attendance cookies for WeChat enterprise account development. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!