1.login.wxml
1 <view class='padding flex flex-direction'> 2 <button class="cu-btn bg-blue margin-tb-xs lg" hidden="{{hid1}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权</button> 3 <button class="cu-btn bg-blue margin-tb-xs lg" hidden="{{hid2}}" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" >进入</button> 4 </view>
2.app.js
wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId if (res.code) { console.log("code========" + JSON.stringify(res)); //发起网络请求 wx.request({ url: 'https://....../api/OnLogin/get', data: { code: res.code }, // header: { 'content-type': 'application/json' }, success: data => { console.log("data" + JSON.stringify(data.data)); this.globalData.session_key = data.data.session_key; this.globalData.openid = data.data.openid; console.log("openid" + this.globalData.openid); } }) } else { console.log('登录失败!' + res.errMsg) } } })
.login.js
Page({ data: { userPhone: '', userNicheng:'', hid1:false, hid2: true }, bindGetUserInfo:function(e){ this.setData({ userNicheng: e.detail.userInfo.nickName, hid1: true, hid2: false }) }, getPhoneNumber: function(e) { var that = this; console.log("getPhoneNumberok" + e.detail.errMsg); if (e.detail.errMsg == "getPhoneNumber:ok") { wx.request({ url: 'https://..../api/OnLogin/get', data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, session_key: app.globalData.session_key }, method: "get", success: function(res) { that.setData({ userPhone: res.data })
wx.navigateTo({
url: '../index/index’
});
}); } }) } },