收藏
回答

小程序第一次授权获取不到openid

小程序第一次授权获取不到openid,需要重新编译一下之后才能获取。

代码如下:

这是授权过了走的代码:


    wx.getSetting({

      success: function (res) {

        if (res.authSetting['scope.userInfo']) {

          wx.getUserInfo({

            success: function (res) {

              that.setData({

                avatarUrl: res.userInfo.avatarUrl,

                userInfo: res.userInfo

              }),

                // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值

                // 根据自己的需求有其他操作再补充

                // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code

                wx.login({

                  success: res => {

                    // 获取到用户的 code 之后:res.code

                    //console.log("用户的code:" + res.code);

                    that.setData({

                      code: res.code

                    }),

                      //请求获取openid

                      wx.request({

                        url: app.globalData.url + '/appInterface/getOpenid.do?code=' + res.code,

                        //url: app.globalData.url + '/appInterface/getOpenid.do',

                        method: 'GET',

                        // data:{

                        //   code: res.code

                        // },

                        header: {

                          'Content-Type': 'application/json'

                        },

                        success: function (ares) {

                          //将openid设置为所有页面都能访问

                          app.globalData.openid = ares.data.obj

                          //默认请求注册会员

                          // wx.request({

                          //   url: app.globalData.url + '/appInterface/addUser.do',

                          //   data: {

                          //     userOpenid: ares.data.obj,

                          //     userNick: that.data.userInfo.nickName,

                          //     userHadimag: that.data.userInfo.avatarUrl

                          //   },

                          //   method: 'POST',

                          //   header: {

                          //     'Content-Type': 'application/x-www-form-urlencoded'

                          //   },

                          //   success: function (bres) {


                          //   }

                          // })


                        }

                      })

                    //授权成功跳转到首页

                    wx.reLaunch({

                      url: '/pages/login/login'

                    })

                  }

                });

            }

          });

        } else {

          // 用户没有授权

          // 改变 isHide 的值,显示授权页面

          that.setData({

            isHide: true

          });

        }

      }

    });


这是第一次授权时走的代码:


  bindGetUserInfo: function (e) {

    if (e.detail.userInfo) {

      //用户按了允许授权按钮

      var that = this;

      // 获取到用户的信息了,打印到控制台上看下

      console.log("用户的信息如下:");

      console.log(e.detail.userInfo);

      //授权成功后,通过改变 isHide 的值,让实现页面显示出来,把授权页面隐藏起来

      that.setData({

        isHide: false

      });

      wx.getUserInfo({

        success: function (res) {

          that.setData({

            avatarUrl: res.userInfo.avatarUrl,

            userInfo: res.userInfo

          }),

            // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值

            // 根据自己的需求有其他操作再补充

            // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code

            wx.login({

              success: res => {

                // 获取到用户的 code 之后:res.code

                console.log("用户的code:" + res.code);

                wx.request({

                        url: app.globalData.url + '/appInterface/getOpenid.do?code=' + res.code,

                        //url: app.globalData.url + '/appInterface/getOpenid.do',

                        method: 'GET',

                        // data:{

                        //   code: res.code

                        // },

                        header: {

                          'Content-Type': 'application/json'

                        },

                        success: function (ares) {

                          //将openid设置为所有页面都能访问

                          app.globalData.openid = ares.data.obj

                          //默认请求注册会员

                          // wx.request({

                          //   url: app.globalData.url + '/appInterface/addUser.do',

                          //   data: {

                          //     userOpenid: ares.data.obj,

                          //     userNick: that.data.userInfo.nickName,

                          //     userHadimag: that.data.userInfo.avatarUrl

                          //   },

                          //   method: 'POST',

                          //   header: {

                          //     'Content-Type': 'application/x-www-form-urlencoded'

                          //   },

                          //   success: function (bres) {


                          //   }

                          // })


                        }

                      })

                wx.reLaunch({


                  url: '/pages/login/login'


                })

              }

            });

        }

      });

    } else {

      //用户按了拒绝按钮

      wx.showModal({

        title: '警告',

        content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',

        showCancel: false,

        confirmText: '返回授权',

        success: function (res) {

          // 用户没有授权成功,不需要改变 isHide 的值

          if (res.confirm) {

            console.log('用户点击了“返回授权”');

          }

        }

      });

    }

  },

各路大佬快来帮帮忙吧

回答关注问题邀请回答
收藏

3 个回答

  • 微信支付技术助手1
    微信支付技术助手1
    2019-12-30

    可到小程序栏目咨询

    2019-12-30
    有用
    回复
  • 老张
    老张
    2019-12-30

    授权和openid没有关系。

    2019-12-30
    有用
    回复 4
  • 嗯哼
    嗯哼
    2019-12-30

    来人帮帮忙啊


    2019-12-30
    有用
    回复
登录 后发表内容
问题标签