微信小程序 获取openid客户端

    
    var that = this
    wx.login({
      success: function (res) {
        var appId = '     ';//微信公众号平台申请的appid
        var appSecret = '   ';//微信公众号平台申请的app secret
        var js_code = res.code;//调用登录接口获得的用户的登录凭证code
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + appSecret + '&js_code=' + js_code + '&grant_type=authorization_code',
          data: {},
          method: 'GET',
          success: function (res) {
            var openid = res.data.openid //返回的用户唯一标识符openid
            console.log(openid)
            console.log("试试吧上面就是获得的openid")
          }
        })
      }
    })




    //试验自己的服务器获取openId
    //调用登录接口
    wx.login({
      success: function (res) {
        //console.log(res);
        that.globalData.loginCode = res.code
        wx.getUserInfo({
          success: function (res) {
            that.globalData.userInfo = res.userInfo
            that.globalData.iv = res.iv
            that.globalData.encryptedData = res.encryptedData
            typeof cb == "function" && cb(that.globalData.userInfo)
            that.req(
              'https://lifar网址.aspx?Action=ActionLogin',
              {
                encryptedData: that.globalData.encryptedData,
                iv: that.globalData.iv,
                code: that.globalData.loginCode
              },
              'GET',
              function (res) {
                console.log(res)
                if (res.data.success) {
                  console.log('试验自己的服务器获取openId:')
                  console.log(res.data)
                  var sessionId = res.data.result;
                  wx.setStorageSync('sessionId', sessionId)
                  console.log(sessionId)
                }
              },
              function (res) {
                console.log(res)
              }
            );
          }
        })
      }
    })

    //试验自己的服务器获取openId结束


方法:

  req: function (url, data, method, success, fail) {
    var mydata = data || {};
    //mydata['appId'] = app.globalData.appId;
    wx.request({
      url: url,
      data: mydata,
      method: method,
      success: success,
      fail: fail,
      complete: function () {
        // complete
      }
    })
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值