WeChat applet obtains user information and unique openid

First come a button 

	<button type="primary"  open-type="getUserInfo"  lang="zh_CN" bindtap="primary" bindgetuserinfo="YuYueSubmit" > 用户信息获取 </button>

 Then come a js

  YuYueSubmit: function (e) {
    console.log(e);                                        //此处用户所有信息
    wx.login({
      success: function (code) {
        console.log(code.code)
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',  
          data: {
            appid: 'xxxxxxxxx',                          //此处是小程序appid
            secret: "70916b1a757dfa2affcxxxx",  //此处是小程序秘钥
            js_code: code.code,                          //此处登录返回的code
            grant_type: "authorization_code"              //此处是固定写法
          },
          method: 'GET',
          success: function (data) {
            console.log(data.data.openid);                //此处是返回的openid
          }

        })


      }
    })
   
  },

Where url is the official request address of WeChat, which needs to be encapsulated in the backend. Therefore, the returned information is received at the back end and returned to the front end.

For local debugging, you only need to open the local domain name verification in the WeChat applet tool, the official WeChat URL.

 

Guess you like

Origin blog.csdn.net/weixin_38959210/article/details/107600249