WebSocketのスキャンコードログイン

コードの利用者の抜本的な成功した後、自動的にページをログに記録します

解析プロセス

  1. ユーザーログインページの負荷は、JSは、意志のwebsocketSession Unicodeで保存され、自動的に生成され、自動的に2秒後にサーバー用WebSocketに送信されたUnicode
  2. マイクロチャンネルスキャンコードユーザがUnicode sceneId、ランダムに生成された2次元コードのように、ボタンをクリックすると、現在のページがない場合には、ジャンプが現在のユーザに表示されます
  3. ユーザースキャンコードの後、背景としてのマイクロチャネルサービス番号は、Unicode sceneId、WebSocketの通知サーバ、それが特定のメッセージ、コマンドを送信するために、そのUnicode値とUnicode値マッチセッションを通過したセッションを取得し、自動的にOpenIDを用いて送信されたログインします過去
  4. ページは、特定のメッセージ受信:自動ログインコマンドとのOpenIDを、自動的にユーザーがログにジャンプするのに役立ちます
//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访问后台登录接口认证并登录
      ......
      }
公開された27元の記事 ウォン称賛21 ビュー4596

おすすめ

転載: blog.csdn.net/weixin_43997143/article/details/90290778