Preface in the use of WeChat public sweep code number login, there is a problem has been very bother me, be WeChat sweep yards after success, mobile WeChat will jump to me on the public platform set redurect_url, response, and the interface to otherwise WeChat client appear blank page, and search a lot of relevant information, but is very tortuous and useless
1. Online solutions (all are pits, don’t step on them)
- By using the window object of the browser, and then closing the browser, what they mean is, respond to a page on the back end, and then close the browser using the window in JS
<script>
window.close()
/ / and
window.go(-1)
</script>
Copy the code
- Error reading
- Error 1: there is no window object in the server response, so if you use some view engine, such as EJS, then window is undefined directly
- Error 2. If the response itself is an HTML file, then js can execute the code because the page is running in the browser, but let’s not overlook the problem, which is
Wechat browser
Ah, it’s a hybrid app, so Windows can’t close this page
2.weixinJSBridge
This is also an API that gives me inspiration. Generally, there are some built-in global objects in a hybrid app to help us call the API of the device, such as the Plus object in uni, so theoretically weixinJSBridge is also a built-in global object in wechat. The solution on the Internet is as follows
weixinJSbRridge.call('closeWindow')
Copy the code
I thought this was the beginning of the day, but the reality is harsh, because I looked through the official documentation, and there is no document introducing weixinJsBridge as a global object, so this is definitely not going to work
3. The dawn
Experienced by search, I decided to calm down and have a good view WeChat public document, first of all I’m number based on WeChat public OAUTH2.0 development WeChat scan code login (someone may ask why not directly use WeChat open platform directly provide login, the answer is – poor, can only be stealing chickens, curve development) and then based on the former development experience, I saw a thing called JS-SDK, document address ushered in the dawn, first OF all, I configured the wechat JS security interface domain name first login wechat public platform into the “public number Settings” “function Settings” fill in the “JS interface security domain name, the next everything is very smooth
4. Solutions
Js-sdk is specially used for the development of wechat web pages, so I responded to an EJS view in the callback interface. The wechat document has a lot of text, which is annoying, but it is really detailed, as long as you calm down and read it
- Firstly, the CDN address of the SDK is introduced through script in the view layer
<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
Copy the code
- The next step is simple. According to the documentation, all we need to do is call the API that closes the page
<script>
wx.closeWindow();
</script>
Copy the code
- In particular, the call to wX.closeWindow () needs to be run in setTimeout because access to the CDN takes some time
<div>
<img src= < % =headimgurl% > style="margin-top:100px" alt="" width="100">
<br>
<h3>Welcome to <%= nickname %></h4>
<img src="/imgs/bg2.png" alt="" style="margin-top:100px; opacity:.5" width="250">
<br />
</div>
<script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script>
setTimeout(function(){
wx.closeWindow();
},100)
</script>
Copy the code
Do call it a day