WeChatアップルは、アプリ関数を非同期的に呼び出して、他のページのオンロードがアプリの起動時に設定されたopenidを取得できないという問題を解決します。

まず、app.jsの関数コンテンツをカスタマイズして、渡す必要のあるデータを書き込みます。ここで必要なのは、ユーザーのopenid
リファレンスです。

lc: function (wei) {

    var that = this;

    wx.login({
      success: function (res) {
        console.log(res.code)
        //发送请求

        wx.request({
          url: 'http://127.0.0.1:9090/xcxmvc/pp/dl', //接口地址
          data: { code: res.code },
          header: {
            'content-type': 'application/json' //默认值
          },
          success: function (res) {
            c = "ff";
            console.log(res.data)
            that.globalData.openid = res.data.openid;
            wei(res);
            return wei;
          }
        })
      }

    })
    console.log("c");
   

    // this.ajax();//调用ajax函数
  },

次に、app.jsの起動時にこの関数を呼び出します

var wei = this.lc(function (wei) {
      console.log(wei);
    
    });

最後に、データを取得する必要があるページのオンロードを書き込みます

onLoad:function(){
 var that = this;
    var wei = app.lc(function (wei) {
      console.log(wei.data.openid);
      that.setData({openid:wei.data.openid});
    
   
    wx.connectSocket({
      url: "ws://127.0.0.1:9090/xcxmvc/so",
    })

    //连接成功
    wx.onSocketOpen(function () {
      console.log("c" + that.data.openid);
      wx.sendSocketMessage({
        data: that.data.openid,
      })
    })
    wx.onSocketMessage(function (res) {
      var objData = JSON.parse(res.data);
      console.log(res);
      that.setData({ nr: objData });
    })


    //连接失败
    wx.onSocketError(function () {
      console.log('websocket连接失败!');
    })

    });
  },

にリクエストを書き込む必要がありますvar wei = app.lc(function (wei) { 写在这里})

おすすめ

転載: blog.csdn.net/weixin_40938312/article/details/104730824