美文网首页
小程序获取unionid

小程序获取unionid

作者: 子枫Eric | 来源:发表于2020-07-06 20:20 被阅读0次

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

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

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

    image-20200706145027140 image-20200706144922908

    小程序也是一样操作

    image-20200706145103644

    小程序获取unionID

    <button hidden="{{is_login}}" class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo" style='width:100px'>
              登录</button>
    
    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('用户点击了“返回授权”')
              }
            }
          })
        }
      },
    
    后端
    这里使用的tp5.1+easywechat4
    use EasyWeChat\Factory;
    $miniProgram = Factory::miniProgram($this->config);
    $decryptedData = $miniProgram->encryptor->decryptData($userinfo['session'],$userinfo['iv'],$userinfo['encryptedData']);
    $data['unionid']= $decryptedData['unionId'];
          
          
    
    image-20200706162422946

    高颜值后台管理系统免费使用 ### 子枫后台管理系统 ###,可在宝塔面板直接安装

    欢迎关注我的公众号:子枫的奇妙世界,获得独家整理的学习资源和日常干货推送。
    如果您对我的其他专题内容感兴趣,直达我的个人博客:www.wangmingchang.com

    相关文章

      网友评论

          本文标题:小程序获取unionid

          本文链接:https://www.haomeiwen.com/subject/cswgqktx.html