微信小程序獲取用戶登錄信息(頭像,昵稱,地址,性別等)


 

 

login.wxml頁面代碼:

 重點:open-type="getUserInfo"
<view wx:if="{{isHide}}">
    <view wx:if="{{canIUse}}" >
        <view class='header'>
            <image src='../../../assets/images/logo.jpg'></image>
        </view>
 
        <view class='content'>
            <!-- <view>申請獲取以下權限</view>
            <text>獲得你的公開信息(昵稱,頭像等)</text> -->
        </view>
 
        <button class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">
            微信授權登錄
        </button>
    </view>
    <view wx:else>請升級微信版本</view>
</view>
 
<view wx:else>
    <view>我的首頁內容</view>
</view>

 

 

css代碼:

 
.header {
    margin: 90rpx 0 90rpx 50rpx;
    border-bottom: 1px solid #ccc;
    text-align: center;
    width: 650rpx;
    height: 300rpx;
    line-height: 450rpx;
}
 
.header image {
    width: 200rpx;
    height: 150rpx;
}
 
.content {
    margin-left: 50rpx;
    margin-bottom: 90rpx;
}
 
.content text {
    display: block;
    color: #9d9d9d;
    margin-top: 40rpx;
}
 
.bottom {
    border-radius: 80rpx;
    margin: 70rpx 50rpx;
    font-size: 35rpx;
}

 

js代碼:

Page({
  globalData: {
    appid: '', //appid需自己提供
    secret: '', //secret需自己提供
  },
  data: {
    //判斷小程序的API,回調,參數,組件等是否在當前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: false
  },

  onLoad: function () {
    var that = this;
    // 查看是否授權
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              // 用戶已經授權過,不需要顯示授權頁面,所以不需要改變 isHide 的值
              // 根據自己的需求有其他操作再補充
              // 我這里實現的是在用戶授權成功后,調用微信的 wx.login 接口,從而獲取code
              wx.login({
                success: res => {
                  // 獲取到用戶的 code 之后:res.code
                  console.log("用戶的code:" + res.code);
                  // 可以傳給后台,再經過解析獲取用戶的 openid
                  // 或者可以直接使用微信的提供的接口直接獲取 openid ,方法如下:
                  var d = that.globalData; //這里存儲了appid、secret、token串 
                  wx.request({
                    //     // 自行補上自己的 APPID 和 SECRET
                    url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + d.appid + '&secret=' + d.secret + '&js_code=' + res.code + '&grant_type=authorization_code',
                    data: {},
                    method: 'GET',
                    success: res => {
                      var obj = {};
                      obj.openid = res.data.openid;
                      console.log("openid:" + obj.openid);
                      console.log("session_key:" + res.data.session_key);
                      obj.expires_in = Date.now() + res.data.expires_in;
                      wx.setStorageSync('user', obj); //存儲openid 

                      // 獲取到用戶的 openid
                      console.log(res.data)
                      console.log("用戶的openid:" + res.data.openid);
                    }
                  });
                }
              });
            }
          });
        } else {
          // 用戶沒有授權
          // 改變 isHide 的值,顯示授權頁面
          that.setData({
            isHide: true
          });
        }
      }
    });
  },

  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用戶按了允許授權按鈕
      var that = this;
      // 獲取到用戶的信息了,打印到控制台上看下
      console.log("用戶的信息如下:");
      console.log(e.detail.userInfo);
      //授權成功后,通過改變 isHide 的值,讓實現頁面顯示出來,把授權頁面隱藏起來
      that.setData({
        isHide: false
      });
    } else {
      //用戶按了拒絕按鈕
      wx.showModal({
        title: '警告',
        content: '您點擊了拒絕授權,將無法進入小程序,請授權之后再進入!!!',
        showCancel: false,
        confirmText: '返回授權',
        success: function (res) {
          // 用戶沒有授權成功,不需要改變 isHide 的值
          if (res.confirm) {
            console.log('用戶點擊了“返回授權”');
          }
        }
      });
    }
  },
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM