zoukankan      html  css  js  c++  java
  • 入坑微信小程序必经之路(七)获取当前用户登录Openid

    不废话,看代码

     onLaunch: function() {
        var _this = this;
        var url=this.globalData.url+"WeiXinConfig";
        wx.request({
          method: 'POST',
          url:url,
          header:{
            'content-type':'application/json'
          },success(res) {
            var Resdata=JSON.parse(res.data.d);
            if(Resdata[0]!=undefined){
              wx.login({
                //获取code
                success: function (res) {
                  var code = res.code; //返回code
                  var appId = Resdata[0].appid;
                  var secret = Resdata[0].appsecret;
                  wx.request({
                    url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
                    data: {},
                    header: {
                      'content-type': 'json'
                    },
                    success: function (res) {
                      var openid = res.data.openid //返回openid
                      console.log('openid为' + openid);
                      console.log('appid为' + Resdata[0].appid);
                      console.log('appsecret为' + Resdata[0].appsecret);
                      if(openid!=undefined){
                        wx.setStorageSync("openid",openid);
                        // wx.setStorageSync("appid",Resdata[0].appid);
                        // wx.setStorageSync("appsecret",Resdata[0].appsecret);
                      }
                    }
                  })
                }
              })
            }
          }
        });
    

      

  • 相关阅读:
    【背包专题】K
    【背包专题】J
    【背包专题】H
    【背包专题】C
    5972: 【递归入门】全排列
    51nod 1136 欧拉函数【数论】
    【背包专题】G
    【背包专题】I
    【背包专题】F
    各浏览器的Hack写法
  • 原文地址:https://www.cnblogs.com/jstblog/p/15321062.html
Copyright © 2011-2022 走看看