公众号和小程序绑定微信开放平台
微信开放平台需要认证(300)
然后进行相关的绑定,绑定时需要相关账号的原始管理者进行扫码绑定
小程序也是一样操作
小程序获取unionID
1 2 3 |
<button hidden="{{is_login}}" class='bottom' type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo" style='width:100px'> 登录</button> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
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('用户点击了“返回授权”') } } }) } }, |
1 2 3 4 5 6 7 8 9 |
后端 这里使用的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']; |