2019年12月28日
1.现象 【开发环境才会包错,正式上线没有问题】

2.解决:
主页添加获取不到用户信息流程
//获取应用实例
var app = getApp()
Page({
data: {
flag: 0,
userInfo: {}
},
onLoad: function() {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
})
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo
})
},
fail: function () {
//获取用户信息失败后。请跳转授权页面
wx.showModal({
title: '警告',
content: '尚未进行授权,请点击确定跳转到授权页面进行授权。',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
wx.navigateTo({
url: '../tologin/tologin',
})
}
}
})
}
})
}
}
})
2.之后跳转到tologin页面

<button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
// pages/tologin/tologin.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
},
bindGetUserInfo: function(e) {
var that = this;
//此处授权得到userInfo
console.log(e.detail.userInfo);
//接下来写业务代码
app.globalData.userInfo = e.detail.userInfo
//最后,记得返回刚才的页面
wx.navigateBack({
delta: 1
})
}
})
3.点击授权后,保存全局对象

参考:
https://developers.weixin.qq.com/community/develop/doc/0000a26e1aca6012e896a517556c01
网友评论