websocket scan code Login

After the sweeping success of the code user to automatically log pages

Parsing process

  1. User login page loads, JS unicode generated automatically, and automatically sent to the server websocket after two seconds, will websocketSession stored in unicode,
  2. Micro-channel scan code the user clicks the button, as the Unicode sceneId, randomly generated two-dimensional code, and in the case where the current page is not the jump is displayed to the current user
  3. After the user scan code, micro-channel service number as a background acquired Unicode sceneId, websocket notification server, the session it has passed its Unicode values ​​and the Unicode value matched session to send a particular message, command, and will automatically log sent with openID past
  4. Page received a specific message: automatic login command and openID, automatically helps the user to jump to log
//html
    <p>使用微信 扫码登录/注册</p>
    <img v-bind:src="wechatUrl">
     
  import sockjs from '../assets/js/sockjs-1.1.2.min' //根据本地路径,引入sockjs
   export default {
    data () {
       return{
          wechatUrl:'',
          websocket: null,
          openId:''
       }
     }
    }
  beforeMount () {
      var that = this;
      var unicode;
      var websocket = null;
      var transports = [];
      var url = `${IPLOCATION}/sockjs`;
      websocket = new SockJS(url, undefined, {
        transports: transports
      });
      websocket.onopen = function () {
        console.log('that.websocket: connection is opened');
      };
      websocket.onclose = function () {
        console.log('that.websocket: connection is closed');
      };
      websocket.onheartbeat = function () {
        console.log('that.websocket: connection heartbeat...');
      };
      websocket.onmessage = function (e) {
        if (e.data.indexOf("AUTOMATIC_LOGIN") != -1) {
          websocket.onclose();
          that.toLogin(e);
        } else {
          console.log('websocket:', e.data);
        }
      };
      that.websocket = websocket
    },
mounted(){
      var that=this;
      that.unicode = Math.round(Math.random() * 100000000) + "1";
      $.get(IPLOCATION + `${接口}sceneId=${that.unicode}`, null, function (url) {
        that.wechatUrl = url
      });
      setTimeout(function () {that.websocket.send(that.unicode)}, 2000);
    }
//扫码登录
 toLogin(e) {
        var that=this;
        that.openId = e.data.split("####")[1];
        // var mixedValues = e.data.split('####');
      // var openId = mixedValues[1]; // 用户的openId
      var sceneId = 911; // 登录系统默认场景值
      // 关闭二维码
      // 通过ajax访问后台登录接口认证并登录
      ......
      }
Published 27 original articles · won praise 21 · views 4596

Guess you like

Origin blog.csdn.net/weixin_43997143/article/details/90290778