美文网首页
小程序获取openid

小程序获取openid

作者: 小进我是爸爸 | 来源:发表于2019-08-12 16:46 被阅读0次

denglu:function(){

    var that = this;

    wx.login({

      success: function (res) {

        if (res.code) {

          //获取openId

          wx.request({

            url: 'https://api.weixin.qq.com/sns/jscode2session',

            data: {

                     //小程序唯一标识

              appid: ' ',

              //小程序的 app secret

              secret: ' ',

              grant_type: 'authorization_code',

              js_code: res.code

            },

            method: 'GET',

            header: { 'content-type': 'application/json' },

            success: function (openIdRes) {

              console.info("登录成功返回的openId:" + openIdRes.data.openid);

              // 判断openId是否获取成功

              if (openIdRes.data.openid != null & openIdRes.data.openid != undefined) {

                        // 有一点需要注意 询问用户 是否授权 那提示 是这API发出的

                wx.getUserInfo({

                  success: function (data) {

                    // 自定义操作

                    // 绑定数据,渲染页面

                    that.setData({

                    });

                  },

                  fail: function (failData) {

                    console.info("用户拒绝授权");

                  }

                });

              } else {

                console.info("获取用户openId失败");

              }

            },

            fail: function (error) {

              console.info("获取用户openId失败");

              console.info(error);

            }

          })

        }

      }

    });

  }

相关文章

网友评论

      本文标题:小程序获取openid

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