微信小程序 获取OpenId

 

微信小程序 官方API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/json

首先 如下代码是 页面加载请求用户 是否赞成受权 赞成以后 用code 访问 微信接口 拿到OpenId小程序

//页面加载 微信受权
var getInfo = function (thisObj){
  var that = thisObj;
  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);
              weChatUserInfo.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); } }) } } }); }


如下是 手动配置 打开 微信受权微信小程序

//手动打开微信受权
var getInfoAgain = function (thisObj){
  var that = thisObj;
  wx.openSetting({
    success: function (data) {
      //判断 用户是否赞成受权
      if (data.authSetting["scope.userInfo"] == true) {
        // 赞成受权
        wx.login({
          success: function (res) {
            if (res.code) {
              console.info("登陆成功返回的CODE:" + 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) {
                  // 获取到 openId
                  console.log(openIdRes.data.openid);
                  // 判断openId是否为空
                  if (openIdRes.data.openid != null & openIdRes.data.openid != undefined) {
                    wx.getUserInfo({
                      success: function (data) {
                        // 自定义操做
                        // 绑定数据,渲染页面
                        that.setData({
                          
                        });
                      }
                    })
                  }else {
                    // openId为空
                  }
                }
              })
            }
          }
        });
      }else {
           // 手动 开启 是否受权提示框后 拒绝
      }
    }
  });
}               

//TODO 有个地方须要注意一下 小程序开发者工具 有一个配置api

这个配置 若是打开 不验证域名 均可以访问微信

可是 这只是开发者工具 能够访问 以及手机预览 能够访问session

若是放到正式版的环境 或者说 测试版的环境 那么 是不能够访问除了 设置好的域名之外 全部的域名 须要将 微信接口 (https://api.weixin.qq.com) 设置到 小程序白名单中 不然 获取不到OpenId 返回undefinedapp

相关文章
相关标签/搜索