1.获取用户信息
目前只能通过button组件引导用户主动进行授权操作,具体代码如下
wxml
<!-- 需要使用 button 来授权登录 --> <button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" class="sqbtn">授权登录</button> <view wx:else>请升级微信版本</view>
js
Page({ data: { canIUse: wx.canIUse('button.open-type.getUserInfo'), }, bindGetUserInfo: function (e) { encryptedData = e.detail.encryptedData; //包括敏感数据在内的完整用户信息的加密数据 iv = e.detail.iv; //加密算法的初始向量
//将数据存储到本地
wx.setStorage({
key: 'info',
data: e.detail,
})
}
})
2.获取不到授权信息时再次授权
openmm = wx.getStorageSync('info').encryptedData || ""; iv = wx.getStorageSync('info').iv || ""; if (iv == "" || openmm == "") { wx.showModal({ title: '警告', content: '重新授权', success: function(res) { if (res.confirm) { wx.navigateTo({ url: '../start/start', //有授权按钮的启动页 }) } } }) }