小程序 獲取openid

 wx.login({       

        success: function(res) {                    
               
              if (res.code) {

                var APPID='***************'

                var SECRET='***********************'

                wx.request({
                  
                  url: 'https://api.weixin.qq.com/sns/jscode2session?appid='+APPID+'&secret='+SECRET+'&js_code='+res.code+'&grant_type=authorization_code',

                  data: {},

                  method: 'POST',

                  success : function(ret){

                      that.globalData.openid = ret.data.openid;       //獲取openid  (這個地方已經獲取到了,也可以後臺去獲取,這裏就不多說了)
                      
                      wx.getLocation({

                          type: 'wgs84',

                          success: function(res) {
                              
                              that.globalData.lat = res.latitude  //獲取維度

                              that.globalData.long = res.longitude     //獲取經度

                              wx.getUserInfo({ //用戶微信信息
                                
                                  success: function (rey) {  
                                    
                                    that.globalData.userInfo = rey.userInfo  
                                      
                                    that.yang()    //調用接口
                                  },
                                  //拒絕共享信息
                                  fail  : function(){  wx.hideLoading()   }
                                  
                              })        
                          },
                           //拒絕定位
                          fail : function(e){   wx.hideLoading()        }

                      })
                      
                  },

                  fail : function(errMsg){  console.log(errMsg)      }

                })
              } else {

                  wx.showModal({    title: '提示', content: '獲取用戶登錄態失敗!' + res.errMsg })

              }            
        }  

  });   

  that.yang()  //頁面數據請求完成執行自定義函數
},
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章