当前位置: 首页 > 后端技术 > PHP

小程序获取unionid

时间:2023-03-29 16:48:10 PHP

公众号,将小程序绑定到微信开放平台。微信开放平台需要认证(300),然后进行相关绑定。绑定时需要相关账号原管理员扫码绑定小程序。同样操作小程序获取unionIDLoginbindGetUserInfo:function(e){if(e.detail.userInfo){//用户按下了授权按钮varthat=this;//将登录用户的相关信息插入数据库varopenid=getApp().globalData.openid;varsession=getApp().globalData.sessiongetApp().globalData.userInfo=e.detail.userInfo;//通过wx.login获取代码获取用户信息必须登录wx.login({    success:function(res){    如果(res.code){varcode=res.code;//获取加密数据ivwx.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',成功(res){if(res.data.result==1){wx.setStorageSync('用户',res.data.msg);那.onLoad();that.setData({is_login:true})}else{console.log("写入失败")}}})}})}else{        console.log('获取用户登录状态失败!'+res.errMsg);      }    }  })返回;//授权成功后,跳转到小程序首页}else{//用户按下了拒绝按钮wx.showModal({title:'Warning',content:'你点击了如果拒绝授权,将无法进入小程序,请授权后进入!!!',showCancel:false,confirmText:'返回授权',success:function(res){if(res.confirm){console.log('用户点击“返回授权”')}}})}},后台使用tp5.1+easywechat4useEasyWeChat\Factory;$miniProgram=Factory::miniProgram($this->config);$decryptedData=$miniProgram->encryptor->decryptData($userinfo['session'],$userinfo['iv'],$userinfo['encryptedData']);$数据['unionid']=$decryptedData['unionId'];价值后台管理系统免费使用###紫峰后台管理系统###,直接安装在宝塔面板即可。欢迎关注我公众号:子枫的精彩世界,获取独家学习资源和每日干货推送。如果您对我的其他话题感兴趣,请访问我的个人博客:www.wangmingchang.com。