ユーザー情報の取得 - WeChat ミニ プログラム

ボタンコンポーネントのオープンタイプ

<button open-type=""></button> の open-type は、WeChat オープン機能を提供するために使用されます。open-type には、次の正当な値があります。

ユーザーがセッションでメッセージ カードをクリックした後にアプレットに戻った場合、bindcontact コールバックから特定の情報を取得できます。
共有し、ユーザーにリツイートを促します。
getPhoneNumber、ユーザーの携帯電話番号を取得するには、bindgetphonenumber コールバックからユーザー情報を取得できます。
getUserInfo、ユーザー情報を取得するには、bindgetuserinfo コールバックからユーザー情報を取得できます。
アプリを起動し、アプリを開きます。
openSetting で、認可設定ページを開きます。
フィードバックをクリックして、フィードバック ページを開きます。
 

<!--index.wxml-->
<button open-type="contact" bindcontact="contact">contact </button>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">getPhoneNumber</button>
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">getUserInfo</button>
// index.js

Page({
  contact:function(e){
    console.log(e.detail);
  },
  getPhoneNumber:function(e){
    console.log(e.detail);
  },
  getUserInfo:function(e){
    console.log(e.detail.userInfo);
  }
})

 クリック ボタンを使用してユーザーに承認を求めるプロンプトを表示します。ユーザー情報は、ユーザーが承認して同意した後にのみ取得できます。

<!--index.wxml-->
<view class="container">
  <button wx:if="{
   
   {!hasUserInfo}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">获取头像昵称</button>
  <view class="userinfo" wx:else>
    <image src="{
   
   {userInfo.avatarUrl}}"></image>
    <text>{
   
   {userInfo.nickName}}</text>
  </view>
</view>
// index.js
Page({
  data:{
    hasUserInfo:false,
    userInfo:{}
  },
  getUserInfo:function(e){
    console.log(e);
    console.log(e.detail.userInfo);
    if(e.detail.userInfo){
      this.setData({
        userInfo:e.detail.userInfo,
        hasUserInfo:true
      })
    }
  }
})

  getUserInfoユーザー情報はほとんど提供されません。これは、2021 年 4 月 13 日以降、getUserInfoポップアップ ウィンドウがポップアップしなくなり、匿名のユーザー個人情報が直接返されるため、

WeChat はgetUserProfile()交換を申し出るgetUserInfo()

インターフェース getUserProfile

インターフェース getUserProfile

インターフェイスは、ユーザー情報を取得するためにgetUserProfile置き換えるために使用されますgetUserInfo
ページ上でクリック イベントが生成された後にのみ呼び出すことができます. リクエストごとに承認ウィンドウがポップアップ表示されます. ユーザーは同意後に戻りますuserInfo. 詳細については、以下の例を参照してください.

<!--index.wxml-->
<view class="container">
  <button wx:if="{
   
   {!hasUserInfo}}" bindtap="getUserProfile">获取头像昵称</button>
  <view class="userinfo">
    <image src="{
   
   {userInfo.avatarUrl}}"></image>
    <view>{
   
   {userInfo.nickName}}</view>
  </view>
</view>
// index.js
Page({
  data:{
    hasUserInfo:false,
    userInfo:{}
  },
  getUserProfile:function(){
    wx.getUserProfile({
      desc: '获取用户信息',
      success:(res) => {
        console.log(res.userInfo);
        this.setData({
          hasUserInfo:true,
          userInfo:res.userInfo
        })
      }
    })
  }
})

 

getUserProfile() の成功コールバック関数のパラメーター res には、次の属性が含まれています。

errMsg、エラー メッセージ。
userInfo (ユーザー情報オブジェクト) には、openid などの機密情報は含まれません。
機密情報を含まない raw データ文字列である rawData は、署名の計算に使用されます。
署名、sha1(rawData+session_key) を使用して文字列を取得します。これは、ユーザー情報の検証に使用されます。
encryptedData、機密データを含む完全なユーザー情報の暗号化されたデータ。
iv、暗号化アルゴリズムの初期ベクトル。

アプレットのフォアグラウンドがユーザー情報をアプレットのバックグラウンドに送信する必要がある場合は、次の方法でwx.request()実現できます。


承認結果を表示する 

wx.getSetting ユーザーの現在の設定を取得します。アプレットがユーザーに要求した権限のみが戻り値に表示されます。小さな例を見てください。

<!--index.wxml-->
<button bindtap="getSetting">查看授权结果</button>
// index.js
Page({
  getSetting:function(){
    wx.getSetting({
      success:(res) => {
        console.log(res.authSetting);
        if(res.authSetting["scope.userInfo"]){
          wx.getUserInfo({
            success: (res) => {
              console.log(res.userInfo);
            }
          })
        }
      }
    })
  }
})

res.authSetting含む

  • scope.address、送り先
  • scope.invoice、明細書
  • scope.invoiceTitle、請求書
  • scope.userInfo、ユーザー情報

コンポーネント open-data は、 WeChat オープンデータを表示するために使用されます。いわゆる「公開」データとは、ユーザーのアバターやニックネーム、性別など、ユーザーの許可なく表示できるデータのことです。

オープンデータのタイプには、次の有効
あり
ます






 

开放数据的类型,有如下合法值,
groupName
userNickName,用户昵称
userAvatarUrl,用户头像
userGender,用户性别
userCity,用户所在城市
userProvince,用户所在省份
userCountry,用户所在国家
userLanguage,用户的语言

おすすめ

転載: blog.csdn.net/m0_46965984/article/details/124320405