当前位置:网站首页>Wechat applet obtains login user information, openid and access_ token

Wechat applet obtains login user information, openid and access_ token

2022-04-23 14:04:00 one billion twenty-nine million one hundred and seventy-nine th

1: Get login user information
Because wechat applet improves user experience , Therefore, obtaining user information is improved , Use button Button , add to open-type="getUserInfo" that will do
wxml

<view class='loginHeader'>
     <image src='{
   {userHead}}' class='userHead' mode='aspectFill' bindtap='previewHead' />
     <button class='userTitle'  open-type="getUserInfo"  bindtap='loginTap'>{
   {userTitle}} </button>
     <!-- <text class='userTitle' bindtap='loginTap'>{
   {userTitle}}</text> -->
</view>

js

  loginTap: function () {
                    wx.getUserProfile({
                         desc: ' Display user information ', //  Declare the purpose of obtaining the user's personal information , The follow-up will be shown in the pop-up window , Please fill in carefully 
                         success: (res) => {
                           console.log(res.userInfo)
                           app.userTitle=res.userInfo.nickName
                           app.userHead=res.userInfo.avatarUrl
                           this.setData({
                              userHead: app.userHead,
                              userTitle: app.userTitle
                           })
                         }
                       })
     },

2: obtain openid and access_token
openid:
Each wechat is unique id
access_token:
access_token The official account is the only global interface invoke credential. , The official account calls for interfaces. access_token.
appid、secret: Inside the wechat public platform , See the figure below for details
 Insert picture description here

js( Put it in wechat applet onload Function )

 // obtain openid
    wx.login({
      success: function (res) {
        var code1 = res.code
        var appid1 = " Their own appid"
        var secret1 = " My secret key "
        var ul = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid1 + '&secret=' + secret1 + '&js_code=' + code1 + '&grant_type=authorization_code'
        // obtain openid
        wx.request({
          url: ul,
          method: 'GET',
          success: function (e) {
            var openid = e.data.openid
            console.log(' Get the unique of login identity openid', openid)
            that.openid=e.data.openid
            wx.setStorageSync('openid', openid)
          }
        })

      }
    })
  },
    // obtain access_token
    const appid = " Their own appid" //  Here's your appid
    const secret = " My secret key " //  Here's your secret
    wx.request({
      url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`, 
      header: {
        'content-type': 'application/json' 
      },
      success(res) {
        console.log("at Wechat applet "+res.data.access_token)
        that.access_token=res.data.access_token
        console.log("onload:"+that.access_token)
        wx.setStorageSync('at',res.data.access_token)
      },
      fail(error){
        console.log(error)
      }
    })

版权声明
本文为[one billion twenty-nine million one hundred and seventy-nine th]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231401298099.html