微信二维码登录原理

在电脑上使用微信时,你可能已经发现微信不提供传统的账号密码登陆,取而代之的是通过扫描二维码进行登陆。今天就要研究下次登陆方式微信时如何实现的?

创新互联专注于黄山区网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供黄山区营销型网站建设,黄山区网站制作、黄山区网页设计、黄山区网站官网定制、微信小程序服务,打造黄山区网络公司原创品牌,更为您提供黄山区网站排名全网营销落地服务。

1、每次用户打开PC端登陆请求,系统返回一个***的uid,并将uid的信息绘制成二维码返回给用户。这里的uid一定是***的,否则就会造成你登陆了其他用户的账号或者其他用户登陆你的账号。

2、当用户使用登陆后的微信扫描该二维码的时候,会将这个uid和手机上的微信账号及密码产生的token进行绑定,并上传到服务器。

3、WEB通过JS不断的向后端发起请求,查询有没有关于uid的登陆记录(uid和token是否存在于服务器上)。实现代码可以从微信页面获取:

 
 
  1. function _poll(_asUUID) { 
  2.         var _self = arguments.callee, 
  3.             _nTime = 0; 
  4.         _sCurUUId = _asUUID; 
  5.  
  6.         _logInPage("_poll Request Start, time: " + new Date().getTime()); 
  7.         _nTime = new Date().getTime(); 
  8.         $.ajax({ 
  9.         type: "GET", 
  10.         url: "https://login." + _sBaseHost + "/cgi-bin/mmwebwx-bin/login?uuid=" + _asUUID + "&tip=" + show_tip, 
  11.         dataType: "script", 
  12.         cache: false, 
  13.         timeout: _nAjaxTimeout, 
  14.         success: function(data, textStatus, jqXHR) { 
  15.             _logInPage("_poll Request Success, code: " + window.code + ", time: " + (new Date().getTime() - _nTime) + "ms"); 
  16.             switch (_aoWin.code) { 
  17.             case 200: 
  18.                 _sSecondRequestTime = new Date().getTime() - _sSecondRequestTime; 
  19.                 _logInPage("Second Request Success, time: " + _sSecondRequestTime + "ms"); 
  20.                 clearTimeout(_oResetTimeout); 
  21.  
  22.                 $.get(_aoWin.redirect_uri + "&fun=new", function(msg) { 
  23.                     _logInPage("new func reponse, reponseMsg: " + msg); 
  24.                     _reportNow("new func reponse, reponseMsg: " + msg); 
  25.                     var code = msg.match(/