先在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的onlaunch中调用这个函数
var wei = this.lc(function (wei) {
console.log(wei);
});
最后在需要获得数据的页面的onload中写
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) { 写在这里})