微信小程序最新授权方法,getUserInfo

20180511微信小程序正式关闭原先getUserInfo的逻辑

        

不再允许自动弹出授权框。

方法一:

index.wxml(准备一个用于给用户授权的页面,我这里设置了一个背景图,一个按钮)

<view hidden='{{eye}}'>
    <image class='preBg' src="你的图片地址" mode='widthFix'></image>
    <button open-type="getUserInfo" bindgetuserinfo="getUserInfoFun" style='top:450px; width:50%'>授权</button> 
</view>

index.wxss  

.preBg{
  position: fixed;
  width: 100%;
  display:flex;
  flex-direction:column;
  align-items:center;/*垂直居中*/
  justify-content: center;/*水平居中*/
}

index.js

Page({
  data:{
    eye:true
  },
  onLoad:function(){
   
  },
  onShow: function (options){
    this.getUserInfoFun()
  },
  getUserInfoFun: function (){
    var S = this;
    wx.getUserInfo({
        success: function (res){
            console.log("userInfo:"+res)
       //do anything }, fail: S.showPrePage }) }, showPrePage:
function(){ this.setData({ eye:false }) } })

以上逻辑 

未授权用户打开小程序

  setp1 显示授权预览页面 也就是index.wxml里的内容,

  step2 用户点击按钮,弹出微信的授权对话窗,

  step3-1 用户点击取消 -> 停留在当前页

  step3-2 用户点击授权 -> 获取信息成功  -> Do anything

已授权用户打开小程序,则直接去 -> Do anything

方法二: 

  通过login拿到code, 然后请求自家服务器,去拿用户的openid,等信息。

猜你喜欢

转载自www.cnblogs.com/luoeeyang/p/9024567.html
今日推荐