// pages/login/index.js const app = getApp() Page({ /** * 页面的初始数据 */ data: { login: false, canIUseGetUserProfilefalse: false, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let userInfo = wx.getStorageSync('userInfo') if (wx.getUserProfile) { this.setData({ canIUseGetUserProfile: true }) } wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId this.setData({ code: res.code }) } }) }, setUserInfo(){ }, getUserProfile(e) { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (response) => { wx.showLoading({ title: '登陆中...', }) let data = { code: this.data.code, iv: response.iv, encryptedData:response.encryptedData, spread: 0, login_type: 0 } app.http('POST','wxapp/auth',data).then( result =>{ if(result.data.success){ wx.hideLoading() wx.setStorageSync('token', 'Bearer '+ result.data.data.token) wx.setStorageSync('userInfo', result.data.data.user) wx.showToast({ title: '授权成功!', duration: 1500 }) if(!result.data.data.user.phone){ this.setData({ login: true }) } else{ setTimeout(()=>{ wx.navigateBack({ delta: 1, }) },1500) } } else{ wx.showToast({ title: result.data.msg, icon : 'none' }) } } ) } }) }, getPhoneNumber(e) { if(e.detail.errMsg == 'getPhoneNumber:ok'){ app.http('post','wxapp/binding', {encryptedData: e.detail.encryptedData, iv: e.detail.iv }).then((res)=>{ if(res.data.success){ app.getInfo(); wx.showToast({ title: res.data.msg, duration: 1500 }) setTimeout(()=>{ wx.navigateBack({ delta: 1, }) },1500) } else{ wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) setTimeout(()=>{ wx.navigateBack({ delta: 1, }) },2000) } }) } }, back(){ wx.navigateBack({ delta: 1, }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })