const app = getApp() Page({ /** * 页面的初始数据 */ data: { motto: 'Hello World', userInfo: {}, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), openid: '', message: '', resturl: app.data.resturl + '/userinfo.action', uinfo:'' }, //事件处理函数 bindViewTap: function() { wx.navigateTo({ url: '../logs/logs' }) }, onLoad: function () { var that = this console.log("孙"); console.log("-"+app.globalData.quanju); if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }) } else if (this.data.canIUse){ // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } } else { // 在没有 open-type=getUserInfo 版本的兼容处理 wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo this.setData({ userInfo: res.userInfo, hasUserInfo: true }) } }) } // 获取openid wx.login({ success: function (res) { if (res.code) { wx.getUserInfo({ success: function (res) { console.log("存在code") } }); var appid = "wxc55158faad3e366a" var secret = "f38f654411e153673ff4a473f1ccefd0" var openid = "" var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code'; wx.request({ url: l, data: {}, method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT // header: {}, // 设置请求的 header success: function (res) { var obj = {}; obj.openid = res.data.openid; console.log("取得的openid==" + res.data.openid); that.setData({ openid: obj.openid }) obj.expires_in = Date.now() + res.data.expires_in; // console.log(obj); // wx.setStorageSync('user', obj);//存储openid } }); } else { console.log('获取用户登录态失败!' + res.errMsg) } } }); //获取登录用户的code // wx.login({ // success(res){ // if(res.code){ // console.log("当前code存在") // //发送code // wx.request({ // url: 'http://localhost:8080/sendOpenid', // data:{ // code:res.code // } // }) // } // else{ // console.log('登录失败!' + res.errMsg) // } // } // }) }, getUserInfo: function(e) { console.log(e) console.log("---"); app.globalData.userInfo = e.detail.userInfo this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.getUserinfo(); }, loginout:function(){ app.globalData.openid = ""; wx.switchTab({ url: '../../pages/index/index' }) }, getUserinfo: function () { let openid = app.globalData.openid; let that = this; if (openid == '') { wx.redirectTo({ url: '../../pages/login/login', }) return; } let param = "openid=" + app.globalData.openid; wx.request({ url: app.data.resturl + '/uinfo.action', data: { uid: openid }, method: 'post', header: { 'content-type': 'application/x-www-form-urlencoded' }, success: (res) => { console.log(res); that.setData({ dinfo: res.data.uinfo }); } }) }, })