欢迎光临
感谢一路有你

小程序获取unionid

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 

公众号和小程序绑定微信开放平台

微信开放平台需要认证(300)

然后进行相关的绑定,绑定时需要相关账号的原始管理者进行扫码绑定

image-20200706145027140

image-20200706144922908

小程序也是一样操作

image-20200706145103644

小程序获取unionID


<code class="">&lt;button hidden="{{is_login}}" class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo" style='width:100px'&gt;
          登录&lt;/button&gt;
</code>

<code class="">bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      //插入登录的用户的相关信息到数据库
      var openid = getApp().globalData.openid;
      var session = getApp().globalData.session
      getApp().globalData.userInfo = e.detail.userInfo;
      //通过wx.login获取code  想要获取用户信息,必须登录
      wx.login({
        success:  function(res){
          if(res.code){
                  var code = res.code;
                  // 获取 encryptedData  iv
                  wx.getUserInfo({
                    withCredentials: true,
                    success: function(res2){
                      //请求自己的登录接口
                        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' // 默认值
                          },
                          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("写入失败")
                            }
                          }
                        })
                    }
                  })
          }else{
            console.log('获取用户登录态失败!'+res.errMsg);
          }
        }
      })

    return ;

      //授权成功后,跳转进入小程序首页
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
</code>

<code class="">后端
这里使用的tp5.1+easywechat4
use EasyWeChat\Factory;
$miniProgram = Factory::miniProgram($this-&gt;config);
$decryptedData = $miniProgram-&gt;encryptor-&gt;decryptData($userinfo['session'],$userinfo['iv'],$userinfo['encryptedData']);
$data['unionid']= $decryptedData['unionId'];


</code>

image-20200706162422946

赞(2) 打赏
未经允许不得转载:王明昌博客 » 小程序获取unionid
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏