码迷,mamicode.com
首页 > 微信 > 详细

微信二维码登录原理

时间:2014-06-25 09:48:09      阅读:458      评论:0      收藏:0      [点我收藏+]

标签:des   android   class   code   http   tar   

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

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(/<script>(.*)<\/script>/); 
  26.                     if(code){ 
  27.                         eval(code[1]); 
  28.                     }else{ 
  29.                         $("#container").show(); 
  30.                         $("#login_container").hide(); 
  31.                     } 
  32.                 }); 
  33.  
  34.                 _reportNow("/cgi-bin/mmwebwx-bin/login, Second Request Success, uuid: " + _asUUID + ", time: " + _sSecondRequestTime + "ms"); 
  35.                 break; 
  36.  
  37.             case 201: 
  38.                 clearTimeout(_oResetTimeout); 
  39.                 show_tip = 0; 
  40.                 $(‘.errorMsg‘).hide(); 
  41.                 $(‘.normlDesc‘).hide(); 
  42.                 $(‘.successMsg‘).show(); 
  43.                 _logInPage("First Request Success"); 
  44.                 _reportNow("/cgi-bin/mmwebwx-bin/login, First Request Success, uuid: " + _asUUID); 
  45. //                setTimeout(function(){ 
  46.                     _logInPage("Second Request Start"); 
  47.                     _reportNow("/cgi-bin/mmwebwx-bin/login, Second Request Start, uuid: " + _asUUID); 
  48.  
  49.                     _sSecondRequestTime = new Date().getTime(); 
  50.  
  51.                     _nAjaxTimeout = 5 * 1000; 
  52.                     _self(_asUUID); 
  53. //                }, 500); 
  54.                 break; 
  55.  
  56.             case 408: 
  57.                 setTimeout(function(){ 
  58.                     _self(_asUUID); 
  59.                 }, 500); 
  60.                 break; 
  61.  
  62.             case 400: 
  63.             case 500: 
  64.                 _reset(); 
  65.                 _afterLoadWebMMDo(function(){ 
  66.                     _aoWin.Log.d("500, Login Poll Svr Exception"); 
  67.                 }); 
  68.                 break; 
  69.             } 
  70.         }, 
  71.         error: function(jqXHR, textStatus, errorThrown) { 
  72.             if (textStatus == ‘timeout‘) { 
  73.                 setTimeout(function(){ 
  74.                     _self(_asUUID); 
  75.                 }, 500); 
  76.             } else { 
  77.                 setTimeout(function(){ 
  78.                     _self(_asUUID); 
  79.                 }, 5000); 
  80.  
  81.                 _logInPage("_poll Request Error:" + textStatus); 
  82.                 _afterLoadWebMMDo(function(){ 
  83.                     _aoWin.Log.e("Login Poll Error:" + textStatus); 
  84.                 }); 
  85.             } 
  86.         } 
  87.         }); 
  88.     } 

网页客户端每500毫秒就向服务器发起ssl请求,请求当前二维码的登陆信息,如果返回结果201,则说明已经获取扫描二维码终端相同的账号登陆授权,当返回其他结果时,将在500毫秒之后重新发起请求。

类似微信登陆场景应用场景还是很多,比如通过二维码进行设备间的授权。比如使用手机遥控 装有android系统的电视盒等。

微信二维码登录原理,布布扣,bubuko.com

微信二维码登录原理

标签:des   android   class   code   http   tar   

原文地址:http://www.cnblogs.com/Free-Thinker/p/3807336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!