Applet to get unionid

Time:2021-3-9

Official account and applet bind WeChat open platform

Wechat open platform needs certification (300)

Then the relevant binding is carried out. When binding, the original manager of the relevant account is required to scan the code

Applet to get unionid

Applet to get unionid

The same is true for applets

Applet to get unionid

The applet gets the unionid

<button hidden="{{is_login}}" class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo" style='width:100px'>
          Log in to < / button >
bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //The user pressed the allow authorization button
      var that = this;
      //Insert the relevant information of the logged in user into the database
      var openid = getApp().globalData.openid;
      var session = getApp().globalData.session
      getApp().globalData.userInfo = e.detail.userInfo;
      //Through wx.login To get user information, you must log in
      wx.login({
        success:  function(res){
          if(res.code){
                  var code = res.code;
                  //Get encrypteddata IV
                  wx.getUserInfo({
                    withCredentials: true,
                    success: function(res2){
                      //Request your own login interface
                        wx.request({
                          url: config.api_base_url + 'login',
                          data: {
                            userinfo: e.detail.userInfo,
                            openid: openid,
                            encryptedData:res2.encryptedData,
                            iv:res2.iv,
                            session:session
                          },
                          header: {
                            'content type':'application / JSON '// default value
                          },
                          method: 'post',
                          success(res) {
                            if (res.data.result == 1) {
                              wx.setStorageSync('user', res.data.msg);
                              that.onLoad();
                              that.setData({
                                is_login:true
                              })
                              
                            } else {
                              console.log (write failed)
                            }
                          }
                        })
                    }
                  })
          }else{
            console.log ('Get user login status failed! '+ res.errMsg );
          }
        }
      })
      
    return ;
      
      //After successful authorization, jump to the home page of the applet
    } else {
      //The user pressed the reject button
      wx.showModal({
        Title: 'warning',
        Content: 'if you click deny authorization, you will not be able to enter the applet, please authorize and then enter again!',
        showCancel: false,
        Confirmtext: 'return authorization',
        success: function (res) {
          if (res.confirm) {
            console.log ("user clicks" return authorization "))
          }
        }
      })
    }
  },
back-end
Tp5.1 + easywechat4 is used here
use EasyWeChat\Factory;
$miniProgram = Factory::miniProgram($this->config);
$decryptedData = $miniProgram->encryptor->decryptData($userinfo['session'],$userinfo['iv'],$userinfo['encryptedData']);
$data['unionid']= $decryptedData['unionId'];

Applet to get unionid

High value background management system is free to use. Zifeng background management system can be directly installed on the pagoda panel

Welcome to my official account: the wonderful world of Zi Feng, exclusive learning resources and daily dry cargo push.
If you are interested in my other topics, please go directly to my personal blog: www.wangmingchang.com .